Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
697 views
in Technique[技术] by (71.8m points)

cordova - How to handle Push notification when application is resumed?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...