NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
mNotificationManager.deleteNotificationChannel(id);
10
您可以尝试这个快速代码
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
155
在Android API> = 23上,你可以做这样的事情来删除一组通知 .
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}
1
只需拨打ID:
public void delNoti(int id) {((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).cancel(id);}
1
只需像下面的代码一样设置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());`
1
如果要生成 Notification from a Service ,它是在 foreground 中启动的
11 回答
这很简单 . 您必须在NotificationManager上调用
cancel
或cancelAll
. cancel方法的参数是应该取消的通知的ID .查看API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
请试试这个,
您也可以在通知管理器上调用
cancelAll
,这样您甚至不必担心通知ID .编辑:我被downvoted所以也许我应该指定这只会从您的应用程序中删除通知 .
this will help:
and if you create a notification by calling
inside a Service.you may have to call
first,then cancel the notification.
NotificationManager.cancel(id)
是正确答案 . 您可以通过删除整个通知渠道删除 Android Oreo 及更高版本的通知 . 这应删除已删除 Channels 中的所有邮件 .以下是Android documentation中的示例:
您可以尝试这个快速代码
在Android API> = 23上,你可以做这样的事情来删除一组通知 .
只需拨打ID:
只需像下面的代码一样设置setAutoCancel(True):
如果要生成 Notification from a Service ,它是在 foreground 中启动的
然后发出
取消通知并且通知仍然显示在状态栏中 . 在这种特殊情况下,您将通过两种方式解决这些问题:
1> Using stopForeground( false ) inside service:
2> Destroy that service class with calling activity:
第二种方式更喜欢在 music player notification 更多因为不仅通知删除但删除播放器... ...
使用NotificationManager取消通知 . 您只需提供通知ID即可
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
还请查看此链接See Developer Link