Android:从通知栏中删除通知

时间:2010-08-29 15:01:49

标签: android notifications

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

12 个答案:

答案 0 :(得分:205)

您可以尝试使用此快速代码

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

答案 1 :(得分:162)

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

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

答案 2 :(得分:60)

您也可以在通知管理器上拨打cancelAll,这样您甚至不必担心通知ID。

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

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

答案 3 :(得分:10)

只需设置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());`

答案 4 :(得分:6)

如果您使用

生成前景中启动的来自服务的通知
startForeground(NOTIFICATION_ID, notificationBuilder.build());

然后发出

notificationManager.cancel(NOTIFICATION_ID);

取消通知&通知仍会显示在状态栏中。在这种特殊情况下,您将通过两种方式解决这些问题:

<强> 1&GT;在服务中使用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);

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

答案 5 :(得分:5)

这会有所帮助:

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

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

如果您通过致电

创建通知
startForeground();
服务中的

。您可能需要致电

stopForeground(false);
首先

,然后取消通知。

答案 6 :(得分:2)

其中一个简短的衬里是:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)

或者取消所有通知是:

NotificationManagerCompat.from(context).cancelAll()

适用于AndroidX或支持库。

答案 7 :(得分:1)

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

mNotificationManager.cancel(YOUR_NOTIFICATION_ID);

也请查看此链接 See Developer Link

答案 8 :(得分:1)

在Android API&gt; = 23上,你可以像这样删除一组通知。

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

答案 9 :(得分:1)

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

答案 10 :(得分:1)

请试试这个,

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {

        Platform.exit();

        Thread start = new Thread(new Runnable() {
            @Override
            public void run() {
                //TODO Auto-generated method stub
                system.exit(0);     
            }
        });

        start.start();
    }
});

答案 11 :(得分:0)

只需致电ID:

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