Hi,
I'm using this plugin in a Cordova App (using Cordova 5.1.1, android-minSdkVersion=15, android-targetSdkVersion=22) in Visual Studio 2015 to send push notifications with a server. WNS and APNS work without a problem, but I coulnd't get GCM on Android to work. I tested it on an Android 4.0.4 and on an Android 5.1 device.
After debugging the plugin, I found that it seems to use the wrong message keys.
My scenario is the following:
- When the Cordova app starts and the deviceready handler is called, I register for notifications (using the Google Project number), and on registration I send the registration ID to the server. I also put an alert in the
"notification"
event handler to verify that a notification has been received (I have obscured IDs etc. in the sample code):
function onDeviceReady() {
// Register for notifications.
var push = PushNotification.init({
"android": { "senderID": "xxxxx" },
"ios": {}, "windows": {}
});
push.on('registration', function (data) {
// Send data.registrationId to the server...
});
push.on('notification', function (data) {
alert("Notification: " + data.message)
});
};
The "registration"
event handler is successfully called and the registration ID is sent to the server. Some time later the server sends a JSON message to the Google GCM service (https://gcm-http.googleapis.com/gcm/send):
{
"registration_ids": [
"APA91bHxxxxxxxxxxxxxxxxxx"
],
"notification": {
"title": "Hi",
"message": "Hi, this is a Test"
}
}
GCM then responds with the following JSON:
{
"multicast_id": xxxxxxxxxx,
"success": 1,
"failure": 0,
"canonical_ids": 1,
"results": [
{
"registration_id": "APA91bFakxxxxxxxxxx",
"message_id": "0:xxxxxxxxxxxxxx"
}
]
}
So, GCM sucessfully received the message. However no notifications did show on android when the app was in the background, and when it was in the foreground the alert also did not apper.
I then inserted some debug statements in the GCMIntentService.java file and found that it seems to use the wrong keys for GCM message.
private void onMessage(...) contains the following code:
if (extras.getString("message") != null && extras.getString("message").length() != 0) {
createNotification(context, extras);
}
So it checks for a "message"
key. However, after printing extras.toString()
, I got the following:
Bundle[{gcm.notification.message=Hi, this is a Test, gcm.notification.title=Hi, collapse_key=do_not_collapse, from=xxxxxxxxxx}]
So, actually the keys are "gcm.notification.message"
instead of "message"
and "gcm.notification.title"
instead of "title"
. After I replaced extras.getString("message")
with extras.getString("gcm.notification.message")
etc. in this class and in the PushPlugin class, everything worked - the phone showed the notification in the notification bar.
Any idea why the notification payload fields in the bundle contain the prefix "gcm.notification."
which the plugin does not check? I haven't found a similar report though.
Also it seems strange to me that the plugin expects the message text in a "message"
field, whereas the GCM Server Reference (https://developers.google.com/cloud-messaging/server-ref#notification-payload-support) specifies that the text should be in a "body"
field in the notification payload.
Thanks!