AJ-- AJ-- - 6 months ago 34
Javascript Question

How to handle Push notification when application is resumed?

Trying to handle 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


  1. When application is in foreground I am getting the alert.

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

  3. When application is in the background and I click on notification the application comes to foreground and I am getting the alert.



But if application is in background and a notification arrives; when I bring the application to the front without clicking on notification, I get no alert. So how to handle this type of situation?

Answer

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