Search code examples
androidandroid-intentcordova-pluginslaunching-application

How to automatically Open the app without user action on receiving a push on Android


I am trying to open an app when a push notification is received.

Code:

public class GCMIntentService extends GCMBaseIntentService {

    public GCMIntentService() {
        super("GCMIntentService");
    }

    @Override
    protected void onMessage(Context context, Intent intent) {
        Intent dialogIntent = new Intent(this, MyActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(dialogIntent);    
    }
}

This is what I have so far. But I cannot figure out what MyActivity.class is.

Let's say I want to start the app and that's it. I do not want to add another activity to the AndroidManifest.xml.

Can't I simply use MainActivity.class for Cordova?

What exactly is MyActivity.class?


Solution

  • PackageManager.getLaunchIntentForPackage can help you find the entry activity.

        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(launchIntent);