简体   繁体   English

使用编程自动清除纸盘中的通知(不起作用)

[英]Automatically clear notification from tray using programming (not working)

I want to clear the notification tray programmatically. 我想以编程方式清除通知托盘。 I am using a service and I want to do that ondestroy method: 我正在使用服务,并且想使用该ondestroy方法:

    protected void dismissProgressNotification() {
    NotificationManager manager = (NotificationManager) 
    getSystemService(Context.NOTIFICATION_SERVICE);

    manager.cancel(PROGRESS_NOTIFICATION_ID);
}

   `  @Override
      public void onDestroy() {
      super.onDestroy();
      uploadTask.cancel();
      dismissProgressNotification()` 

Here is my code but it did not work when I stop the service. 这是我的代码,但是当我停止服务时它不起作用。

This should work. 这应该工作。

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

Try to use the application context, also you can test canceling all the the notifications 尝试使用应用程序上下文,也可以测试取消所有通知

notificationManager.cancelAll();

Check the docs here: https://developer.android.com/reference/android/app/NotificationManager.html#cancel(int) 在此处检查文档: https : //developer.android.com/reference/android/app/NotificationManager.html#cancel(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM