Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cordova push plugin callback not invoked when the app is in background

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.

like image 267
ramsundark5 Avatar asked Jun 15 '14 23:06

ramsundark5


2 Answers

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.

like image 129
Mukul M. Avatar answered Sep 19 '22 15:09

Mukul M.


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!
like image 41
remo Avatar answered Sep 21 '22 15:09

remo