当应用程序处于后台时,不会调用cordova push plugin回调

ram*_*rk5 6 push-notification cordova cordova-plugins phonegap-pushplugin

我有 使用cordova 3.5配置的推送插件https://github.com/phonegap-build/PushPlugin.git.当应用程序位于前台时,将调用通知插件回调,并且一切都按预期工作.

当应用程序处于非活动状态(在后台)时,会收到通知,我可以在通知栏中看到它们,但不会调用回调函数.我的代码基于push插件中给出的示例.下面是我简化的代码重现问题,

 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 + '');

}
Run Code Online (Sandbox Code Playgroud)

我已经在这个问题上突破了一天多了.任何帮助表示赞赏.

更新: 我终于找到了问题所在.我必须清除dalvik缓存并重新启动手机.到目前为止两次发生在我身上.似乎是android中的一个已知问题,https://github.com/phonegap-build/PushPlugin/issues/35.

Fac*_*ony 13

我在Cordova 3.5.0和PushPlugin 2.2.0上遇到了类似的问题:当应用程序处于前台时通知工作,但是当它在后台或未运行时通知.我通过阅读PushPlugin的源代码(文件src/com/plugin/gcm/GCMIntentService.java)找到了解决方案:通知的有效负载必须包含"message"和"msgcnt"键.


may*_*yur 0

如果您查看“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).
Run Code Online (Sandbox Code Playgroud)

因此,在后台模式下,您的“onNotificationGCM”方法将仅在以下情况下被调用:

  1. 如果通知显示在警报视图中并且用户选择“查看”选项 [适用于 iOS 平台]
  2. 用户从设备通知托盘中选择通知 [适用于 iOS 和 Android]