能否通过编程方式清除通知?
我尝试使用NotificationManager
,但它无法工作。是否有其他方法可以做到这一点?
能否通过编程方式清除通知?
我尝试使用NotificationManager
,但它无法工作。是否有其他方法可以做到这一点?
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
setContentText
设置的文本),尽管我将优先级设置为2。有什么建议吗? - IqbalNotificationManagerCompat.from(this).notify(NOTIFICATION_ID,...)
。 作为notify
方法的第一个参数传递的NOTIFICATION_ID
与你应该传递给notificationManager.cancel
的NOTIFICATION_ID
相同。 - Donald Duck来自: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
为了在用户从通知窗口中选择通知时清除状态栏通知,请将 "FLAG_AUTO_CANCEL" 标志添加到您的 Notification 对象。您还可以使用 cancel(int) 手动清除它,传递通知 ID,或使用 cancelAll() 清除所有通知。
但唐纳德是正确的,您只能清除自己创建的通知。
由于没有人发布过代码答案:
notification.flags = Notification.FLAG_AUTO_CANCEL;
如果您已经有了标志,您可以像这样使用OR FLAG_AUTO_CANCEL:
notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
notifcation.flags |= Notification.FLAG_AUTO_CANCEL;
的意思是设置通知标志位中的自动取消属性。 - Sebastian NowakNotificationManagerCompat.from(context).cancelAll();
要删除特定的通知,NotificationManagerCompat.from(context).cancel(notificationId);
cancel
方法不存在的困扰,那么 cancel
方法不是静态的,因此您需要一个 NotificationManager
实例,像这样:NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
然后可以像 Rohit Suthar 在他的答案中引用的那样调用 notificationManager.cancel(notificationId);
。
notificationId
简单地是您传递给 notificationManager.notify(notificationId, mNotification)
的 ID。 - Mira_Cole从API 18(Jellybean MR2)开始,您可以通过NotificationListenerService取消除自己以外的通知。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class MyNotificationListenerService extends NotificationListenerService {...}
...
private void clearNotificationExample(StatusBarNotification sbn) {
myNotificationListenerService.cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
如果您正在从在前台启动的服务中生成通知,可使用以下方法:
startForeground(NOTIFICATION_ID, notificationBuilder.build());
notificationManager.cancel(NOTIFICATION_ID);
如果您不想取消通知,而且该通知仍然出现在状态栏中。在这种情况下,您需要发出
stopForeground( true );
从服务中将其放回到后台模式,并同时取消通知。或者,您可以将其推入后台而不使其取消通知,然后取消通知。
stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);
Notification mNotification = new Notification.Builder(this)
.setContentTitle("A message from: " + fromUser)
.setContentText(msg)
.setAutoCancel(true)
.setSmallIcon(R.drawable.app_icon)
.setContentIntent(pIntent)
.build();
.setAutoCancel(true)
当你点击通知时,打开相应的活动并从通知栏中删除通知。
我认为最新的AndroidX和向后兼容性方面的更新是必要的。使用Kotlin和Java来实现这个最好的方式应该是:
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
取消所有通知的方法是:
NotificationManagerCompat.from(context).cancelAll()
NotificationCompat.Builder
(android.support.v4
的一部分),那么只需调用其对象的setAutoCancel
方法即可。NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);
setAutoCancel()
对他们无效,所以您也可以尝试这种方式。builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
请注意,getNotification()
方法已经被弃用!!!
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
builder.setSound(soundUri);
builder.setAutoCancel(true);
如果您正在使用通知构建器,则此方法有效...