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?
Related
I have an app with Android and iOS users, subscribed to Firebase Clould Messaging topics. Some users have access to send a message to these topics. I make an http request to send the message. The body looks like this:
{
"to": "/topics/debug"
"notification" : {
"body" : "Hi there",
"title" : "Wow!",
"sound": "default"
},
"priority": "high"
"data": {
"title": "Hi there",
"body": "Wow",
}
}
For android, in order to send data message, I need to use the "data" key, so that onMessageReceived is called.
However, ios needs the "notification" key, so that even if the app is killed the message will display. But, whenever I add the "notification" key, the android devices don't call onMessageReceived, and it isn't treated as a data message.
How can I solve this issue, to make one request which is work for both devices. If I do two requests (one with "notification" for ios, and one with "data" for android, the android devices get two notifications (one from onMessageReceived, and one from the system. Can I stop the android devices from showing the message if its sent with just the "notification" key?
I hope I explained the issue well, since this is how I understand the problem I have.
Thanks
I'm not sure this counts as an answer, but I realised an easy workaround, that I prescribed different topics to android and ios users, and set different code appropriate for each one, instead of having one topic for all users.
As far as i am aware of FCM to override my notificaiton on server if it is not deleivered(means device is offline), i can set maximum of 4 keys as stated here
To mark a message as collapsible, include the collapse_key parameter
in the message payload. FCM allows a maximum of four different
collapse keys per device to be used by the app server at any given
time. In other words, the FCM connection server can simultaneously
store four different collapsible send-to-sync messages per device,
each with a different collapse key. If you exceed this number, FCM
only keeps four collapse keys, with no guarantees about which ones are
kept.
So i experimented with this code in POSTMAN,
{
"to":"egAhXXXXXXXXI-HDeLB0SgpK5AmJ8nda1B1TNZrIV-Rpl-1Fqt15UPvfSBdYUWGQoJqaMCz29RvRELdaNE-KaiQvOP-QbmmcWMKe5Y_P7lA38NFiIl2RQHaVh5JJCtv6IId-7kRG1K76o",
"notification" : {
"body" : "Body one",
"title" : "Title",
"icon" : "beintent",
"sound": "getsintheway",
"color":"#ff0000"
},
"collapse_key":"Key 1"
}
I sent four messages with two keys(two message with each collapse_key). But only got last message when device came online.
Anyone having idea why it is so? And what exactly the use of collapse_key as same thing I can do from tag?
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.
i want to know how we can send multiple user's specific push notifications messages in a row.
For example using GCM, if you want to send a specific message to a specific user you can use this content in your POST request to Google servers
{
"to" : "user_registration_id",
"priority" : "normal",
"notification" : {
"body" : "You have 3 messages in your inbox",
"title" : "NewsMagazine.com",
"icon" : "new",
}
}
The problem that i'am facing right now, is that i have tons of notifications to send at a time, each notification is user specific, so i have to make multiple POST requests in a row to Google servers to send each notification separately, and this cause a huge performance issue on my server. What i want is to be able to send them in a single POST request
Any idea on how this might be implemented ?
Thank you.
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.