Android Push Notification Message format - android

I am working on an application and implemented push notifications for that.
Previously everything was working fine. In the onMessage() method of GCMIntentService class, I was getting the message in the format like this:
{
"collapse_key": "null",
"time_to_live": 108,
"delay_while_idle": true,
"data": {
"message": "Hello android",
},
"registration_ids":["4", "8", "15", "16", "23", "42"]
}
This is what I am sending from my server, and was able to parse it properly. I mean intent of onMessage() contained "data","registration_ids"...
But my problem occurs now - I am not getting the message in the format :
"data": {
"message": "Hello android",
},
Instead I am getting a value only for "message", no value for "data". It's null.

My suggestion is that you put everything you need inside the "message" key because that's all you'll be getting. When you receive the message you can parse it back with your own structure. Hope it helps.

You should be getting only the keys contained within the data dictionary and their values. So in your case, message is all you should get.

Related

FirebasePushNotificationPlugin(xamarin) - events not fire when app in background(android)

I use this plugin for my Xamarin Forms app(android).
Any event not fire when app is in background. Events work when the application is in the foreground. But app is background any event not fire, the app is just opening. But I need to pass the message parameters to the application. I'm not sure if this is a bug, maybe this is my mistake. I will be grateful for any help.
I use this api for send message:
https://fcm.googleapis.com/fcm/send
This is my payload:
{
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept"
},
"notification" : {
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
I also subscribe to all available events - OnNotificationAction, OnNotificationOpened, OnNotificationReceived.
I also try to subscribe at the android level, but this also does not work. Any help?
When the app is in the background firebase use their notification system that way your code not called.
If you want that your code will be called you need to pass the data through the data object only and not use the notification object.
so your notification object need to be like that:
"data": {
"id_serv" : "12",
"id_group" : "1",
"click_action":"Accept",
"body" : "test",
"title" : "test"
},
"priority": "high",
"condition": "'g1' in topics"
}
More info here: FB Receive massages
i don't know anything about xamarin, but in firebase version above 16, you just got push notification data when app is running,
when app is killed: after click on notification, android open your first activity with bundle (extra data), and you must get this data and use them, so debug your app and check your first activity bundle after clicking on notification

How to config Firebase Push notification Message

I have found various examples on how to configure the message json for push notifcations on firebase for android and ios. But I have a problem, that not all push messages arrives to my client apps. I have noticed that on different android versions it work in a different way. Some of them, doesn't receive any message, some of them without the configured ringtone, some of them only with the default ringtone and some works fine. On client side I think every thing should be fine. I'am using xamarin forms for that. My message json looks like this. I have read now that I should remove the notification tag but it works then for older androids?
{
"to": "/topics/MYTOPIC",
"notification": {
"title": null,
"body": "test",
"sound": "de900",
"content_available": true
},
"priority": "high",
"data": {
"missionGuid": "",
"eventGuid": "",
"messageGuid": "e3ab4c34-125b-4ea7-abf7-3ee8fe1453ce",
"ric": "199900",
"title": null,
"body": "test",
"priority": "high",
"sound": "de900"
},
"android": {
"priority": "high",
"notification": {
"title": null,
"body": "test",
"sound": "de900",
"content_available": true
},
"data": {
"missionGuid": "",
"eventGuid": "",
"messageGuid": "e3ab4c34-125b-4ea7-abf7-3ee8fe1453ce",
"ric": "199900",
"title": null,
"body": "test",
"priority": "high",
"sound": "de900"
},
},
"apns": {
"headers": {
"apns-priority": 10
},
"payload": {
"aps": {
"alert": {
"title": null,
"body": "test"
},
"sound": "de900"
}
}
}
}
Setting the "priority" and "sound" in the notification payload never helped me. The behavior of this was very strange.
I decided to use only Data payload and handle both background/foreground messages inside onMessageReceived method and show your custom notifications.
After this I was able to show the notifications with the highest priority, pop-ups and sound.
Read more about Firebase data messages here
As far as I know, setting "priority": "high" is the only thing you can do to make sure the notifications deliver immediately. If they still can't receive anything, it's probably something on the client's side.
For the ringtone, supposedly, "sound": "WHATEVER" is the supposed to specify the sound, but again, because of the different OEMs and system variations, it could be more consistent to just send a data message and push out the notification yourself upon receiving, but you should know that even that probably won't guarantee either deliver or ringtone.
The fact that on some devices this works fine (hopefully the majority of them), indicates you probably have a correct set up for the configuration, and the best you can do is to handle the notification yourself and maybe have special cases for specific devices.

Multiple push notifications using ionic push

When sending two notifications the second one always overrides the first one in the notification center.
According to the docs this should happen only when using the tag:
"tag": Indicates whether each notification message results in a new
entry on the notification center. If not set, each request creates a
new notification. If set, and a notification with the same tag is
already being shown, the new notification replaces the existing one.
However this happens to me all the time, regardless whether I set a value for the tag or not.
I even tried setting a random tag & collapse_key (which as far I understand shouldn't be related in this case, but I gave it a try). Still didn't help.
Here's a sample of a notification object I'm sending:
{
"tokens":[
"my-device-token"
],
"profile":"my-profile-tag",
"notification":{
"message":"message",
"android":{
"payload":{
"collapse_key":9918519,
"tag":2825928
}
}
}
}
I resolved this problem.
You need to add "notId" into "data" in "android".
Example:
"notification": {
"title": "testing stack notification ",
"message":"is it working",
"android": {
"data": {
"title": "testing stack notification",
"message": "is it working",
"style": "inbox",
"summaryText": "yes its %n% notifications",
"notId": "123456"
}
}
}
You need to specify a unique notification id (unix timestamp for example) in the object in order not to overwrite the previous one - in your case:
{
"tokens":[
"my-device-token"
],
"profile":"my-profile-tag",
"notification":{
"notId": <some-unique-value-here>,
"message":"message",
"android":{
"payload":{
"collapse_key":9918519,
"tag":2825928
}
}
}
}

GCM push notification message outside data dictionary

Is there a way to retrieve the key "message" on the device when sending notification to GCM.
{ "collapse_key" : "score",
"data" : { "k1" : "v1" },
"message" : "Hello World!",
"registration_ids" : [ "DEVICE_REGISTRATION_ID_GOES_HERE" ]
}
From the intent.getExtras(), I'm able to receive the following keys "collapse_key", "from", "k1" and their corresponding values as well. However I do not get key "message". If I do not add anything in the "data" dictionary, I still get push notification without "k1" (as expected).
Push to GCM does not result in errors. Does GCM drop keys not mentioned in "data" dictionary?
Yes the way you have it won't work. Anything you want to add, make it part of the data object:
{
"collapse_key": "score",
"data": {
"k1": "v1",
"message": "Hello World!"
},
"registration_ids": [
"DEVICE_REGISTRATION_ID_GOES_HERE"
]
}

Is there a way to add "hidden" data to a GCM (android push) notification?

Is there a way to somehow add some data to the Android push notification by the server side?
So I can parse the added data in my app and do some actions related to it, like an Id or something like that.
Of course it must be "hidden" so the simple message is not the way I want it.
Of course there is. Every piece of data you put in the message payload can be "hidden". As the developer of an Android app, it's your decision which parts of the payload are displayed to the user and which are not.
In the server side you decide which data you wish to put in the payload of the GCM message (inside the data dictionary), and you decide what to do with it in the application.
{ "time_to_live": 108,
"data": {
"message": "you can display this message",
"id": "123456",
"some-hidden-field": "some-hidden-value"
},
"registration_ids":["4", "8", "15", "16", "23", "42"]
}

Categories

Resources