将通知固定到通知区域的顶部 [英] Pin Notification to top of notification area
问题描述
我有一个每三秒刷新(即发送)一次的通知.我已经设置了 FLAG_ONGOING_EVENT 标志和 FLAG_NO_CLEAR 标志,以便始终显示.问题是,如果例如下载处于活动状态(在通知区域显示进度条)两个通知不断切换位置,因为它们都每隔几秒钟刷新一次.
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.
如何将我的通知固定到列表的顶部(或某个静态位置),以便在我每次通过调用 NotificationManager.notify()
更新它时停止跳动?
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()
?
编辑:这是更新通知的代码.每三秒运行一次.
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.
在Notification 类中,有一个名为何时.根据 API:
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.
默认行为(对于代码和编码人员来说:)是给 when 通知的时间戳:
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.
这篇关于将通知固定到通知区域的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!