I’ve got an for block which looks like this:
for(int counter = 0; counter < sList.size(); counter++){
String s = sList.get(counter);
Notification notification = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText(s).setSmallIcon(R.drawable.ic_launcher).setContentIntent(pendingIntent).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(counter, notification);
}
This block is located in a service which is triggered by an alarmmanager. So this block may really well be executed a couple of times before the user sees the notifications.
When this block is re-executed when something has been added to the sList, it overrides the current notifications, because the ID’s of the notification are the same.
How can I prevent that from happening? How can I get a unique ID every time? Or is there maybe possible to avoid the whole ID part, like telling android that is has to show the notification anyway, no matter what the ID is?
Thanks in advance!
I’m sure you shouldn’t have so much of notifications for user at once. You should show a single notification that consolidates info about group of events like for example Gmail client does. Use
Notification.Builderfor that purpose.If you still need to have a lot of status bar notifications, you should save the last value of your counter somewhere and use for loop like this:
But as I have said I think it is bad solution that leads to bad user experience from your app.