【问题标题】:Phonegap PushPlugin message event gets called twicePhonegap PushPlugin 消息事件被调用两次
【发布时间】:2013-10-09 11:46:39
【问题描述】:

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

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

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

这是我的代码中的简短 sn-p:

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 中:Issue

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

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

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

【问题讨论】:

    标签: javascript cordova notifications phonegap-plugins


    【解决方案1】:

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

    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 日志并注意到,当应用程序关闭并发送新通知时,应用程序会收到通知,但插件处理它以某种方式出错并且不会显示它。也许很快就会有修复。

    【讨论】:

    • 谢谢。我希望你能尽快找到其他部分的解决方案。
    • 谢谢,这对我有用。我无法重现您的第二期。你在什么设备上看到的?
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2011-10-05
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多