How to handle Push notification when application is resumed?

让人想犯罪 __ 提交于 2019-11-26 14:47:44

问题


Trying to handled push Notification using PushPlugin. Following is my code.

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;
    }
}

So Everything is working.

  • when application is in foreground I am getting the alert.

  • when click the notification when message is received application opens and I am getting the alert.(coldstart)

  • when application is in the background and then clicking on notification the application comes in foreground and I am getting the alert.

But when I keep the application to background and when push notification arrives without clicking on notification when I bring the application to the front i get no alert.So how to handle this type of situation?


回答1:


I have faced the same issue. Simply put, PushPlugin does not support this feature now. But you can very easily modify it to suit your needs

How it works now

When a message is received by the plugin's GCMIntentService, onMessage is called. This method fetches all the additional parameters passed to it, saving them in extras. After this, if the app is in foreground, this function simply passes extras to you by calling sendExtras. Otherwise it calls createNotification, which actually creates the notification in the status bar.

Your problem

From what I have understood from your question, the alert is not received if, after getting a notification in status bar, you open your app without touching on the notification.

This is what happens:

  1. GCMIntentService receives a message
  2. Since your app is in background, PushPlugin calls createNotification
  3. When you start your app, it gets no alert, because you have not yet touched the notification in the status bar

You would have received the alert if you had touched the notification because then the notification intent would wake up your app. When your app wakes up it looks for cached extras and then passes them to your app by again calling sendExtras.

The solution

I have not tested this yet, but I strongly believe that if you edit your plugin, to sendExtras before (or after) you call createNotification, the data will be cached for your application regardless of whether you touch the notification or swipe it away.

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);
            }
        }
    }
}

Modify the above section to:

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);
    }
}



回答2:


When the app is in the background maybe you need to save the notification playload to the localStorage and then in the resume event of the app read it from localStorage and show the alert message.

Here's a link with a similar problem



来源:https://stackoverflow.com/questions/29204334/how-to-handle-push-notification-when-application-is-resumed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!