javascript - Phonegap PushPlugin 消息事件被调用两次

标签 javascript cordova notifications phonegap-plugins

我目前正在开发适用于 Android 和 iOS 的 Phonegap 3.0 应用程序。我添加了PushPlugin除了两件事之外,几乎所有东西在 Android 上都能正常运行:

1. 当我收到推送通知并且我的应用程序当前不在前台时,该消息会显示在我的通知栏中。一旦我单击通知,应用程序就会启动,并且通知消息会显示两次。显示的消息是一个带有通知数据的简单 JavaScript 警报,我将其添加到“onNotificationGCM”消息事件中。

第一次在通知栏中添加通知时触发此事件,第二次在我单击通知并启动我的应用程序时触发。因此,包含我的消息的警报函数被调用两次,并显示 2 个警报。

这是我的代码的简短片段:

onNotificationGCM: function (e) {
    switch( e.event )
    {
        case 'registered':
            if ( e.regid.length > 0 )
            {
                console.log('Regid ' + e.regid);
            }
        break;

        case 'message':
          // this is the actual push notification. its format depends on the data model from the push server
          console.log('Entered message');
          alert('message = '+e.message);
        break;
    }
}

所以我的问题是,如何防止这种情况并且我的通知仅在我打开应用程序时显示一次?

2. 我也有这个问题,它已经作为问题发布在 github 存储库中:Issue

一旦我退出应用程序(不是通过设置中的“管理应用程序”菜单),我就无法收到任何推送通知。我尝试在启动时启动我的应用程序,但这不起作用。但是当我启动应用程序时,所有通知都会显示。

也许有人已经知道一些解决方法。

我还注意到,PushPlugin 使用已弃用的 GCM 方法。有谁知道这是否可能是原因,为什么即使应用程序未运行,通知也不会显示?

最佳答案

好的,所以我自己想出了我的第一点。我不再使用警报函数,而是使用了使用通知插件的 cordova.exec() 函数。在此我引用了单击警报按钮时的回调函数。之后,我添加了一个小标志,指示是否已看到并单击了警报。并且只要该标志显示该消息尚未被确认,就不会显示其他通知。瞧,当应用程序位于后台时,通知仅显示一次。这是简短的代码片段:

var confirmedGcmNotification = true;

...

onNotificationGCM: function (e) {
    switch( e.event )
    {
        case 'message':
            // this is the actual push notification. its format depends on the data model from the push server
            console.log('Entered message');                

            if ( e.foreground )
            {
                // When app is already open
                cordova.exec(null, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']);
            }
            else
            {  // otherwise we were launched because the user touched a notification in the notification tray.
                if ( e.coldstart )
                {
                    console.log('coldstart entered');  
                    cordova.exec(null, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']);
                }
                else
                {
                    console.log('Background entered');
                    if(confirmedGcmNotification) {
                        confirmedGcmNotification = false;
                        cordova.exec(PushSupport.gcmNotificationBackgroundAlert, null, 'Notification', 'alert', [e.message, 'Notification', 'OK']);
                    }
                }
            }
        break;
    }
},

gcmNotificationBackgroundAlert: function() {
    confirmedGcmNotification = true;
},

第二点有点不同。我还没有解决方案,但我检查了 android 日志并注意到,当应用程序关闭并发出新通知时,应用程序收到通知,但插件以某种方式处理它错误并且不会显示它。也许很快就会有修复。

关于javascript - Phonegap PushPlugin 消息事件被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19271142/

相关文章:

Javascript 二进制文件读取

cordova - 错误 : could not find gradle wrapper within android sdk. 可能需要更新您的 android sdk

reactjs - 如何防止Redux多次显示同一错误?

node.js - 文件上传后谷歌云存储通知

android - 后台定期任务和通知显示

Javascript 关联数组返回空/未定义,尽管已填充在函数内部

javascript - 在javascript中使用http请求下载文件

document.ready 中的 Javascript 函数

javascript - Webkit JavaScript 可以 PUT 或 POST 图像 REST 样式作为纯二进制文件吗?

javascript - 我如何从 inappbrowser cordova 请求桌面站点