Bring app to foreground when receiving GCM Push notification (Cordova)

800 Views Asked by At

I am using the latest GCM Push Notification plugin with Cordova. The notifications are working great, but I want the application to come to the foreground when the user receives a push notification.

I have tried several solutions from many places but I still cannot make the application come to the foreground when a notification is received.

It seems that someone managed to do something similar here, but no matter what I do I cannot replicate success:

Bring cordova application to foreground when push arrives

The codes for the GCMIntentService.java in his question are different to what's available now.

How can I get this to work?

1

There are 1 best solutions below

1
On

Incorporate following lines in onMessageReceived method in GCMIntentservice.java file.

@Override
    public void onMessageReceived(String from, Bundle extras) {
        Log.d(LOG_TAG, "onMessage - from: " + from);

        if (extras != null) {
            Context applicationContext = getApplicationContext();

            SharedPreferences prefs = applicationContext.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
            boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);
            boolean clearBadge = prefs.getBoolean(CLEAR_BADGE, false);

            extras = normalizeExtras(applicationContext, extras);

            if (clearBadge) {
                PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0);
            }

            // if we are in the foreground and forceShow is `false` only send data
            if (!forceShow && PushPlugin.isInForeground()) {
                Log.d(LOG_TAG, "foreground");
                extras.putBoolean(FOREGROUND, true);
                extras.putBoolean(COLDSTART, false);
                PushPlugin.sendExtras(extras);
            }
            // if we are in the foreground and forceShow is `true`, force show the notification if the data has at least a message or title
            else if (forceShow && PushPlugin.isInForeground()) {
                Log.d(LOG_TAG, "foreground force");
                extras.putBoolean(FOREGROUND, true);
                extras.putBoolean(COLDSTART, false);

                showNotificationIfPossible(applicationContext, extras);
            }
            // if we are not in the foreground always send notification if the data has at least a message or title
            else {
                Log.d(LOG_TAG, "background");
                extras.putBoolean(FOREGROUND, false);

                Intent wintent = new Intent(context, MainActivity.class);
            wintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(wintent);

                extras.putBoolean(COLDSTART, PushPlugin.isActive());

                showNotificationIfPossible(applicationContext, extras);
            }
        }
    }

And make sure you import the MainActivity.java