Android - Notification Pending Intent - Add to top of activity stack issue - android

I'm a bit confused with pending intents in the notification builder. I've got a MainActivity activity and a MessageList activity. I have a service to show a notification when a new message in found, and I want it to be that if the user presses the notification it opens to the MessageList activity but when they press back they will return to the activity they were in.
Essentially I want to add MessageList activity to the top of the activity stack when they press the notification without modifying the current activity stack.
Thank you

Okay, so I got it to work with some old code I wrote a while back. This does what I wanted -
Intent notificationIntent = new Intent(this, AlertListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
mBuilder.setContentIntent(contentIntent);

Related

How to handle navigation when tapping on notification

I have implemented push notification for the app. When notification is tapped, How to start the application from splash screen if the app is already killed. I want to start from Splash screen if the app is already killed and start from Inside landing screen if the app is already in the background. How to handle this? Please help me.
Add this code to your create notification method:
Intent resultIntent = new Intent(this, SplashActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Play around with the intent flags [resultIntent.addFlags(/*intent flag here*/)], if you want to:
start from Splash screen if the app is already killed and start from
Inside landing screen if the app is already in the background.
Hope this helps!
Use pendingIntent to specify the action which should be performed once the user select the notification.
Example tutorial Here

PendingIntent launches different activity of the app package

I'm developing notifications in my app, and I'm having some issue with the pending intent that is driving me crazy.
Normal flow: My app has the launcher activity (Activity A, singleTop) which shows a Splash and then launches Activity B (singleTop too).
Notification:
When app is in background, I show a notification on the notification bar, which opens the launcher activity of my app when clicked, through a PendingIntent. This PendingIntent addresses to Activity A (singleTop). But in this scenario, instead of open Activity A, it brings to foreground the Activity B, but without calling onNewIntent() (onResume() is being called instead), so I can't retrieve the extras of the notification Intent and show the information, because this Activity B.getIntent() retrieves the old intent which opened the activity the first time.
Can any of you bring me some light on this issue, please?
This is how I set up the PendingIntent:
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Edited:
Following the answer given by #Woodi_333, the code for creating the pending intent is as follow.
Intent notificationIntent = new Intent(context, SplashActivity.class);
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TYPE, "Notification");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_MESSAGE, "Message");
notificationIntent.putExtra(StaticResources.EXTRA_NOTIFICATION_TITLE, "title");
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Some code would help but I can take a guess at what is causing this.
So it sounds like the Activity Stack before clicking the PendingIntent is A,B. When opening the PendingIntent, it closes B because of FLAG_ACTIVITY_CLEAR_TOP but leaves activity A alone so it remains running.
So onNewIntent won't run because A is still running, and the FLAG_ACTIVITY_SINGLE_TOP flag won't cause it to run as when the intent is fired, it is not on the top of the history stack.
You might want to combine it with FLAG_ACTIVITY_NEW_TASK as suggested in Documentation for FLAG_ACTIVITY_CLEAR_TOP
I'd also recommend looking at the flags you defined in the manifest to see if they are interfering with the flags of the Pending Intent, or add them to the activities so every time you launch them, they are obeying the same rules.

updating android activity when a user taps a notification

I use this code, inside a notification, so when the user taps the notification the MainActivity starts:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
If the user exits the app with the back button and taps a notification then main activity starts but if the user exit the app with the home button a new MainActivity starts over the first one. How can I detect if the MainActivity is loaded?
Thkx
You should add flags to your Intent to specify this behavior. Something like FLAG_ACTIVITY_CLEAR_TOP may be what you're looking for. This will finish any Activities on top of MainActivity (if any) and bring any existing instance to the foreground. If no instance is available, one will be created. Depending on your launchMode, you'll either get a callback to onNewIntent() (receiving the new Intent you provided) or the activity will be recreated with the new Intent.
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent.getActivity(this, 0, mainIntent, 0);

Android notification to bring back existing activity

I have a notification in my app and I want it to bring back an existing activity when the user clicks it.
The notification is genereated within the activity I want to bring back so I assume it still exists.
This is my code for the notification:
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification noti = new Notification(R.drawable.icon, "s", 0);
CharSequence title = "S";
CharSequence details = "W";
Intent intent = new Intent(getBaseContext(), Start.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
PendingIntent pending = PendingIntent.getActivity(getBaseContext(), 0, intent, 0);
noti.setLatestEventInfo(Start.this, title, details, pending);
nm.notify(0,noti);
But for some reason it keeps creating a new activity.
what am I doing wrong?
"The notification is genereated within the activity I want to bring back so I assume it still exists." Well, it may, but it's not generally a good idea for you to count on that. What if the user clicks on the notification only after backing out of your app using the back button, or after some length of time during which the OS has killed your activity while the user has been doing other things? Is there a particular reason why you need the "old" activity? If it has a state that you need to preserve or recreate, you can do that: have a look at http://developer.android.com/training/basics/activity-lifecycle/recreating.html and http://developer.android.com/guide/components/activities.html#SavingActivityState.

android notification while the notification icon is clicked

I wrote my notification intent like this(the snippet below). I have Activity A,B and C running. While running C, I pressed the home screen and soon received a notification. I pressed on the notification icon, hoping restart the app from Activity A but unfortunately it doesn't. What this code current do is create a new Activity A on top of the stack.... So I am stuck with the following running activity(or stack):A,B,C,A
So my main question is, how can I clean up the Activity stack so that only Activity A is on the stack when the notification icon is clicked?
Any tips or comments would appreciated.
Intent notificationIntent = new Intent(context,
A.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(appContext, contentTitle,
contentText, contentIntent);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.notify(1, notification);
Set launchMode="singleTask" for the activity A in the manifest. In this case activity A will be the only one running after you go back to it from notification.
However when launch mode is singleTask or singleInstance your app is going to behave the same way when you resume your app from the background. It's not going to be possible to resume to activity B or C.
This question may help you either with FLAG_ACTIVITY_CLEAR_TOP or using startActivityForResult.

Categories

Resources