Android - Notifications PendingIntent goes to wrong Activity? - android

I currently have a notification that displays in the notification bar and when the user clicks it it should bring them to the root Activity of my application.
I have used android:finishOnTaskLaunch ="true" & android:clearTaskOnLaunch="true" in my manifest to achieve this when the user presses the app icon from the apps screen or the home screen. The user is always taken to the root activity as expected.
However when I use the following code from my notification the user gets taken to the last screen they were on rather than the root activity which is StartActivity.
Does anyone know what I'm doing wrong?
Intent notificationIntent = new Intent(this, StartActivity.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), getString(R.string.notification_app_name), notificationText, contentIntent);
mNotificationManager.notify(1, notification);
Adding these flags helps however it still doesn't work after a restart:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
When I restart my application I set a service running which pops up the notification and when I do this the notification reverts back to the old undesired behavior .
SOLVED:
Solved the issue by adding this flag to the contentIntent of my notification - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED.
Here is an link with some excellent information on the area Google dev group

try this 2 properties
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

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

Go back to previous screen on backbutton pressed after responding to notification

I am using code similar to Creating a simple notification to create and show a notification from a network call.
The issue is that I want the activity that responds to the notification to do it's business and then on a backbutton click, put the previously active activity back in the foreground, with it's back stack intact. This is regardless of if the previously active activity was part of my app or somebody elses.
Currently it is following the generated TaskStackBuilder. Leading it back up the app hierarchy and out to the home screen. This is bad UI-design as it breaks the work-flow of anyone using the device, forcing them to manually go back to their app and spending more buttonclicks than necessary. It is also rather unintuitive.
According to the official desing guidelines this is how it should work. The implementation I linked to higher up makes back button have the same functionality as up button should have
It is also a common way to implement it in a plethora of other apps, including official google ones (google-play update notifications come to mind), so there must be a relatively standard way to do this.
Intent intent = new Intent(getApplicationContext(), MainActivity.class)
//add Intent.FLAG_ACTIVITY_CLEAR_TASK flag this will clear all activitys and
//launched activity at top. Means no other activity of this application will be running
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
or
// add Intent.FLAG_ACTIVITY_MULTIPLE_TASK which start one more task your applications
// where activity will not be cleared;
.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification n = new Builder(context.getApplicationContext())
.setContentTitle("simple notification title")
.setContentText("simple message")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "And more",pendingIntent ).build();
NotificationManager notificationManager =(NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, n);

Waze-like notification icon that returns to app

I'm trying to add a notification icon to my application, that will function much like Waze's notification icon - when you open the status bar and tap on the "Return to MyApp" line, the application will move to foreground - with exactly the same activity stack state it had when it was moved to background.
I went over numerous SO questions, and found a lot of answers that are all very good if I know in advance which activity is going to be shown when the notification is tapped. I obviously don't know which activity it's going to be - it can be any of the app's activities.
I also tried sending a broadcast when the notification is tapped. I can the broadcast in my receiver alright, but from there I'm stuck with the exact same problem - I don't know which activity to launch - I can't find the last activity (I can only find the last task, but since my app has one task, it's no help).
you need to create a pending intent.
Like this:
Intent notifyIntent = new Intent(context, Chat.class);
PendingIntent intent =
PendingIntent.getActivity(Chat.this, 0, notifyIntent, SIMPLE_NOTFICATION_ID);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
please see this link
sample project
related

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