Cordova plugin callback does not start when application is in background

I have a push plugin https://github.com/phonegap-build/PushPlugin.git with a corridor 3.5. When the application is in the foreground, a callback notification plugin is called, and everything works as expected.

When the application is inactive (in the background), notifications are received, and I can see them in the notification panel, but the callback function is not called. My code is based on the example provided in the push plugin. Below my code is simplified to reproduce the problem,

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 rack my brains over this issue during the day. Any help is appreciated.

UPDATE: Finally I found what the problem is. I had to clear the dalvik cache and restart my phone. Happened to me twice so far. Looks like a known issue in android, https://github.com/phonegap-build/PushPlugin/issues/35 .

+6
source share
5 answers

I had a similar problem with Cordova 3.5.0 and PushPlugin 2.2.0: notifications worked when the application was in the foreground, but not when it was in the background or not working. I found a solution by reading the source code of PushPlugin (src / com / plugin / gcm / GCMIntentService.java file): the notification payload should include the message and msgcnt keys.

+13
source

Also, if you use PhoneGap, make sure that you have the necessary attributes included in the message. From the doc phone plugin :

On Android, if you want your on ('notification') event handler to be when your application is in the background, the JSON that you send from GCM will need to enable "content-available": "1" .

I use Ruby gem Rpush and spent a lot of time figuring out why the "notification" handler does not start when the application is in the background. The code that ultimately 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! 
+4
source

If you look at the "ecb" documentation 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). 

Thus, in the background, your onNotificationGCM method will only be called in the following scenarios:

  • If the notification is displayed in the warning window and the user selects the "view" option [for iOS platform]
  • User selects notification from device notification tray [for iOS and android]
0
source

For me, onNotification functions, which are also not called, even when the application was in the foreground.

Reading on the Internet I made sure that the sender ID was 100% correct, as the register function will return success, even if it is incorrect.

Now you have proposed clearing the Dalvik cache, which seems to require a built-in device or recovery tools (something that my Galaxy Nexus test device does not have by default). However, it made me try to rename the application in the hope that it would not use the current cache. It worked.

My steps to solve the problem:

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

Run and run, and the onNotification function worked! :)

Update

The same thing started again, I tried to prevent the rooting of my device, since it would need a full cleaning, but my correction did not repeat the second time.

After rooting the device and installing the recovery tool, clearing the Dalvik cache did the trick ...

0
source

According to FactualHarmony's third answer, I also look at the source code in /src/android/com/plugin/gcm/GCMIntentService.java.

Your message must have the keys "message" and "msgcnt" for it to work when the application is in the background and it works for me.

Hope this helps.

0
source

Source: https://habr.com/ru/post/970878/


All Articles