Android:从通知栏删除通知

发布于 2024-09-16 07:17:21 字数 73 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

能怎样 2024-09-23 07:17:21

您可以尝试这个快速代码

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

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);
}
遗弃M 2024-09-23 07:17:21

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

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

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

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

书信已泛黄 2024-09-23 07:17:21

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

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

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

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

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.

鲜肉鲜肉永远不皱 2024-09-23 07:17:21

这会有所帮助:

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

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

如果您通过调用创建通知

startForeground();

在服务内部。您可能需要调用

stopForeground(false);

先取消通知。

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

stopForeground(false);

first,then cancel the notification.

近箐 2024-09-23 07:17:21

只需像下面的代码一样设置 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());`

simply set setAutoCancel(True) like the following code:

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());`
音栖息无 2024-09-23 07:17:21

如果您要从在前台启动的服务生成通知

startForeground(NOTIFICATION_ID, notificationBuilder.build());

notificationManager.cancel(NOTIFICATION_ID);

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

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

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

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

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

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'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:

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

2> Destroy that service class with calling activity:

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 notification more because thay way not only notification remove but remove player also...!!

动听の歌 2024-09-23 07:17:21

简短的一句话是:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

或者取消所有通知是:

NotificationManagerCompat.from(context).cancelAll()

为 AndroidX 或支持库制作。

A short one Liner of this is:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

Or to cancel all notifications is:

NotificationManagerCompat.from(context).cancelAll()

Made for AndroidX or Support Libraries.

甜中书 2024-09-23 07:17:21

请尝试这个,

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

Please try this,

public void removeNotification(Context context, int notificationId) {      
    NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    nMgr.cancel(notificationId);
}
如痴如狂 2024-09-23 07:17:21

它将帮助你

private var notificationManager = NotificationManagerCompat.from(this)
notificationManager.cancel("your_notification_id")

如果你像这样启动通知前台

startForeground("your_notification_id",notification.build())

那么你也应该停止前台服务

notificationManager.cancel("your_notification_id")
stopForeground(true)

It will help you

private var notificationManager = NotificationManagerCompat.from(this)
notificationManager.cancel("your_notification_id")

If you start notification foreground like this

startForeground("your_notification_id",notification.build())

Then you should stop foregroundservice also

notificationManager.cancel("your_notification_id")
stopForeground(true)
海拔太高太耀眼 2024-09-23 07:17:21

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

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

另请检查此链接
查看开发者链接

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

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

also check this link
See Developer Link

与酒说心事 2024-09-23 07:17:21

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

以下是 Android 文档中的示例:

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

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

Here is the example from the Android documentation:

NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
临风闻羌笛 2024-09-23 07:17:21

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

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

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

  for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
        if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
            mNotificationManager.cancel(statusBarNotification.getId());
        }
    }
疯狂的代价 2024-09-23 07:17:21

只需拨打ID:

public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}

Just call ID:

public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文