【发布时间】:2011-04-07 12:59:06
【问题描述】:
我做了一个应用,可以在安卓手机的下拉状态栏中设置通知。但是,我的代码中有一个错误(有时通知已设置,有时未设置)。如果通知对用户可见,我希望能够检查(在代码中)。 (即用户可以在状态栏中看到通知吗?)。
我该怎么做? (提前致谢)。
非常感谢示例代码。
【问题讨论】:
标签: android
我做了一个应用,可以在安卓手机的下拉状态栏中设置通知。但是,我的代码中有一个错误(有时通知已设置,有时未设置)。如果通知对用户可见,我希望能够检查(在代码中)。 (即用户可以在状态栏中看到通知吗?)。
我该怎么做? (提前致谢)。
非常感谢示例代码。
【问题讨论】:
标签: android
我希望能够检查(在代码中)通知是否对用户可见。 (即用户可以看到 状态栏中的通知?)。
我该怎么做?
你不能,抱歉。更新:现在可以使用 Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
但是,您始终可以简单地 cancel() 它 - 取消不在屏幕上的 Notification 非常好。相反,您始终可以安全地再次调用notify() 来调用相同的Notification,如果Notification 已经出现在屏幕上也不会造成问题。
编辑:
如果您不想使用NotificationListenerService,则在 API 23 中添加了NotificationManager.getActiveNotifications()
【讨论】:
NotificationListenerService(这需要监听通知的权限)。
只是将所有内容放在一起。这就是它的工作原理
要构建通知,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
要发出通知声音呼叫setSound() 的通知,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
要在用户选择并启动接收者Intent后取消通知,请致电setAutoCancel(),
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
要为特定通知只发出声音/振动一次,请使用Notification.FLAG_ONLY_ALERT_ONCE。使用此标志,您的通知只会发出一次声音,直到它被取消,您可以使用通知 ID 多次调用 notify()。请注意,如果您调用 cancel() 或者如果用户取消通知或自动取消,则 notify() 调用将使通知再次发出声音。
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
最后把通知放到通知面板上,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
请注意,如果您想有效地使用通知,这里的notification_id 很重要。(为通知保持单一声音/振动或取消特定通知)。
要取消特定通知,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
即使通知不存在,您也可以cancel() 发送通知,或者您可以使用相同的 ID 多次调用 notify()。请注意,使用不同的 id 调用 notify 将创建新的通知。
因此,无论通知是否存在,如果您再次调用 notify() 并设置正确的 notification_id 并设置 Notification.FLAG_ONLY_ALERT_ONCE 标志,您可以保持通知处于活动状态,而不会因重复的声音而打扰用户。
【讨论】:
.setOnlyAlertOnce(true) 用于 NotificationCompat
您需要为您发出的每个通知设置一个 id。
所以你发出通知..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
清除它..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
并检查是否处于活动状态..(如果没有可用的待处理意图,existAlarm 返回 null)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
所以一切都归结为为每个通知初始化一个 ID 以及如何使其唯一。
【讨论】:
API 23 中的 NotificationManager 类引入了一个新方法:
public StatusBarNotification[] getActiveNotifications()
【讨论】:
有一个标志。
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
...如果您希望每次发送通知时都播放声音和/或振动,则应设置,即使在此之前尚未取消。
虽然再次发送通知时会闪烁,但不会有任何声音或振动。
【讨论】:
现在可以在 android 4.3 以上检查未完成的通知
看这里:
【讨论】:
seems 从 Android M (API 23) 中获取您的进程是可能的,而无需使用 NotificationListenerService 也不需要额外的权限:
notificationManager.getActiveNotifications()
【讨论】:
从 Android Marshmallow (API 23) 开始,您可以恢复应用发布的活动通知列表。这个 NotificationManager 方法是getActiveNotifications()。更多信息在这里:https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()
【讨论】: