Java – Android multiple notifications show only the last one

Android multiple notifications show only the last one… here is a solution to the problem.

Android multiple notifications show only the last one

I’m creating a notification system that pushes multiple notifications via a FOR loop.
Logcat shows that I’m creating multiple notifications (my phone is even sending two corresponding ringtones). But the notification bar only shows the last one.
I read the documentation and it says that calling NotificationManager.notify requires a unique ID, and if this ID exists, then the notification will be replaced with a new one. I used a unique GUID for each notification, but it still didn’t work.

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

Do I have any suggestions to use? Could it be the PendingIntent part of the notification?

Thanks in advance.

Solution

Just make sure you’re using a unique ID, and as a workaround, you can use the for loop integer i value instead of notification.getInt(“id”).

For example:

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

Or you can save an integer value in SharedPrefrence and increment it periodically in a for loop and use the same value as id.

Related Problems and Solutions