How to automatically Open the app without user act

2019-05-31 10:02发布

问题:

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?

回答1:

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


回答2:

Its the class that implements the activity you wish to launch. LoginActivity, MainActivity, whatever it was you named it.



回答3:

public void getnotification(View view) {

       NotificationManager notificationmgr =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
       Intent intent = new Intent(this,resultpage.class);
       PendingIntent p = PendingIntent.getService(this,(int) System.currentTimeMillis(),intent,0);


       Notification n = null;
       if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
           n = new Builder(this)
                                .setSmallIcon(R.mipmap.ic_launcher)
                               .setContentTitle("Hello android user here")
                               .setContentText("welcome to notification serrvice")
                               .setContentIntent(p)
                               .build();

           notificationmgr.notify(0,n);

       }