GCM push notification message outside data dictionary - android

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"
]
}

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

Not getting FCM notification when app is completely killed

I have been trying to send the Push notification using FCM. It works completely when the app is in Foreground or background. But if i kill the app from recent list then i'm not getting any notification.
This is my structure of FCM with data payload
{"registration_ids":["here is my FCM token"],
"data":{
"body" :"Hello All, Hope you had a great time!",
"title":"Title",
"click_action":"ACTIVITY_MESSAGE",
"sound":"default",
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
This is the structure with data and notification
{"registration_ids":["Here is my FCM token"],
"notification":{
"body" : "Hello All, Hope you had a great time!",
"title":"title here",
"click_action":"ACTIVITY_HOME",
"sound":"default"
},
"data":{
"type" : "message",
"message": "message_here",
"time" : "2016-12-14 03:37:pm"
}
}
I have also tried the same with notification tag and data as well but nothing is working in case whent the app is killed. If any one have any idea how can i get the notification in case of my app is killed then do share.
You should try to put "forceStart": "1" inside the data brackets.
In this way you should force the application to start the notification.

GCM not using data from JSON payload in the notification

I am sending the following JSON through GCM (Google Cloud Messaging) but I have not been able to get the right response data through the Client. I get a push notification response, but the title is the App's name, and the texts reads: "message" so I can't display my notification properly.
Here is the JSON that I am trying to send:
{
"to": "somekey",
"notification": {
"body":"Test",
"title":"Test"
},
"data": null
}
I think that your problem is on the key-values of the payload of your message.
As you can see at GCM Server documentation, the payload can be set using two different keys:
data: This parameter specifies the key-value pairs of the message's payload.
notification: This parameter specifies the key-value pairs of the notification payload.
You should use the data key and set inside the payload of the notification. Once send, you can print the result of the notification received and see that there are the right payload inside the data key.
Your message should be like the following JSON data:
{
"registration_ids" => "some_target_device_id",
"data": {
"title": "My title",
"message": "This is the message!",
}
}
Authorization: key= AIz......#GCM project key
{ "data": {
"title": "Summer Offer.",
"message": "Click to visit the offer."
},
"to" : "Device Token"
}

Android Push Notifications: How do I show a title?

We are using the Google Cloud Messaging API to push Android notifications. We are executing the https://android.googleapis.com/gcm/send endpoint with a payload similar to:
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!"
}
}
The notification works and the phone shows our app logo, and the message from above. However, there is no title in the notification (above the message and to the right of the logo):
For reference, we are using Phonegap 3.3 and PhoneGap Build.
Well, a stab in the dark of adding "title" fixed it. I wasn't able to find this documented at http://developer.android.com/google/gcm/server.html#params
{
"registration_ids" : ["..."],
"data" : {
"message" : "You received cash back!",
"title" : "Title"
}
}
Now in docs:
https://developers.google.com/cloud-messaging/server#payload
Have a look at the documentation here:
{
"registration_ids" : ["..."],
"notification" : {
"body" : "You received cash back!",
"title" : "Title",
"icon": "ic_notification" // replace with your own
}
}
Edit: cloud messaging is now deprecated and this example is no longer valid. The link to the updated documentation is at firebase
its working my side
$message = array("message" => $pushMessage,"title" => "My Notification");
$fields = array(
"registration_ids" => "device id to send notification",
"data" => $message
);

Android Push Notification Message format

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.

Categories

Resources