1

我目前正在为 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 repo中作为问题发布:问题

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

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

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

4

1 回答 1

1

好的,所以我自己想出了我的第一点。我不再使用警报功能,而是使用了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 日志并注意到,当应用程序关闭并发送新通知时,应用程序会收到通知,但插件处理它以某种方式出错并且不会显示它。也许很快就会有修复。

于 2013-10-11T08:04:21.300 回答