如何在android中以编程方式从通知栏中删除通知?

时间:2022-12-05 22:22:38

Anybody have idea how can we remove notification from application programmatically which is called using Pending intent.

任何人都知道我们如何以编程方式从应用程序中删除使用Pending intent调用的通知。

I have used to cancel notification using following method.

我曾经使用以下方法取消通知。

AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(Display.this, TwoAlarmService.class);
PendingIntent pi = PendingIntent.getBroadcast(Display.this, AlarmNumber, intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(pi);

But problem is notification which fired already that are not removed from notification bar.

但问题是已经解雇的通知没有从通知栏中删除。

Thanks in advance...

提前致谢...

如何在android中以编程方式从通知栏中删除通知?

2 个解决方案

#1


120  

Maybe try this :

也许试试这个:

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

OR, you can also do this to cancel all notifications in given context:

或者,您也可以执行此操作以取消给定上下文中的所有通知:

notificationManager.cancelAll();

See this link to the documentation : NotificationManager

请参阅文档链接:NotificationManager

#2


0  

Notifications remain visible until one of the following happens:

在发生以下情况之一之前,通知仍然可见:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared). The user clicks the notification, and you called setAutoCancel() when you created the notification. You call cancel() for a specific notification ID. This method also deletes ongoing notifications. You call cancelAll(), which removes all of the notifications you previously issued. If you set a timeout when creating a notification using setTimeoutAfter(), the system cancels the notification after the specified duration elapses. If required, you can cancel a notification before the specified timeout duration elapse public void cancelNotification() {

用户单独或使用“全部清除”(如果可以清除通知)解除通知。用户单击通知,并在创建通知时调用setAutoCancel()。您可以为特定通知ID调用cancel()。此方法还会删除正在进行的通知。您调用cancelAll(),它会删除您之前发出的所有通知。如果在使用setTimeoutAfter()创建通知时设置超时,则系统会在指定的持续时间过后取消通知。如果需要,您可以在指定的超时持续时间过去之前取消通知public void cancelNotification(){

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
    nMgr.cancel(NOTIFICATION_ID);
}

#1


120  

Maybe try this :

也许试试这个:

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

OR, you can also do this to cancel all notifications in given context:

或者,您也可以执行此操作以取消给定上下文中的所有通知:

notificationManager.cancelAll();

See this link to the documentation : NotificationManager

请参阅文档链接:NotificationManager

#2


0  

Notifications remain visible until one of the following happens:

在发生以下情况之一之前,通知仍然可见:

The user dismisses the notification either individually or by using "Clear All" (if the notification can be cleared). The user clicks the notification, and you called setAutoCancel() when you created the notification. You call cancel() for a specific notification ID. This method also deletes ongoing notifications. You call cancelAll(), which removes all of the notifications you previously issued. If you set a timeout when creating a notification using setTimeoutAfter(), the system cancels the notification after the specified duration elapses. If required, you can cancel a notification before the specified timeout duration elapse public void cancelNotification() {

用户单独或使用“全部清除”(如果可以清除通知)解除通知。用户单击通知,并在创建通知时调用setAutoCancel()。您可以为特定通知ID调用cancel()。此方法还会删除正在进行的通知。您调用cancelAll(),它会删除您之前发出的所有通知。如果在使用setTimeoutAfter()创建通知时设置超时,则系统会在指定的持续时间过后取消通知。如果需要,您可以在指定的超时持续时间过去之前取消通知public void cancelNotification(){

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
    nMgr.cancel(NOTIFICATION_ID);
}