FCM collapse_key overrides all previous messages (with different key values) - android

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?

Related

Firebase cloud message to ios and android devices

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.

Is aps dictionary necessary in iOS notification payload? Can we have same payload for iOS and Android?

I just started working on notifications on iOS and it seems that apple has defined a format for payload to receive notifications.
So currently, I am using this payload and everything is working as expected. I am getting title, subtitle, body sound, image.
{"aps" : {
"alert" : {
"title" : "Introduction To Notification",
"subtitle" : "Session 707",
"body" : "New Notification Look Amazing"
},
"sound" : "default",
"category" : "message",
"badge" : 1,
"mutable-content": 1
},
"attachment-url": "https://pusher.com/static_logos/320x320.png"
}
Lets say I want to have a single payload for both Android and iOS.
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
How can I create a payload which works for both?
Update for cross-platform payloads: A recent feature was added for FCM that gives an option to provide specific params for specific platforms, called Platform Overrides.
The sample payload you posted seems to be in-line with the official parameters for APNs. When using GCM or FCM, the parameters to be used are different (see the links).
Is there a standard format defined in Android for notifications or can you set any data in Android and the client has to manually handle and display these notifications?
It depends on which type of message payload you're planning to use. There are 2 types of Messages for GCM/FCM, notification and data.
notification messages only have predefined set of parameters available, while data messages can be used to have custom key-value pairs. Both are usually handled by the client, but note that the behavior for Android and iOS are different depending on the message type you use (see the links).
How can I create a payload which works for both?
As I mentioned in comments section in the other post:
You'll have to do the mapping in your own database/app server. Yes. What I was thinking here was every time a registration token is generated on the client app side, you send it to your database/app server along the type of device (i.e. "Android", "iOS"). So that when you'll be sending messages, you'll first have to check the type of device. I did say it's more work, but it's a sure way to give you control over things. AFAIK, it is the developer's responsibility to keep track of the registration tokens and any details that should be associated with it.
You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
So in your case you should do something like:
{
"aps": {
"alert": "Hello World",
"sound": "default"
},
"Person": {
"Address": "Your address",
"Name": "Your Name",
"Number": "XXXXXXXXXX"
}
}
Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

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?

Send specific push notification messages to different users in a single request to GCM

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.

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