How to handle Push notification when application is resumed?

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

Solution 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