I raised a notification . What i want is , that when the user clicks on the notification , my activity is brought to the front without creating a new instance of it .
For this , I added the flag , REORDER_TO_FRONT , but still oncreate is being called instead of onNewIntent when i click on the notification .
This is my code -
int icon = R.drawable.android;
long when = System.currentTimeMillis();
CharSequence text = "new message";
CharSequence contentTitle = stanza.getFrom(); // message title
CharSequence contentText = stanza.getBody(); // message text
Intent notificationIntent = new Intent(context, ChatBox.class);
notificationIntent.putExtra("buddyid",stanza.getFrom());
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations above
Notification notification = new Notification(icon, text, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
Have u tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your notificationIntent.addFlag();
The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:
Notification triggers a broadcast action with an extra the name of the activity to launch.
Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag
Activity is brought to the top of activity stack, no duplicates.
Related
Well, I have a simple notification. But when I tap it in Notification bar, I just get opened my app. So how can I know that my app was opened by notification tap or how can I open a specific Activity after tapping?
There is no onClick event on a notification. It is merely launching a prepackaged PendingIntent that is attached to THAT specific notification object.
I think you have this code in Statusbar Notification. When you click the Notification Item, the following Intent is called.
Context context = ctx.getApplicationContext();
Intent notificationIntent = new Intent(ctx, ctx.getClass());
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
If you want specific Activity to be opened, call the particular Class in the above Intent
Is it possible to use startActivityForResult() from a status bar notification?
Say I have an activity A, which on some event starts activity B using startActivityForResult(). Now when it is in the background, on the event it shows a notification. Now on selecting the notification, how do i start activity B for result?
I do realize that activity A should have a service that runs in the background, but i guess the same question would apply even in that case.
Here's the code for the notification. This is in the Activity A.
Notification notification = new Notification(R.drawable.ic_launcher, "New Notification", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
CharSequence contentTitle = "My Notification Title";
CharSequence contentText = "My Notification Text";
Intent notificationIntent = new Intent(this, ActivityB.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
nm.notify(1, notification); //1 = id
I think that you should just start the activity A from the activity B when the activity B is opened by opening the notification and then closed.
You can pass the return value in the intent that you use to start the activity A from the activity B.
Dont use startActivityForResult. You can achieve the same functionality in different way.
Pass the result as an extra with your notification. Get this result in onResume of activity B.
Hi i am new to android.
I am implementing a code with notification functionality. Here i have two activities in my application those are ActivityA, and ActivityB.
I want to start ActivityB from notification and i need to send some flag or some value to the ActivityB. How can i send the data like int value or flag to that called activity using notification on click. The problem is when i am launching activity from launcher icon first it will called ActivityA and from that ActivityA i am passing some value to ActivityB.
But when i am launching ActivityB from notification i con't send any values to that activity so it is giving force close.
To call activity from notification i am using this code
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me";
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setComponent(new ComponentName("mypackage","mypackage.ActivityB"));
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, 0,notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
Please advise me how can i send values from notification to called activity.
you must set your ActivityB in notifyIntent
Intent notifyIntent = new Intent(this, ActivityB.class); // 'this' - Context object
For sending values use extras
for example:
intent.putExtra("yourTag", yourVariable);
I want make a notification, that when clicked on it will bring my app from the background to the front. I am using the following code:
NotificationManager noma = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent, 0);
intent.putExtra("key", "trigerred");
String body = "This is a message";
String title = "This is title";
Notification no = new Notification(R.drawable.ic_launcher, body, System.currentTimeMillis());
no.defaults = Notification.DEFAULT_ALL;
no.setLatestEventInfo(this, title, body, pen);
noma.notify(uniqueID, no);
When I click on the notification that makes a new intent but I want the last created intent brought to the front. How i can do this?
You need to set the FLAG_ACTIVITY_SINGLE_TOP flag on the intent that you pass to getActivity.
This should bring you back to the all ready running activity when clicking your notification.
See here for a list of the different launch flags.
Try this
PendingIntent pen = PendingIntent.getActivity(Timer.this, 0, intent,Intent.FLAG_ACTIVITY_TASK_ON_HOME);
I have been having a problem with a notification not opening/going to the correct activity when it has been clicked.
My notification code (located in a class which extends Service):
Context context = getApplicationContext();
CharSequence contentTitle = "Notification";
CharSequence contentText = "New Notification";
final Notification notifyDetails =
new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis());
Intent notifyIntent = new Intent(context, MainActivity.class);
PendingIntent intent =
PendingIntent.getActivity(context, 0,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent);
((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails);
If I click the notification while the application which created the service is open, the notification disappears (due to the FLAG_AUTO_CANCEL) but the activity does not switch.
If I click the notification from the home screen, the notification disappears and my app is brought to the front, however it remains on the activity which was open before going to the home screen, instead of going to the main screen.
What am I doing wrong? How do I specify the activity that will be pulled up?
May have actually answered my own question:
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), Main.class);