3

I have app up and running. Push notifications are working ok. I need that when push arrives, bring app to foreground, on Android. So, what I found is this piece of code:

Intent toLaunch = new Intent(getApplicationContext(), MainActivity.class);

toLaunch.setAction("android.intent.action.MAIN");
toLaunch.addCategory("android.intent.category.LAUNCHER");

Taken from this question: Bring application to front after user clicks on home button

I am trying to put this code in GCMIntentService.java from cordova push plugin. No matter where I put it, on compile i always get this error:

/appdir/android/src/com/plugin/gcm/GCMIntentService.java:94: error: cannot find symbol
Intent toLaunch = new Intent(getApplicationContext(), MainActivity.class);
                                                      ^
symbol:   class MainActivity
location: class GCMIntentService

Any ideas how to access this "MainActivity.class" from cordova plugin .java file?

Community
  • 1
  • 1
Dragan Malinovic
  • 603
  • 6
  • 12

2 Answers2

3

The java compiler is telling you that it doesn't know what MainActivity.class is while compiling GCMIntentService.java. You must import MainActivity class from the package where it is defined e.g. if the package is called cordovaExample then at the top of GCMIntentService.java put

import cordovaExample.MainActivity;

and the class must be declared public

package cordova;

public class MainActivity {
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1

Here is what I did, Changes on GMCIntentService.java file which worked great for me.

import com.package.app.*;


@Override
        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);

            Log.d(TAG, "force launch event");
            Intent wintent = new Intent(context, MainActivity.class);
            wintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(wintent);

            // Send a notification if there is a message
            if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                createNotification(context, extras);
                PushPlugin.sendExtras(extras);
            }
        }
    }
}
karma
  • 903
  • 10
  • 26