protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification notification = new Notification(R.drawable.icon, "Upload Started", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Upload", response, pendingIntent);
nManager.notify((int)System.currentTimeMillis(), notification);
}
This function will be called multiple times. I would like for each notification
to launch testActivity when clicked. Unfortunately, only the first notification launches testActivity. Clicking on the rest cause the notification window to minimize.
Extra information: Function displayNotification()
is in a class called UploadManager
. Context
is passed into UploadManager
from the activity
that instantiates. Function displayNotification()
is called multiple times from a function, also in UploadManager, that is running in an AsyncTask
.
Edit 1: I forgot to mention that I am passing String response into Intent intent
as an extra
.
protected void displayNotification(String response) {
Intent intent = new Intent(context, testActivity.class);
intent.putExtra("response", response);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This makes a big difference because I need the extra "response" to reflect what String response was when the notification was created. Instead, using PendingIntent.FLAG_UPDATE_CURRENT
, the extra "response" reflects what String response was on the last call to displayNotification()
.
I know why this is from reading the documentation on FLAG_UPDATE_CURRENT
. However, I am not sure how to work around it at the moment.
Don't use
Intent.FLAG_ACTIVITY_NEW_TASK
for PendingIntent.getActivity, use FLAG_ONE_SHOT insteadCopied from comments:
Then set some dummy action on the Intent, otherwise extras are dropped. For example
I see answers but no explanations. Also none of the answers address all possible solutions, so I'll try to make that clear.
Documentation:
Cause of the problem:
You create 2 notifications with 2 pending intents. Each pending intent is associated with an intent:
However, these 2 intents are equal, therefore when your 2nd notification arrives it will launch the first intent.
Solution:
You have to make each intent unique, so that no pending intents will ever be equal. How do you make the intents unique? Not by the extras you put with
putExtra()
. Even if the extras are different, the intents might still be equal. To make each intent unique, you must set a unique value to the intent action, or data, or type, or class, or category, or request code: (any of those will work)intent.setAction(...)
intent.setData(...)
intent.setType(...)
intent.setClass(...)
intent.addCategory(...)
PendingIntent.getActivity(context, YOUR_UNIQUE_CODE, intent, Intent.FLAG_ONE_SHOT);
Note: Setting a unique request code might be tricky because you need an int, while
System.currentTimeMillis()
returns long, which means that some digits will be removed. Therefore I would recommend to either go with the category or the action and setting a unique string.I had the same problem, and was able to fix it by changing the flag to:
Was struggling with
RemoteViews
and several differentIntents
for eachButton
onHomeScreen
Widget. Worked when added these:1.
intent.setAction(Long.toString(System.currentTimeMillis()));
2.
PendingIntent.FLAG_UPDATE_CURRENT
I had the same problem and i fixed it by the below steps
1) Clear any flag for intent
2) insert intent.setAction by the below code
3) for Pendingintent ,insert the below code
I hope to work with you