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

ramsundark5 picture ramsundark5 · Jun 16, 2014 · Viewed 9.5k times · Source

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.

Answer

FactualHarmony picture FactualHarmony · Jul 28, 2014

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.