views:

104

answers:

3
  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.

A: 

Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity, use FLAG_ONE_SHOT instead

ognian
This flag actually did not work either for the same reason, I think, that my extra is not working correctly (check my Edit 1).
Matt Langston
Then set some dummy action on the Intent, otherwise extras are dropped. For example intent.setAction("foo")
ognian
Excellent. That worked. I setAction(Long.toString(System.currentTimeMillis())) in conjunction with using FLAG_UPDATE_CURRENT that mbauer suggested. Using FLAG_ONE_SHOT only allowed me to click the notification once (which makes sense). Thanks a lot ognian.
Matt Langston
A: 

I had the same problem, and was able to fix it by changing the flag to:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mbauer14
Thanks a lot for taking the time to post what fixed the problem for you. I forgot to mention that I am passing an extra into the Intent. This makes the problem a bit more complex. Check my Edit 1.
Matt Langston