Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Refresh Activity from Notification

I have a program in which I call a notification. The notification, if you pull it down, launches a new activity.

mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.stat_sys_secure_green;
CharSequence tickerText = "Browser Security Enabled";
long when = System.currentTimeMillis();

notification = new Notification(icon, tickerText, when);

Context context = getApplicationContext();
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Security Vulnerability Detected";
Intent notificationIntent = new Intent(this, PrivacyMessage.class);

//Test Extra
notificationIntent.putExtra("Primary Key", "Primary Text");

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

mNotificationManager.notify(HELLO_ID, notification);

The problem comes later in the code, when I want to refresh the secondary activity. The main activity should be able to dynamically change the extras in it. I tried doing this by launching a new intent.

CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Test New Notification";
Intent intent = new Intent(this, PrivacyMessage.class);
notification.icon = R.drawable.stat_sys_secure_orange;

intent.putExtra("Test Thing", "Test Value");
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent cI = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "New Title", "NewText", cI);
mNotificationManager.notify(HELLO_ID, notification);

Now, when I execute that code, the new notification title pops up, the icon color changes, and the pulldown reflects the new title and addition information. However, when I click on it, it does not launch the activity with the new intent. Instead, it just pulls out the old activity with the old extras. I tried both FLAG_ACTIVITY_CLEAR_TOP, and FLAG_ACTIVITY_NEW_TASK, but neither one seems to clear the old secondary activity and create a new one. Any idea on how I might do that?

like image 309
Lark Avatar asked Jul 26 '11 17:07

Lark


People also ask

How do I manage multiple notifications on Android?

Set the unique id to let Notification Manager knows this is a another notification instead of same notification. If you use the same unique Id for each notification, the Notification Manager will assume that is same notification and would replace the previous notification.


1 Answers

Apparently this is actually a bug/feature of the android environment. Unless a pendingIntent() is passed with a unique requestCode, it simply retrieves the old intent that was originally passed with that number.

Details can be found here: http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4

The solution they came up with was to simply increment the requestCode every time pendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) is called, and set the flags the way I had done it originally with

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Which, doesn't seem like a perfect solution, but it works. Thank you guys for your help!

like image 154
Lark Avatar answered Oct 21 '22 22:10

Lark