Android:从通知栏中删除通知
这很简单。您必须在NotificationManager上拨打cancel
或cancelAll
。取消方法的参数是应该取消通知的ID。
见API:http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int)
谢谢Roflcoptr :) 我在这里找到它:http://*.com/questions/2839727/remove-the-notification-icon-from-the-status-bar – Nezir 2010-08-29 15:09:12
private static final int MY_NOTIFICATION_ ID = 1234; String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager; mNotificationManager =(NotificationManager)getSystemService(ns); mNotificationManager.notify(MY_NOTIFICATION_ID,notification); 示例代码不完整。这取决于你如何创建你的通知。只要确保您使用相同的ID来取消您在创建通知时使用的通知。 取消: mNotificationManager.cancel(MY_NOTIFICATION_ID); – Nezir 2010-08-29 15:19:05
你节省了一天!,很高兴看到像你这样的人,即使他们不是复杂问题的复杂答案,像你这样的人在困难时期帮助我们很多。非常感谢你。 – 2016-04-30 23:50:33
你可以试试这个简单的代码
public static void cancelNotification(Context ctx, int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancel(notifyId);
}
这应该是正确的答案,因为它很简单,正确并直接适用于这一点。 – ZeroTek 2013-07-11 10:42:07
嗨,这是正确的答案。但通知托盘仍然可见。有什么方法可以隐藏它吗?例如,我有两个与通知相关的操作:取消和完成。当我采取这些行动时,我将删除通知。但是,这样做只会删除通知,但通知托盘保持可见状态。 – 2014-11-14 21:26:14
使用NotificationManager取消通知。您只需提供您的通知ID
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);
也检查此链接 See Developer Link
您也可以拨打cancelAll
在通知管理器,所以你甚至不必担心通知的ID。
NotificationManager notifManager= (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.cancelAll();
编辑:我downvoted所以也许我应该指定,这只会从您的应用程序中删除通知。
只需设置setAutoCancel(真),如下面的代码:
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(假)内服务:
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);
第二种方式喜欢音乐播放器通知更因为不仅通知删除而且删除p层也... !!
感谢您的回答!使用'stopForeground(true); manager.cancelAll();'是为我解决了它! – Sky 2016-12-12 04:54:46
这将帮助:
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cannelAll();
,如果你通过调用
startForeground();
一个Service.you内创建一个通知可能需要调用
stopForeground(false);
先取消通知。
在Android API> = 23上,您可以这样做来删除一组通知。
for (StatusBarNotification statusBarNotification : mNotificationManager.getActiveNotifications()) {
if (KEY_MESSAGE_GROUP.equals(statusBarNotification.getGroupKey())) {
mNotificationManager.cancel(statusBarNotification.getId());
}
}
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);
的[从状态栏中的删除通知图标]可能重复(http://*.com/questions/2839727/remove-the-notification- icon-from-the-status-bar) – rds 2013-02-21 15:10:18
代码片段请参阅[删除通知](http://androidtrainningcenter.blogspot.in/2013/04/removing-status-bar-notification-from.html) – Sameer 2013-04-21 07:06:47