android - 将通知固定到通知区域的顶部

标签 android notifications

我有一个每三秒刷新一次(即发送)的通知。我设置了 FLAG_ONGOING_EVENT 标志和 FLAG_NO_CLEAR 标志,以便始终显示。问题是,如果例如下载处于 Activity 状态(在通知区域显示进度条)两个通知不断切换位置,因为它们都每隔几秒刷新一次。

如何将我的通知固定到列表顶部(或某个静态位置),以便它在每次我通过调用 NotificationManager.notify() 更新它时停止跳转?

编辑:这是更新通知的代码。它每三秒运行一次。

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);

最佳答案

有一个解决方案可以完全满足您的需求。

Notification类中,有一个名为 when 的公共(public)字段.根据 API:

The timestamp for the notification. The icons and expanded views are sorted by this key.

默认行为(对于代码和编码人员来说都是一样的:) 是何时通知的时间戳:

notification.when = System.currentTimeMillis();

所以 - 为了保持正确的通知顺序,您需要做的就是给它一个预设的时间戳:

notification.when = previousTimestamp;

当您对所有通知执行此操作时,它们会保持其顺序。我遇到了和你一样的问题,并通过这种方式解决了。

关于android - 将通知固定到通知区域的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6189817/

相关文章:

android - 在外部单击时保持通知区域打开

objective-c - 如何让 NSUserNotification 超时?

javascript - 最简单的 "red notification badge"CSS 计数

java - RealmResults 查询返回部分结果 - Android

android - 设置键盘坐标 : How to set the keyboard x, y 坐标

android - 布局 setMargins 在横向模式下不起作用

linux - 如何知道文件何时添加到目录?

安卓谷歌地图

java - 获取解雇通知 ID

java - Facebook 之类的通知或 ajax 调用?