I have the push plugin https://github.com/phonegap-build/PushPlugin.git configured with cordova 3.5. When the app is in foreground, the notification plugins callback is invoked and everything works as expected.
When the app is inactive(in the background), the notifications are received and i can see them in the notification bar but the callback function is not invoked. My code is based on the example given in push plugin. Below is my code simplified to reproduce the issue,
initialize : function () {
console.info('NOTIFY Ready to register for push notification.');
var pushNotification = window.plugins.pushNotification;
// Register device with push server
pushNotification.register(gcmSuccessHandler, gcmErrorHandler, {
'senderID': GCM_SENDER_ID,
'ecb': 'onNotificationGCM'
});
}
window.onNotificationGCM = function(notification){
//the beep is invoked 3 times only when the app is in foreground
navigator.notification.beep(3);
console.log('EVENT -> RECEIVED:' + notification.event + '');
}
I have been breaking my head on this issue for over a day. Any help is appreciated.
UPDATE: I finally found what the issue was. I had to clear the dalvik cache and restart my phone. Happened to me twice so far. Seems to a known issue in android, https://github.com/phonegap-build/PushPlugin/issues/35.
I had a similar issue with Cordova 3.5.0 and PushPlugin 2.2.0: notifications worked when the app was in the foreground but not when it was in the background or not running. I found the solution by reading the source code of PushPlugin (file src/com/plugin/gcm/GCMIntentService.java): the notification's payload must include "message" and "msgcnt" keys.
Also if you use PhoneGap make sure you have required attributes included in message. From phonegap push plugin doc:
On Android if you want your on('notification') event handler to be called when your app is in the background, JSON you send from GCM will need to include "content-available": "1".
I use Ruby gem Rpush and spent a lot of time figuring out why 'notification' handler isn't fired when app is on background. Code which finally works looks like this:
n = Rpush::Gcm::Notification.new
n.app = Rpush::Gcm::App.find_by_name("MyApp")
n.registration_ids = [ 'xxxxxxxxx' ]
n.data = {
title: "Message Title",
message: "Message Body",
sound: "default",
'content-available' => "1"
}
n.save!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With