3

I am creating a notification system that pushes multiple notifications through a FOR loop. Logcat shows that I am creating multiple notifications (The phone even sends two ringtones corresponding to each one). But the notification bar only shows the last one. I read the documentation and it says that calling NotificationManager.notify takes a unique Id and if this Id exists, then the notification will be replaced with the new one. I used a unique GUID for each notification but still no success.

notificationManager.notify(notification.getInt("id"), n);

Any suggestions I can work with? Could it be the PendingIntent part of the notification?

Thanks in advance.

Pacemaker
  • 1,117
  • 2
  • 17
  • 36
  • post the notification builder and pending intent related code, make sure the int in "id" is different, it should work fine – Pararth Jul 23 '14 at 10:20
  • Why can't you just print the id value to log just to make sure the id value is different – user3505725 Jul 23 '14 at 10:21
  • I just logged the Id and you were right. It is showing the same Id , it is really weird because I was using a global variable and changing it in the notification loop. I used a local variable this time. – Pacemaker Jul 23 '14 at 10:50
  • One more thing, Now I receive multiple notifications but only one opens, the others do not do anything. Which pendingIntent flag should I use to force each notification to start a new instance of the target activity? – Pacemaker Jul 23 '14 at 10:51

3 Answers3

7

just make sure you are using unique id, as a work around you can use for loop integer i value instead of notification.getInt("id").

eg:

for(int i = 1; i < 10; i++)
{
  notificationManager.notify(i, n);
}

or you can save an integer value in SharedPrefrence and increment it regularly in for loop and using the same as id.

androidStud
  • 532
  • 4
  • 9
  • Hi I think you should use different id for each notification .. and use loops for sending multiple notifications.. – Sivakumar Jul 23 '14 at 10:27
1

You have to make a new intent, just add the following piece of code:

intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));

You need to setFlag in notification also:

n.flags += Notification.FLAG_ONGOING_EVENT;
n.flags += Notification.FLAG_NO_CLEAR;
notificationManager.notify(notificationId, n);

Please refer the link : Android: Managing Multiple Notifications

Community
  • 1
  • 1
0

Please Check the Pending Intent request code, that should be unique for each cycle of loop.

Siddesh Kumar
  • 59
  • 1
  • 9