Android:从通知栏中删除通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3595232/
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 10:54:08  来源:igfitidea点击:

Android: remove notification from notification bar

androidnotifications

提问by Nezir

I have created an application and with an event I manage to add notification in android notification bar. Now I need sample how to remove that notification from notification bar on an event ??

我创建了一个应用程序,并通过一个事件我设法在 android 通知栏中添加通知。现在我需要示例如何从事件的通知栏中删除该通知?

回答by Ankit Patial

You can try this quick code

你可以试试这个快速代码

public static void cancelNotification(Context ctx, int notifyId) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
    nMgr.cancel(notifyId);
}

回答by RoflcoptrException

This is quite simple. You have to call cancelor cancelAllon your NotificationManager. The parameter of the cancel method is the ID of the notification that should be canceled.

这很简单。您必须调用cancelcancelAll在您的 NotificationManager 上。cancel 方法的参数是应该取消的通知的 ID。

See the API: http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

请参阅 API:http: //developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

回答by Stephane Mathis

You can also call cancelAllon the notification manager, so you don't even have to worry about the notification ids.

您还可以调用cancelAll通知管理器,因此您甚至不必担心通知 ID。

NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();

EDIT : I was downvoted so maybe I should specify that this will only remove the notification from your application.

编辑:我被否决了,所以也许我应该指定这只会从您的应用程序中删除通知。

回答by farhad.kargaran

simply set setAutoCancel(True) like the following code:

只需像以下代码一样设置 setAutoCancel(True) :

Intent resultIntent = new Intent(GameLevelsActivity.this, NotificationReceiverActivityAdv.class);

PendingIntent resultPendingIntent =
        PendingIntent.getActivity(
                GameLevelsActivity.this,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT
                );

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        getApplicationContext()).setSmallIcon(R.drawable.icon)
        .setContentTitle(adv_title)
        .setContentText(adv_desc)
        .setContentIntent(resultPendingIntent)
         //HERE IS WHAT YOY NEED:
        .setAutoCancel(true);

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(547, mBuilder.build());`

回答by pilotmario

this will help:

这将有助于:

NotificationManager mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();

this should remove all notifications made by the app

这应该删除应用程序发出的所有通知

and if you create a notification by calling

如果您通过调用创建通知

startForeground();

inside a Service.you may have to call

在 Service.you 内,您可能需要调用

stopForeground(false);

first,then cancel the notification.

首先,然后取消通知。

回答by Dhruv Raval

If you are generating Notification from a Servicethat is started in the foregroundusing

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

startForeground(NOTIFICATION_ID, notificationBuilder.build());

Then issuing

然后发行

notificationManager.cancel(NOTIFICATION_ID);

does't work canceling the Notification & notification still appears in the status bar. In this particular case, you will solve these by 2 ways:

取消通知不起作用,通知仍然出现在状态栏中。在这种特殊情况下,您将通过两种方式解决这些问题:

1> Using stopForeground( false ) inside service:

1> 在服务内部使用 stopForeground( false ):

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

2> Destroy that service class with calling activity:

2> 使用调用活动销毁该服务类:

Intent i = new Intent(context, Service.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if(ServiceCallingActivity.activity != null) {
    ServiceCallingActivity.activity.finish();
}
context.stopService(i);

Second way prefer in music player notificationmore because thay way not only notification remove but remove player also...!!

第二种方式更喜欢音乐播放器通知,因为这种方式不仅可以删除通知,还可以删除播放器......!!

回答by Akansha patel

Please try this,

请试试这个,

public void removeNotification(Context context, int notificationId) {      
    NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notificationId);
}

回答by gil.fernandes

NotificationManager.cancel(id)is the correct answer. Yet you can remove in Android Oreoand later notifications by deleting the whole notification channel. This should delete all messages in the deleted channel.

NotificationManager.cancel(id)是正确答案。然而,您可以通过删除整个通知渠道来删除Android Oreo和更高版本的通知。这应该删除已删除频道中的所有消息。

Here is the example from the Android documentation:

以下是Android 文档中的示例:

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);

回答by Muhammad Usman Ghani

Use the NotificationManager to cancel your notification. You only need to provide your notification id

使用 NotificationManager 取消您的通知。您只需要提供您的通知 ID

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

also check this link See Developer Link

还检查此链接 请参阅开发人员链接

回答by Szabolcs Becze

On Android API >=23 you can do somehting like this to remove a group of notifications.

在 Android API >=23 上,您可以执行这样的操作来删除一组通知。

  for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
        if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
            mNotificationManager.cancel(statusBarNotification.getId());
        }
    }