I have a Notification which is refreshed (i.e. sent) every three seconds. I’ve set the FLAG_ONGOING_EVENT flag and the FLAG_NO_CLEAR flag so that is always shown. The problem is, that if e.g. a download is active (which displays a progress bar in the notification area) both notifications constantly switch positions as they are both refreshed every few seconds.
How can I pin my notification to the top of the list (or to some static position), so that it stops jumping around every time I update it by calling NotificationManager.notify()?
Edit: Here’s the code to update the notification. It’s run every three seconds.
Notification notification = new Notification();
notification.contentView = appBarNotification; // this sets the changed notification content
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.icon = R.drawable.icon;
nm.notify(APP_BAR_NOTIFICATION, notification);
There is a solution that does exactly what you want.
In the Notification class, there is a public field called when. According to the API:
The default behaviour (for code and coders alike 🙂 is to give when the timestamp of the notification:
So – in order to maintain the correct order of notifications, all you need to do is give it a preset timestamp instead:
When you do this with all of your notifications, they maintain their order. I had the same problem as you and solved it this way.