【问题标题】:How to handle Push notification when application is resumed?应用程序恢复时如何处理推送通知?
【发布时间】:2015-05-26 02:15:11
【问题描述】:

尝试使用PushPlugin 处理推送通知。 以下是我的代码。

onNotificationGCM: function(e) {
    switch( e.event )
    {
        case 'registered':
            if ( e.regid.length > 0 )
            {
                console.log("Regid " + e.regid);
                //alert('registration id = '+e.regid);
                sDeviceId = e.regid;
                //alert(sDeviceId);
            }
            break;

        case 'message':
            // this is the actual push notification. its format depends on the data model from the push server
            alert('message = '+e.message);
            alert('message = '+e.message+' msgcnt = '+e.msgcnt);
            if ( e.foreground )
            {
                alert("Notification Received");

            }
            else
            {  // otherwise we were launched because the user touched a notification in the notification tray.
                if ( e.coldstart )
                {
                    alert("coldstart");
                }
                else
                {
                    alert("other than coldstart");
                }
            }
            break;

        case 'error':
            alert('GCM error = '+e.msg);
            break;

        default:
            alert('An unknown GCM event has occurred');
            break;
    }
}

所以一切正常。

  • 当应用程序在前台时,我会收到警报。

  • 当收到消息时点击通知应用 打开,我收到警报。(冷启动)

  • 当应用程序在后台然后点击 通知应用程序进入前台,我得到了 提醒。

但是当我将应用程序保持在后台并且当我将应用程序带到前面时没有点击通知而推送通知到达时,我没有收到任何警报。那么如何处理这种情况呢?

【问题讨论】:

  • 感谢 Ajoy,我已尽力解释问题。不过我会注意您的建议。

标签: cordova push-notification phonegap-plugins phonegap-pushplugin


【解决方案1】:

当应用程序在后台时,您可能需要将通知播放负载保存到 localStorage,然后在应用程序的 resume 事件中从 读取它localStorage 并显示警报消息。

这是link with a similar problem

【讨论】:

    【解决方案2】:

    我也遇到过同样的问题。简单来说,PushPlugin 现在不支持这个功能。但是你可以很容易地修改它以满足你的需要

    现在的运作方式

    当插件的GCMIntentService 收到消息时,会调用onMessage。此方法获取传递给它的所有附加参数,并将它们保存在extras 中。在此之后,如果应用程序处于前台,此函数只需通过调用 sendExtrasextras 传递给您。否则它会调用createNotification,这实际上是在状态栏中创建通知。

    您的问题

    根据我从您的问题中了解到的情况,如果在状态栏中收到通知后,您在未触摸通知的情况下打开应用程序,则不会收到警报。

    会发生这样的事情:

    1. GCMIntentService收到消息
    2. 由于您的应用位于background,PushPlugin 调用createNotification
    3. 当您启动应用时,它不会收到任何警报,因为您尚未触摸状态栏中的通知

    如果您触摸了通知,您会收到警报,因为通知意图会唤醒您的应用。当您的应用唤醒时,它会查找 cached extras,然后通过再次调用 sendExtras 将它们传递给您的应用。

    解决方案

    我尚未对此进行测试,但我坚信,如果您在调用 createNotification 之前(或之后)将插件编辑为 sendExtras,则无论您是否触摸通知或将其滑开。

    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "onMessage - context: " + context);
        // Extract the payload from the message
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
            // if we are in the foreground, just surface the payload, else post it to the statusbar
            if (PushPlugin.isInForeground()) {
                extras.putBoolean("foreground", true);
                PushPlugin.sendExtras(extras);
            }
            else {
                extras.putBoolean("foreground", false);
                // Send a notification if there is a message
                if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                    createNotification(context, extras);
                }
            }
        }
    }
    

    将以上部分修改为:

    protected void onMessage(Context context, Intent intent) {
        Log.d(TAG, "onMessage - context: " + context);
        Bundle extras = intent.getExtras();
        if (extras != null)
        {
            if (PushPlugin.isInForeground()) {
                extras.putBoolean("foreground", true);
            }
            else {
                extras.putBoolean("foreground", false);
                if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                    createNotification(context, extras);
                }
            }
            // call sendExtras always
            PushPlugin.sendExtras(extras);
        }
    }
    

    【讨论】:

    • 感谢 Ajoy 的回答,它有效你能帮我在 IOS 中做同样的事情吗?
    • @AJ-- 抱歉让您久等了。我确实试图理解代码,但到目前为止还不能。我们一起在聊天室讨论这个怎么样?
    • 实际上我对iOS一无所知。所以讨论不会有帮助srry..无论如何我正在寻找ios中的解决方案,如果我发现任何我会在这里发布..谢谢:) :)
    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    相关资源
    最近更新 更多