Android - open or restart app after clicking push notification using flag activities
17,202
Solution 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);
Solution 2
set following things in your Android Mainfest file
android:noHistory="true"
android:launchMode = "singleTop"
Author by
DijkeMark
Updated on June 05, 2022Comments
-
DijkeMark almost 2 years
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?
-
DijkeMark over 11 yearsYes, this works. Ecpet now I have another problem. Is there a way execure te code I have in onCreate(), because it contains the same code the push notification has to do. Now the onCreate isn't called, thus the code is not being executed.
-
David Wasser over 11 yearsIf you don't set the flag
Intent.FLAG_ACTIVITY_SINGLE_TOP
, then the current instance of the activity will be destroyed and a new instance will be created (thereby callingonCreate()
). -
Tooroop almost 10 yearsthis is not working for me...do you have to set a particular launch mode in the manifest for this activity?
-
David Wasser almost 10 years@Tooroop no you don't. What is not working? Can you be more specific?
-
Tooroop over 9 yearsI add a flag to the Intent so depending on that flag I open a fragment in my activity. This is OK when I exit my app via back button, but if I exit my app with the home button or when my app is already opened nothing happens.
-
Tooroop over 9 yearsLooks like I solved it. My onNewIntent() method wouldn't be called if I set Intent.Intent.FLAG_ACTIVITY_SINGLE_TOP or launchMode="singleTop" or even btoh at the same time. I added : Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); and in the manifest i set launchMode="singleTask". Now it is working as expected
-
David Wasser over 9 years@Tooroop in general,
singleTask
launch mode is a very bad idea :-( It sounds like you've got something else going on. In general, if you setIntent.FLAG_ACTIVIY_NEW_TASK
and are using your root activity (ie: the starting activity) and your app is already running, Android will just bring your app's task to the front without callingonCreate()
oronNewIntent()
. -
Moritz over 9 yearsI'm experiencing the same issue as @Tooroop. The target activity of the notification just launches as if no flags were set at all and does not receive a call to onNewIntent. @David Wasser: Why is
singleTask
a bad idea? -
David Wasser over 9 years@Moritz perhaps you should post a new question, include your manifest and a description of the problem. It is difficult to diagnose these things in comments to an answer :-( I could write a book about why
singleTask
is a bad idea. Basically you just need to know exactly what you are doing and a lot of developers misuse these launch modes. In general, it creates more problems than it solves: You can end up with multiple tasks which will confuse the user; You need to make sure you have different icons and/or labels for the tasks so as not to confuse the user... -
Renan Franca over 9 yearsTo solve the mistery stackoverflow.com/questions/15229054/… @Tooroop
-
Yousha Aleayoub over 7 yearsWhy
android:noHistory="true"
andandroid:launchMode = "singleTop"
?