如何清除Android中的通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2665634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:50:29  来源:igfitidea点击:

How to clear a notification in Android

androidnotifications

提问by rahul

Is it possible to clear a notification programatically?

是否可以以编程方式清除通知?

I tried it with the NotificationManagerbut its not working. Is there any other way I can do it?

我试过它,NotificationManager但它不工作。我还有其他方法可以做到吗?

回答by Janusz

Use the following code to cancel a Notification:

使用以下代码取消通知:

NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

In this code there is alway the same id used for notifications. If you have different notifications that need to be canceled you have to save the ids that you used to create the Notification.

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

回答by Chuck C.

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

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

To clear the status bar notification when the user selects it from the Notifications window, add the "FLAG_AUTO_CANCEL" flag to your Notification object. You can also clear it manually with cancel(int), passing it the notification ID, or clear all your Notifications with cancelAll().

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

But Donal is right, you can only clear notifications that you created.

但是 Donal 是对的,您只能清除您创建的通知。

回答by NPike

Since no one has posted a code answer to this:

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

notification.flags = Notification.FLAG_AUTO_CANCEL;

.. and if you already have flags, you can OR FLAG_AUTO_CANCEL like this:

.. 如果你已经有了标志,你可以像这样 OR FLAG_AUTO_CANCEL:

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

回答by Rohit Suthar

Please try default method provided into NotificationManager.

请尝试提供到NotificationManager 中的默认方法。

NotificationManager.cancelAll()to remove all notification. NotificationManager.cancel(notificationId)to remove particular notification.

NotificationManager.cancelAll()删除所有通知。 NotificationManager.cancel(notificationId)删除特定通知。

回答by Jim Vitek

Starting with API level 18 (Jellybean MR2) you can cancel Notifications other than your own via NotificationListenerService.

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

回答by Neeraj Singh

 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)

.setAutoCancel(true)

when you click on notification, open corresponding activity and remove notification from notification bar

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

回答by sandalone

If you're using NotificationCompat.Builder(a part of android.support.v4) then simply call its object's method setAutoCancel

如果您正在使用NotificationCompat.Builder(的一部分android.support.v4),则只需调用其对象的方法setAutoCancel

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setAutoCancel(true);

Some guys were reporting that setAutoCancel()did not work for them, so you may try this way as well

有些人报告说这setAutoCancel()对他们不起作用,所以你也可以试试这种方式

builder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;

Note that the method getNotification()has been deprecated!!!

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

回答by Ian Gough

If you are generating Notification from a Service that is started in the foreground using

如果您正在使用在前台启动的服务生成通知

startForeground(NOTIFICATION_ID, notificationBuilder.build());

Then issuing

然后发行

notificationManager.cancel(NOTIFICATION_ID);

does not end up canceling the Notification, and the notification still appears in the status bar. In this particular case, you will need to issue

最终不会取消通知,通知仍然出现在状态栏中。在这种特殊情况下,您需要发出

stopForeground( true );

from within the service to put it back into background mode and to simultaneously cancel the notifications. Alternately, you can push it into the background without having it cancel the notification and then cancel the notification.

从服务内部将其恢复到后台模式并同时取消通知。或者,您可以将其推送到后台,而无需取消通知,然后再取消通知。

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

回答by Dhina k

   String ns = Context.NOTIFICATION_SERVICE;
  NotificationManager Nmang = (NotificationManager) getApplicationContext()
                                                     .getSystemService(ns);
  Nmang .cancel(getIntent().getExtras().getInt("notificationID"));

for more reference click here http://androiddhina.blogspot.in/2015/01/how-to-clear-notification-in-android.html

更多参考点击这里http://androiddhina.blogspot.in/2015/01/how-to-clear-notification-in-android.html

回答by Alejandro Casanova

Actually as answered before starting with API Level 18 you can cancel Notifications posted by other apps differet than your own using NotificationListenerService but that approach will no longer work on Lollipop, here is the way to remove notifications covering also Lillipop API.

实际上,在从 API 级别 18 开始之前,您可以使用 NotificationListenerService 取消与您自己的应用程序不同的其他应用程序发布的通知,但这种方法将不再适用于 Lollipop,这里是删除也涵盖 Lillipop API 的通知的方法。

if (Build.VERSION.SDK_INT < 21) {
    cancelNotification(sbn.getPackageName(), sbn.getTag(), sbn.getId());
}
else {
    cancelNotification(sbn.getKey());
}