Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

android - 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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...