Notification is not being dismissed (Android)

送分小仙女□ 提交于 2019-12-01 18:22:43

I faced this problem today, and found that FLAG_AUTO_CANCEL and setAutoCanel(true), both work when click on notification, but not work for action click

so simply, in the target service or activity of action, cancel the notification

NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancelAll();

or if have more notification

manager.cancel(notificationId);

You have created two pending intent use in boths and change Flag too.

  PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

  PendingIntent phoneCallIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);

// CHANGE TO THIS LINE

   PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

   PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);

use this flag:

Notification.FLAG_AUTO_CANCEL inside this:

int flags = Notification.FLAG_AUTO_CANCEL;
Notification notification = builder.build();
        notification.flags = flags;

Documentation

Ok turns out it's a known problem already, and it needs extra code to be done (keeping reference to notification through id). Have no clue why API does not provide this, as it seems very logical to do. But anyways,

see this answer in stackoverflow:

When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:

notify(int id, Notification notification)

To cancel, you would call:

cancel(int id)

with the same id. So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!