Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中以编程方式从通知栏中删除通知?_Android_Notifications_Android Notifications_Android Pendingintent - Fatal编程技术网

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

如何在android中以编程方式从通知栏中删除通知?,android,notifications,android-notifications,android-pendingintent,Android,Notifications,Android Notifications,Android Pendingintent,任何人都知道如何以编程方式从应用程序中删除通知,即使用挂起的意图来调用通知 我曾经使用以下方法取消通知 AlarmManager am=(AlarmManager)getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(Display.this, TwoAlarmService.class); PendingIntent pi = PendingIntent.getBroadcast(Display.this, Al

任何人都知道如何以编程方式从应用程序中删除通知,即使用挂起的意图来调用通知

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

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);
但问题是已经触发的通知并没有从通知栏中删除

先谢谢你

也许可以试试这个:

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
或者,您也可以执行此操作以取消给定上下文中的所有通知:

notificationManager.cancelAll();

请参阅此文档链接:

通知将保持可见,直到发生以下情况之一:

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

public void cancelNotification() {

    String ns = NOTIFICATION_SERVICE;
    NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
    nMgr.cancel(NOTIFICATION_ID);
}
所有通知(甚至其他应用程序通知)都可以通过收听中提到的“NotificationListenerService”删除

在服务中,您必须调用
cancelAllNotifications()

必须通过“应用程序和通知”->“特殊应用程序访问”->“通知访问”为您的应用程序启用该服务

添加到清单:

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:label="Test App" android:name="com.test.NotificationListenerEx" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>
之后,使用广播接收器触发全部清除

根据上述代码触发广播使用

getContext().sendBroadcast(new Intent("com.test.app"));

我认为最新和更新的方法(Kotlin和Java)应该是:

NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID)
要取消所有通知,请执行以下操作:

NotificationManagerCompat.from(context).cancelAll()
专为AndroidX或支持库设计。

适用于使用(通过子类)
服务的用户,并使用以下方法运行该服务:

final Intent serviceentent=新意图(上下文,YourNotificationService.class)
ContextCompat.startForegroundService(上下文,serviceIntent)
您可以这样关闭它:

context.stopService(新意图(context,YourNotificationService.class))

您好,我不知道是否可以通过编程方式删除通知,但您可以使用新通知覆盖旧通知,这是肯定的。如果它满足您的要求,那么我可以在这里为您发布代码。plz replyNo,实际上我有两个不同的通知,我已经取消了通知。如果(Context.notification\u SERVICE!=null)
,我看不出有什么意义,因为这是一个字符串常量,永远不会为null。你是对的,这个测试没有用,我不知道为什么我这么做(删除了它):/@Matthieu它在4年前就开始工作了…:)现在没有检查。如果它在任何情况下都不起作用,请告诉我。@JayeshSojitra我只是好奇
cancel(NOTIFICATION\u ID)
cancelAll()
中的哪一个起了作用。如果你的记忆还记得那么久,那没什么大不了的:)@Matthieu在我的情况下都起作用了。如果您想取消特定的一个,请使用cancel(通知\ ID),如果您想取消所有通知,请使用cancelAll()。我不知道您为什么在已被接受的情况下发布此答案。您是否有任何上述问题不起作用的特定情况?上述情况不适用于所有通知(所有“应用”通知). 仅删除当前应用程序创建的通知。如何触发?发送哪个广播?你能不能也张贴如何触发它?根据上面的代码,它将是;getContext().sendBroadcast(新意图(“com.test.app”);
NotificationManagerCompat.from(context).cancelAll()