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

5

6

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.

Terpineol answered 15/6, 2014 at 23:11 Comment(0)
B
13

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.

Bunion answered 28/7, 2014 at 19:32 Comment(3)
Hi @AUser, Do you know why they separate foreground and background code ?Sinusoidal
It's because when the app is in the background, Android has to show a message in the status bar. When the app is in the foreground, there is no need to show a notification in the status bar, and the plugin calls your callback immediately.Bunion
What does the payload looks like, I am having the same problem?Southing
K
4

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!
Katinakatine answered 25/4, 2016 at 9:23 Comment(0)
S
0

If you look into documentation of "ecb" https://github.com/phonegap-build/PushPlugin#ecb-amazon-fire-os-android-and-ios

Your app may receive a notification while it is active (INLINE). 
If you background the app by hitting the Home button on your device, you may later receive a status bar notification. 
Selecting that notification from the status will bring your app to the front and allow you to process the notification (BACKGROUND).

So in background mode, your "onNotificationGCM" method will be invoke only in below scenarios:

  1. If notification is presenting in alert view and user select "view" option [for iOS platform]
  2. User select notification from device notification tray [for iOS and android]
Swiftlet answered 28/6, 2014 at 9:4 Comment(1)
Thanks for your response. It turns out that i had to clear my cache and everything is working now.Terpineol
I
0

For me the onNotification functions where also not called, not even when the app was in the foreground.

Reading on the internet I made sure the Sender ID was 100% correct, since the register function will return a success even when its wrong.

Now you suggested to clear the Dalvik cache, this seems to need a rooted device or recovery tools installed (something which my Galaxy Nexus test device does not have by default). Still this led me to try renaming the application in the hope it wouldn't use the current cache. This worked.

My steps to solve the problem:

  • Change the widget id value in the config.xml file
  • Delete the application from the phone in the App settings
  • In my case removed the app from the Phonegap build service and created a new one (not sure this is necessary)
  • Rebuild and deploy the application

Start and run and the onNotification function started to work! :)

Update

The same thing started happening again, I tried to prevent rooting my device, since it would need a full wipe, but my same fix did not work again the second time.

After rooting the device and installing a recovery tool, wiping the Dalvik cache did do the trick...

Invalidity answered 30/8, 2014 at 9:19 Comment(0)
C
0

According to the 3rd answer by FactualHarmony I also look at the source code under /src/android/com/plugin/gcm/GCMIntentService.java.

You must have "message" and "msgcnt" keys in your message to make it work when the app is in the background and it works for me.

Hope this helps.

Childers answered 29/4, 2015 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.