GCM not using data from JSON payload in the notification - android

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

Related

Formatting background notifications with notifee

Is it possible to format the notification received when app is in background or in quit state?
Firebase messaging displays the notification as it is i.e. as a json string, and does not hit any of the background handlers, it gets directly displayed in system tray.
So basically we want to receive a notification payload body field as notification json format it to string and display with help of notifee.
you can formate the received notification.
for that you need to send only data in payload and set "content_available" : true,
"priority": "high" just like the below payload
{
"to":"Device_token",
"data": {
"title": "JSON.stringify(owner)",
"body": "JSON.stringify(user)",
"picture": "JSON.stringify(picture)"
},
"content_available" : true,
"priority": "high"
}
when receive on device parse it according to your need

Capacitor doesn't recive android specific push-notifications

I try to implement push-notifications to my Ionic4-Capacitor-App.
In the app I have this code:
PushNotifications.register();
PushNotifications.createChannel({ id: '1', description: '2019', importance: 2, name: '2019'});
PushNotifications.addListener('pushNotificationReceived', (notification) => {
console.debug(JSON.stringify(notification));
});
With POSTMAN I try to send the following message:
{
"to": "<User-Token>",
"notification": {
"title": "Default Title",
"body": "Default Body"
},
"android": {
"notification": {
"title": "Android Title",
"body": "Android Title",
"channel_id": "1"
}
}
}
Here is the documentation I used.
The notification I recive has "Default Title" as a title and "Default Body" as a body.
I expected it to have "Android Title" and "Android Body". Additionally, the notification is not pushed to channel 1, but to Miscellaneous.
When I leave out the "root" notification part, no notification is displayed at all.
For everyone who faces the same problem:
Here are the configuration steps I made to send firebase cloud messages via Postman.
Google-Cloud Configuration:
Go To: https://console.cloud.google.com/apis/credentials/consent
Add getpostman.com to the authorized domains
Go To: https://console.cloud.google.com/apis/credentials
Add a new OAuth-Client-ID
Select Webapplication and give it a name.
Set authorized redirect-URL to https://www.getpostman.com/oauth2/callback and save.
Download Json for this account
POSTMAN Configuration:
Set Request-Type to POST
Enter the url: https://fcm.googleapis.com/v1/projects/{your-firebase-project-name}/messages:send
Go to Authorization, select type OAuth 2.0 and click "Get New Access Token"
Grant-Type Authorization Code
Callback URL: https://www.getpostman.com/oauth2/callback
Auth URL: [auth_uri from the json File]
Access Token URL: [token_uri from the json File]
Client ID: [client_id from the json File]
Client Secret: [client_secret from the json File]
Scope: https://www.googleapis.com/auth/firebase.messaging
State: [empty]
Client Authentication: Send as Basic Auth header
When you have done these steps, you should be able to Send messages as described in this documentation: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send

Creating Notifications onesignal not language

I'm adding the push in an application already published, and I just wanted to change how to send the push without having to send a new update to the application.
Today by amazon we send like this:
{"GCM": "{ "data": { "message": "teste" } }"}
And by your system send thus:
{"GCM": "{ "data": { "message": {en:"teste"} } }"}
Is it possible to send a json in this way?
{"GCM": "{ "data": { "message": "teste" } }"}
Due to actual limitations of OneSignal, the default language is always English ('en').
You can check more details in its documentation here: https://documentation.onesignal.com/docs/language-localization

FCM returning response for single registration id when targeted for multiple registration_ids

I am writing app server side of FCM implementation where it will send a particular message to Multiple devices.
I am using registration_ids as key and JsonArray with multiple recipients, however the response returned is showing only for one recipient as follows
{"multicast_id":7185150746202793615,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}
I am not sure whether to set any property in firebase console to enable multiple registration ids acceptance.
Following is the request json :
{
"registration_ids":[
[
"eU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i",
"FERTeU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i"
]
],
"notification":{
"title":"Hello FCM",
"text":"Notification details"
},
"time_to_live":3600,
"priority":"HIGH"
}
And the response for this request is
{
"multicast_id":7697036511101523125,
"success":0,
"failure":1,
"canonical_ids":0,
"results":[
{
"error":"InvalidRegistration"
}
]
}
The Problem was with this Json formation for request. It has to be
{
"registration_ids"[ "eU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i",
"FERTeU6HKx7QRMc:APA91bEIoJtHmTFz_MzUgbMkoZvwdlXzrMDbCB1via_fV16MRF_Xc0C0KkkVA1diR8QgzRdQtkCy-6JDd5it_NSaiIbIlBDDi0g2GFkmrE4ESMi43dBODBePQrzFLqiKeDmX26DkXB8i" ],
"notification":{
"title":"Hello FCM",
"text":"Notification details"
},
"time_to_live":3600,
"priority":"HIGH"
}
The extra square brackets ([ ]) is causing the issue.

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

Categories

Resources