【发布时间】:2011-04-05 10:41:31
【问题描述】:
我创建了一个应用程序,并通过一个事件设法在 android 通知栏中添加通知。现在我需要示例如何从事件的通知栏中删除该通知?
【问题讨论】:
标签: android notifications
我创建了一个应用程序,并通过一个事件设法在 android 通知栏中添加通知。现在我需要示例如何从事件的通知栏中删除该通知?
【问题讨论】:
标签: android notifications
它会帮助你
private var notificationManager = NotificationManagerCompat.from(this)
notificationManager.cancel("your_notification_id")
如果你这样启动通知前台
startForeground("your_notification_id",notification.build())
那么你也应该停止前台服务
notificationManager.cancel("your_notification_id")
stopForeground(true)
【讨论】:
一个简短的Liner是:
NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
或者取消所有通知是:
NotificationManagerCompat.from(context).cancelAll()
专为 AndroidX 或支持库设计。
【讨论】:
这会有所帮助:
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
这应该会删除应用发出的所有通知
如果您通过调用创建通知
startForeground();
在服务中。您可能需要调用
stopForeground(false);
先取消通知。
【讨论】:
只需调用 ID:
public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
【讨论】:
请试试这个,
public void removeNotification(Context context, int notificationId) {
NotificationManager nMgr = (NotificationManager) context.getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(notificationId);
}
【讨论】:
NotificationManager.cancel(id) 是正确答案。然而,您可以通过删除整个通知渠道来删除 Android Oreo 和更高版本的通知。这应该会删除已删除频道中的所有消息。
这是来自Android documentation的示例:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
【讨论】:
在 Android API >=23 上,您可以执行类似这样的操作来删除一组通知。
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}
【讨论】:
只需像以下代码一样设置 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());`
【讨论】:
如果您正在生成通知,该服务在前台使用
启动startForeground(NOTIFICATION_ID, notificationBuilder.build());
然后发行
notificationManager.cancel(NOTIFICATION_ID);
取消通知不起作用,通知仍然出现在状态栏中。在这种特殊情况下,您将通过 2 种方式解决这些问题:
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);
第二种方式更喜欢音乐播放器通知,因为这种方式不仅可以删除通知,还可以删除播放器...!!
【讨论】:
stopForeground(true); manager.cancelAll(); 为我解决了这个问题!
您也可以在通知管理器上拨打cancelAll,这样您甚至不必担心通知ID。
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
编辑:我被否决了,所以也许我应该指定这只会从您的应用程序中删除通知。
【讨论】:
startForeground(NOTIFICATION_ID, mNotification);
这很简单。您必须在 NotificationManager 上调用 cancel 或 cancelAll。 cancel 方法的参数是应该取消的通知的 ID。
查看 API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
【讨论】:
你可以试试这个快速代码
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
【讨论】:
使用 NotificationManager 取消通知。您只需要提供您的通知 ID
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
也检查这个链接 See Developer Link
【讨论】: