GCM data returning null [duplicate]

I am having issues with the GCM platform and getting any kind of data from a request. I can see the push notification but can retrieve any data from it.

self.addEventListener('push', function(event) {
  console.log('Push message', event); //<--- event.data == null !!! :(

  var title = 'Push message';

  event.waitUntil(
    self.registration.showNotification(title, {
    'body': 'Some Messages',
    'icon': 'images/myImageSmall.png'
    }));
});

I test with a simple curl request

curl --header "Authorization: key=MY_KEY" --header "Content-Type: application/json" 
https://android.googleapis.com/gcm/send -d 
"{\"data\" : {\"THIS_DOESNT\":\"SHOW_HELP_ME\"}, \"to\":\"THERE_ID\"}"

Anybody have any idea why I get the notification without a data body?

Edit: I have also tried this json.

{
"registration_ids":
    [
        "xxxxxxxxx"
    ],
"data":
    {
        "title": "some title",
        "message": "some message"
    }
}

Answers:

Answer

Based on the Official Google Documentation, it can be set using two different keys.

Data, this parameter specifies the custome key-value pairs of the message's payload.

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).

Values in string types are recommended. You have to convert values in objects or other non-string data types (e.g., integers or booleans) to string.

Notification, This parameter specifies the predefined, user-visible 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!",
}
}

For more information about notification message and data message options, see Payload.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.