应用程序恢复后如何处理推送通知?

Anu*_*j.T 5 push-notification phonegap-plugins cordova phonegap-pushplugin

尝试使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

所以一切都在发挥作用.

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

  • 当收到消息时单击通知应用程序打开,我收到警报.(coldstart)

  • 当应用程序在后台,然后单击通知时,应用程序进入前台,我收到警报.

但是当我将应用程序保留到后台并且当我将应用程序带到前面而没有单击通知时推送通知到达时我没有得到警报.那么如何处理这种情况呢?

Ajo*_*joy 4

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

现在如何运作

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

你的问题

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

发生的情况是这样的:

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

如果您触摸了通知,您就会收到警报,因为通知意图会唤醒您的应用程序。当您的应用程序唤醒时,它会查找缓存的额外内容,然后通过再次调用将它们传递给您的应用程序sendExtras

解决方案

我还没有对此进行测试,但我坚信,如果您编辑插件,在sendExtras调用之前(或之后)createNotification,无论您触摸通知还是将其滑开,数据都会为您的应用程序缓存。

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将上面的部分修改为:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)