Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - open or restart app after clicking push notification using flag activities

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?

like image 460
DijkeMark Avatar asked Nov 27 '12 09:11

DijkeMark


People also ask

Does an app need to be open for push notifications?

Push notifications do not require any additional app to be functional — the recipient can see the notification even if the app is not running. Almost every app offers an option for push notifications where the user can opt-out or opt-in to receive those notifications.

What is PendingIntent?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.


2 Answers

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);
like image 120
David Wasser Avatar answered Oct 02 '22 23:10

David Wasser


set following things in your Android Mainfest file

android:noHistory="true"
 android:launchMode = "singleTop"
like image 39
suraj mahajan Avatar answered Oct 02 '22 21:10

suraj mahajan