Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pin Notification to top of notification area

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);
like image 560
ubuntudroid Avatar asked May 31 '11 15:05

ubuntudroid


People also ask

How do I pin a notification to the top?

Swipe left on any notifications, and tap the "Pin" icon to keep the messages at the top of the notification bar. Note: When more than one notification are pinned on top in the notification bar, they are ordered by the time they are pinned. The newer the notification is, the higher it is pinned.

How do I pin notifications on Android notification bar?

You can swipe right to dismiss a notification, swipe left to snooze (Figure D), long press to either pin to top or rearrange, or tap the yellow circle to dismiss all notifications.

Can you pin notification?

Pin Notifications in Your Phone AppTo pin it, you need to click on the menu button with three horizontal dots, and select Pin Notification. This will re-arrange the list, and move it above the other entries.


1 Answers

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 timestamp for the notification. The icons and expanded views are sorted by this key.

The default behaviour (for code and coders alike :) is to give when the timestamp of the notification:

notification.when = System.currentTimeMillis();

So - in order to maintain the correct order of notifications, all you need to do is give it a preset timestamp instead:

notification.when = previousTimestamp;

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.

like image 165
Vaiden Avatar answered Nov 11 '22 08:11

Vaiden