I am working on a cordova app and using the cordova-fcm plugin to enable push notification on my app. Everything works find, the plugin can retrieve tokens, subscribe to channels and I can send notifications to my device using the FCM HTTP v1 API. The problem is the device does not play any sound! I have tried setting up the payload to "sound":"default" or a custom sound file put in the res/raw directory, but no luck. Now I looked into the source code of the above said plugin and found that onMessageReceived callback does not have the code to forward the notification using notificationManager.notify(). Now I have a confusion here. When I was reading FCM docs, what I understood is that when a message is received in the background or the app is killed, the message is handled by the system and I don't need to do anything with notificationManager.notify(). Do I understand it correct? Also the payload has a "sound" property and it is mentioned in the doc that either I can pass "default" or any custom file name I put in res/raw directory. So how is this handled, I mean which sound needs to be played? Is this handled by the system? Also if I implement notificationManager.notify(), then how do I do setSound() as per the "sound" property in the received payload?
Here is the payload that I have tried...
{
'message': {
"topic": "all",
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM',
},
"android": {
"priority": "high",
"notification": {
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY"
}
}
}
}
Never mind. It seems like a problem with my phone. Sound is played on other devices.. I tried to find a setting on my phone to check if sound needs to be enabled for some particular app notifications, but I could not find it either. Just to let the the users know I am using MI Note 5 Pro. If someone else stumble on this issue, this might help.
Related
I'm trying to get push notifications running with React Native on iOS and Android.
To do so I am using firebase, as that seemed to be very convenient.
On Android I managed to show notifications when the app is in the foreground and at least show a log when the app is in the background, meaning I can process them.
On iOS I have not managed to do any of those, not from our custom backend in aws (via SNS), nor from firebase console with fcm token. What interestingly worked, was to show a badge on the ios app icon when sending broadcast messages to all users. So something seems to be received on the phone.
Now looking into this a little deeper I found that iOS needs notifications in the following format:
{
"Simulator Target Bundle": "com.compass.SomeExampleApp",
"aps": {
"badge": 0,
"alert": {
"title": "Push Notification Test",
"subtitle": "Hey! 👋",
"body": "Is this working?",
},
"sound":"default"
}
}
At least this is how you can test them on the sim.
1. Does it mean that I have to send the notifications in that same format? Or can it be something like:
{
"data":{},
"apns": {
"Simulator Target Bundle": "com.company.Example",
"aps": {
"alert": {
"body": "Wellcome to MyApp!",
"title": "MyApp"
}
}
}}
2. How can I send a format that iOS AND Android can read, show on screen and process in background if necessary?
Since we are sending only data messages I think this might be part of the issue, if I look at our backend.
Still when sending from firebase I am not able to show anything on screen on ios Device. Any hint?
I found a solution that actually works on both platforms although it is a bit weird since it seems to be outdated. Code needs to look like this to send to both iOS and Android:
{ "GCM": "{ \"notification\" : {\"content_available\" : true }, \"data\": { \"body\": \"Sample message for iOS endpoints\", \"title\":\"Hello world\"} }" }
As seen here APNS attributes seem to at least partly work if added within the GCM message like content_available in this case or badge.
We're trying to migrate from silent FCM pushes to loud ones (i.e. from "data":{} managed by the app, to those that "notification": {} causes).
With loud push, FCM client framework automatically creates a notification. Therefore:
I don't have its ID so I can modify it
I can't control its channel ID (which is important starting Oreo).
Not sure if there is a way for my server to request a specific notification sound for it (the same way they do in iOS). Actually, I can do that from onRemoteMessage() but the auto generated notification plays the default beep, which will cause a funny playing of both sounds.
How can I solve those issues?
Thanks
In case of notification payload when FCM client framework automatically creates a notification using notification payload data.
Notification payload contains a key for sound so you have to set it from server side.
For Example :
{
"to" : "yourToken",
"notification" : {
"body" : "Notification Body",
"title" : "Notification Title",
"sound" : "/res/raw/yourSoundResourceFile"
}
}
As per as firebase documentation sound is a Optional string
Supports "default" or the filename of a sound resource bundled in the app. Sound files must reside in /res/raw/.
For more info follow this offical firebase link
I'm new to Cordova and JS stuff, and currently making a Cordova mobile app. The purpose is to send notifications with the help of firebase (I'm focusing on android for now).
The notification has to be send with a phone, to an other phone.
I use JQuery Mobile 1.4.5, JQuery 2.1.0 and Cordova CLI 6.1.1
My project is a Visual Studio project (techno restriction)
I'm using this Cordova FCM plugin: https://github.com/ostownsville/cordova-plugin-fcm
My code to receive the notifications works pretty well: No errors, token generated successfully, notification received ! (notifications sended by firebase console, with the token, etc.).
==> My problem here is that I don't know how to proceed to send notifications to an other phone !
I already have a payload example, that can be use to send the notification. but I don't know how to use it. I'm sure I have to put it in firebase, somewhere, but i don't know where.
Plus, I can't figure how to obtain the token of the phone that I want to send a notification. Is it possible to know the receiver token ?
here is my payload code:
var fcm_server_key = "aiz******************************";
method: "post",
datatype: 'json',
headers: { 'content-type': 'application/json', 'authorization': 'key=' + fcm_server_key },
url: "https://fcm.googleapis.com/fcm/send",
data: json.stringify(
{
"notification": {
"title": "title ", //any value
"body": "body ", //any value
"sound": "default", //if you want notification sound
"click_action": "fcm_plugin_activity", //must be present for android
"icon": "fcm_push_icon" //white icon android resource
},
"data": {
"param1": "value1", //any data to be retrieved in the notification callback
"param2": "value2"
},
"to": "<USER_TOKEN>", //topic or device ==> maybe the place to put the token !?
"priority": "high", //if not set, notification won't be delivered on completely closed ios app
"restricted_package_name": "" //optional. set for application filtering
}
)
==> Is it possible to send notifications with a phone to an other phone, with firebase ? how then :) ?
Finally, I use the the Cordova Contact plugin to list all the contacts in the phone. Is there an other way to show to the user his contacts and then send the notification to the right person ?
Thanks a lot for your help. I've already checked the stackoverflow forum and the plugin issues section in order to solve that problem but I haven't find the solution yet.
(Sorry for bad english, this is my first post...)
To answer to your question there are more levels
My problem here is that I don't know how to proceed to send
notifications to an other phone !
Push notification can be sent through firebase console.
Is it possible to know the receiver token ?
Yes if you Initialize firebase plugin, you will get the firebase token in your javascript function inside FCMPlugin.getToken()
Is it possible to send notifications with a phone to an other phone,
with firebase ?
You can send only from one receiver to another receiver directly using firebase push data. But I wont recommend you that. Because it is not safe to exchange data directly between receivers without a server
Note:
Before starting firebase notification I would like to give you a note. There are 2 types of payloads in firebase.
1.Notification payload : This has a few limitation in receiving notification that, the app will get notification only when app is in background. So notification will not be shown to receiver if he is using the app. It will be handled by your FCMPlugin
1.Data payload : If you need to show the notification even if the app is in foreground then go for it. But you need to trigger the notification manually in your java code in android way
Our app now has targetSdkVersion 26 (Android 8) and the app uses FCM push notifications.
As FCM documentation prescribes I updated the FCM client library to version 11.2.0:
dependencies {
compile 'com.google.firebase:firebase-messaging:11.2.0'
}
With this FCM client library update the FCM notifications started to appear on Android devices. Good, but when app is in background it's system who processes the FCM message, so it uses the default Android notification channel named "Miscellaneous", which is not what we want (we have other notification channels and "Miscellaneous" sounds confusing in that list).
As FCM documentation says there is a way to specify default notification channel for FCM messages:
(Optional) Within the application component, metadata elements to set
a default icon, color and notification channel (new in Android O) for
notifications. Android uses these values whenever incoming messages do
not explicitly set icon, color or notification_channel.
However there is no code sample shown (samples are shown only for icon and color). So I just found by googling a sample in Firebase Cloud Messaging Quickstart on github:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="#string/default_notification_channel_id"/>
But it does not work - FCM notifications still appear within the "Miscellaneous" channel. And I see in the logs:
W/FirebaseMessaging: Missing Default Notification Channel metadata in
AndroidManifest. Default value will be used.
Of course, I tried to reinstall the app. Still having the issue.
Well, ideally there should be some way to specify notification channel(s) on back-end at the moment of sending the messages. The FCM dev console, which allows to test sending, now has such an option in UI:
And it works fine. However our back-end uses Java Amazon SNS API and I have no idea if that API allows to specify Android notification channel when sending a message (because it'a new Android feature, and Amazon needs time to adopt it). So setting a default notification channel in AndroidManifest.xml would be a valid workaround for now, but it does not work.
Look at docs: https://firebase.google.com/docs/cloud-messaging/http-server-ref
android_channel_id The notification's channel id (new in Android O).
The app must create a channel with this ID before any notification
with this key is received.
If you don't send this key in the request, or if the channel id
provided has not yet been created by your app, FCM uses the channel id
specified in your app manifest.
Try to include android_channel_id in json you are about to post to fcm. I have no idea why manifest value is not working for you. Try to just add channel to your request, you should get the same effect as from Firebase Console.
Edit: I just realized you are asking for Amazon client integration. Maybe you are able to build json request manually then (I don't know much about Amazon services, sorry).
FCM has migrated to HTTP v1 API:
https://fcm.googleapis.com/v1/projects/{{projectId}}/messages:send
android_channel_id will cause bad request:
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.notification",
"description": "Invalid JSON payload received. Unknown name \"android_channel_id\" at 'message.notification': Cannot find field."
}
The correct payload should be:
{
"message": {
"token": "{{deviceToken}}",
"notification": {
"body": "This is an FCM notification message hello 23",
"title": "FCM Message",
"image": "https://lumiere-a.akamaihd.net/v1/images/au_moviesshowcase_mulan_poster_r_2_54011055.jpeg?region=0,0,960,1420"
},
"android": {
"notification": {
"channel_id": "channel_id_1"
}
},
"data": {
"key1": "42",
"key2": "sent by 21"
}
}
}
see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
Depend on this resource : https://firebase.google.com/docs/cloud-messaging/http-server-ref
Here Payload look like :
{
"to":"$device_token"
"notification":{
"title":"Title",
"body":"Here Body",
"android_channel_id":"$channel_id", // For Android >= 8
"channel_id":"$channel_id", // For Android Version < 8
"image": "https://xxxxx.com/xxxxx.jpeg"
},
"data":{},
"priority":"normal"
}
I am using Firebase Cloud Messaging HTTP Protocol to send push notifications to my application using postman for testing.
I am using the following code to send the push.
{
"notification":{
"title":"Title",
"body":"this is a notification to a specific topic",
"sound":"default",
"click_action":"FCM_PLUGIN_ACTIVITY",
},
"data":{
"action":"ping"
},
"to":"/topics/Topic_1",
"priority":"high"
}
and I am using this code to handle the notification on my app:
FCMPlugin.onNotification(function(data){
console.log(data);
if(data.wasTapped){
//Notification was received on device tray and tapped by the user.
alert('notification tapped'+ JSON.stringify(data) );
}else{
//Notification was received in foreground. Maybe the user needs to be notified.
alert('application is open'+ JSON.stringify(data) );
}
});
it is all working except i cannot get the title and the body to use them in my application, all I am getting is the following:
Object {wasTapped: false, action: "ping"}
I cannot find a way to get the notification title and body.
I know I can copy them to the data section but that's not logical its a dirty workaround
so any idea how to get the notification data?
thank you.
Basing from the behavior shown, I'm presuming that the client platform is Android. If so, then this is working as expected.
When sending a combination of both notification and data in your message payload, the Android System tray will be the one to handle the values in notification. The dirty workaround you mentioned is the only workaround so far -- a workaround that I actually suggested on one of my answers as well.