如何在安卓系统中清除通知

111

能否通过编程方式清除通知?

我尝试使用NotificationManager,但它无法工作。是否有其他方法可以做到这一点?


13
可以清除自己的通知,但不能清除别人的。 - Donal Rafferty
1
同意Donal在这段代码的想法,可以参见 http://androidtrainningcenter.blogspot.in/2013/04/removing-status-bar-notification-from.html。 - Tofeeq Ahmad
请参考此答案以了解在API>=18中清除其他通知的方法 - https://dev59.com/fnE85IYBdhLWcg3wqleE#25693210 - Jim Vitek
17个回答

248
使用以下代码取消通知:
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

在这段代码中,通知总是使用相同的ID。如果您有不同的通知需要取消,您必须保存您用于创建通知的ID。

10
我不知道为什么这个回答没有得到更多的赞同和被选为最佳答案。这正是我在寻找的解决方案。谢谢! - loeschg
3
这里应该使用什么通知ID? - Deepak
这确实有效。但是一旦我取消一个通知,所有后续的通知都不会显示通知消息文本(我使用setContentText设置的文本),尽管我将优先级设置为2。有什么建议吗? - Iqbal
如果通知标志设置为Notification.FLAG_NO_CLEAR,则不起作用。 - Anand Savjani
2
对于所有的通知,如果您不知道其ID,可以使用以下代码取消所有通知:if (notificationManager != null) { notificationManager.cancelAll(); } - mehmet
@Deepak 创建通知时,你可能会这样做:NotificationManagerCompat.from(this).notify(NOTIFICATION_ID,...)。 作为notify方法的第一个参数传递的NOTIFICATION_ID与你应该传递给notificationManager.cancelNOTIFICATION_ID相同。 - Donald Duck

44

来自: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

为了在用户从通知窗口中选择通知时清除状态栏通知,请将 "FLAG_AUTO_CANCEL" 标志添加到您的 Notification 对象。您还可以使用 cancel(int) 手动清除它,传递通知 ID,或使用 cancelAll() 清除所有通知。

但唐纳德是正确的,您只能清除自己创建的通知。


12
这不会通过程序取消通知。 - Janusz
5
不确定Janusz在说什么,但是cancel(id)和cancelAll()肯定有效。 - Andrew G

35

由于没有人发布过代码答案:

notification.flags = Notification.FLAG_AUTO_CANCEL;

如果您已经有了标志,您可以像这样使用OR FLAG_AUTO_CANCEL:

notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;

4
这样更好:notifcation.flags |= Notification.FLAG_AUTO_CANCEL; 的意思是设置通知标志位中的自动取消属性。 - Sebastian Nowak
有人能解释一下为什么那样做更好吗? - Prof
@Prof 这是同样功能的更少代码,且不会对性能产生影响。 - Denny
1
@Denny 这假设第一个通知标志已经在上面定义,所以它是两行而不是一行。 - Prof

29
请尝试使用NotificationManagerCompat中提供的方法。 要移除所有通知,
NotificationManagerCompat.from(context).cancelAll();
要删除特定的通知,
NotificationManagerCompat.from(context).cancel(notificationId);

3
如果有人在 NotificationManager 上遇到了 cancel 方法不存在的困扰,那么 cancel 方法不是静态的,因此您需要一个 NotificationManager 实例,像这样:NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 然后可以像 Rohit Suthar 在他的答案中引用的那样调用 notificationManager.cancel(notificationId);notificationId 简单地是您传递给 notificationManager.notify(notificationId, mNotification) 的 ID。 - Mira_Cole

8

从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());
}

7

如果您正在从在前台启动的服务中生成通知,可使用以下方法:

startForeground(NOTIFICATION_ID, notificationBuilder.build());

然后发出
notificationManager.cancel(NOTIFICATION_ID);

如果您不想取消通知,而且该通知仍然出现在状态栏中。在这种情况下,您需要发出

stopForeground( true );

从服务中将其放回到后台模式,并同时取消通知。或者,您可以将其推入后台而不使其取消通知,然后取消通知。

stopForeground( false );
notificationManager.cancel(NOTIFICATION_ID);

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

当你点击通知时,打开相应的活动并从通知栏中删除通知。


6

我认为最新的AndroidX和向后兼容性方面的更新是必要的。使用Kotlin和Java来实现这个最好的方式应该是:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

取消所有通知的方法是:

NotificationManagerCompat.from(context).cancelAll()

适用于AndroidX或支持库。

4
如果您正在使用NotificationCompat.Builderandroid.support.v4的一部分),那么只需调用其对象的setAutoCancel方法即可。
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);

一些人报告说setAutoCancel()对他们无效,所以您也可以尝试这种方式。
builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;

请注意,getNotification()方法已经被弃用!!!


问题是如何通过编程来清除它。当用户单击通知时,setAutoCancel()会将其清除。http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setAutoCancel(boolean) - S Fitz
@SFitz 从问题中并不清楚他想要什么。我理解他想在用户点击通知时清除通知。 - sandalone

2
    // Get a notification builder that's compatible with platform versions
    // >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);
    builder.setSound(soundUri);
    builder.setAutoCancel(true);

如果您正在使用通知构建器,则此方法有效...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接