Firebase Cloud Messaging Only Seeing Last Notification on Android - android

I am using FCM to send data push notifications to android devices via HTTP protocol. If I send several notifications, phone chimes that many times but I only only see the last notification that was sent in the notifications tray. I am NOT using the collapse_key so they should show up individually. Here is the payload:
{
"data":{
"testId":"3",
"test2Id":"2",
"title":"Test Alert Title 1",
"body":"Test Alert Body 1"
},
"to":"DEVICE_REG_ID_XXXX"
}
UPDATE:
On the device side, we're using phonegap-cordova-push plugin (https://github.com/phonegap/phonegap-plugin-push/tree/v1.9.x) in our Cordova app.
What am I doing wrong?

We were able to figure out the issue so I am posting for others who may have the same issue. The problem was with the plugin in question. Looking at the plugin code, we determined that the plugin was expecting attribute notId in the data payload to mark each notification as unique otherwise they'd get collapsed and only the latest would show up in the notification tray. So payload should like this:
{
"data":{
"testId":"3",
"test2Id":"2",
"title":"Test Alert Title 1",
"body":"Test Alert Body 1"
"notId":"45"
},
"to":"DEVICE_REG_ID_XXXX"
}
And sender needs to ensure that each FCM request has unique notId attribute.

Related

Blank notification appears without any code call

i experience pretty weird thing in my android app. In some unknown for me cases sometimes blank notification apperas in notification bar, without any title, body and with default gray color. The only filled thing is icon, which is my default application icon(from manifest).
There is only one place in project when i create notification manually - intent service that sends data via rest api. Notification have his own icon (different than default), color, text, progressbar and works fine when service is running. I have not configured any Cloud Messaging or push notification in this project.
I spent a lot of time and i have no idea why blank notification described above appears. I will be grateful for any hint how to prevent it.
Yes, I am also facing this issue, when the app is closed that time notification will appear like this. I am resolved this issue in server side.
There are two types of messages in FCM (Firebase Cloud Messaging):
Display Messages: These messages trigger the onMessageReceived() callback only when your app is in foreground
Data Messages: Theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed
Firebase team have not developed a UI to send data-messages to your devices, yet.
refer this link to achieve
https://fcm.googleapis.com/fcm/send
And the following headers:
Key: Content-Type, Value: application/json
Key: Authorization, Value: key=<your-server-key>
Body using topics:
{
"to": "/topics/my_topic",
"data": {
"my_custom_key" : "my_custom_value",
"other_key" : true
}
}
Or to send it to specific devices:
{
"data": {
"my_custom_key" : "my_custom_value",
"other_key" : true
},
"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}
NOTE: Be sure you're not adding JSON key notification
NOTE: To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

How to send notifications with a phone / Cordova mobile app

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

How to get the notification title and body using cordova-plugin-fcm on an ionic 1 app

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.

Contextual push messages and badge icon in FCM

Up until now I have been using APNS and manually connection to their services over TCP. I have packaged and delivered all push notifications in a single operation by writing all of the combined data to APNS.
Things are moving along and Android is starting to become relevant, so I looked into FCM as a common platform for push.
I cannot figure out how I am supposed to send, let's say:
"Hello (username)! Blah Blah blah blah whatever"
... to many users at one.
The FCM docs allow me to specify multiple device tokens, which is great, but they will all receive the same notification. Is there no way to customize this per device/token? Also this is a big problem when it comes to the notification badge count in iOS. If everyone receives the same notification payload, they will also receive the same badge icon count.
Am I really to believe that I am supposed to make an HTTP request for every single user to make this work properly across both iOS and Android?
{
"registration_ids" : ["token1", "token2", "token3"], <--- devices
"priority" : "normal",
"notification" : {
"body" : "Hello, Maria! Whatever blah blah", <-- Same message
"title" : "Whatever",
"badge" : "3" <--- all users receive the same? How?
}
}
I tried posting the payload as an array of above objects in a single request, but that does not work (as expected, since it's not documented).
How do we solve this?

Android silent push notifications

We think that Android’s push notifications by default are silent notifications and must be the developer who programmatically raise the notification on the screen. Or the push notifications aren’t silent by default?
Today we work using this JSON structure which our app receives from the GCM server.
{
"data":
{
“Type” : “2”,
“_dId” : “3718829”,
“_mId” : “9924012”,
“_msg” : “HOLA JVE”,
“collapse_key” : “9924012”
},
"to" : "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
Is this structure valid for silent notifications or there is a different one ?
Truly we are pretty lost on this topic. Following this question, anyone have a json structure of an Android push notification with message and title tags ?
Other question : When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
Or the push notifications aren’t silent by default?
There are two ways that a notification is received in Android, either it's Notification Tray, or you handle it yourself in onMessageReceived() depending on which payload you use (Notification or Data).
Basing from your inquiry (silent push notification), I'm guessing you'd prefer the latter, since you'll be able to handle it yourself. As per the GCM Payload docs:
Use notifications when you want GCM to handle displaying a notification on your client app’s behalf. Use data messages when you want your app to handle the display or process the messages on your Android client app,..
So what you are currently using right now, (a data payload) should be fine. However, if you have both notification and data in your payload, you will have to consider your app's status. Referring to this FCM docs, for data payload:
App state Notification Data Both
Foreground onMessageReceived onMessageReceived onMessageReceived
Background System tray onMessageReceived Notification: system tray
Data: in extras of the intent.
..anyone have a json structure of an Android push notification with message and title tags ?
As also mentioned in the docs I linked above:
Data messages have only custom key/value pairs.
So I think it's safe for you to just use keys so long as it's not a reserved word, as mentioned in this docs:
The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as collapse_key).
When a silent notification is shown to the user ? When the user open the app or just when the user unlock the phone ?
I think what I mentioned above pretty much covers this part (see the table).
On Android, differently from iOS, the app is responsible for creating and showing the push notification. So yes, you can think of them as silent by default, although you don't exactly have this concept on Android.
What I normally do is, if the notification should not be shown, add a silent field. For example:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "Will not show this message",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
"silent": "true"
}
}
And then you can use the silent field to show or not show the notification according to an if statement.

Categories

Resources