Android - open or restart app after clicking push

2019-02-12 11:02发布

问题:

I am using push notifications in Android. When I receive a push notification, I want to open the application if it is still running, else it should open a new instance of it.

I am using

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
    Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);

But when I receive a push notification now and I click on it, nothing happens.

How can I still achieve this by using flagIntents?

回答1:

You need to set the Intent flags on the Intent. You were specifying them in the call to get a PendingIntent. Try this:

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
    Intent.FLAG_ACTIVITY_SINGLE_TOP | 
    Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
    notificationIntent, 0);


回答2:

set following things in your Android Mainfest file

android:noHistory="true"
 android:launchMode = "singleTop"