'How to dismiss notification since Android 12?
My app creates a notification with two actions the user can choose. After choosing either of the actions, I want the notification to be automatically dismissed. Until now I achieved that by having this at the end of onReceive from my BroadcastReceiver:
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
NotificationManagerCompat.from(context.getApplicationContext()).cancelAll();
After updating to Android 12, the notifications are not getting closed anymore. Instead, I'm having this error:
java.lang.SecurityException: Permission Denial: android.intent.action.CLOSE_SYSTEM_DIALOGS broadcast from (...) requires android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS.. Cause: android.os.RemoteException
Adding that permission had no effect though. Googling about it I found that ACTION_CLOSE_SYSTEM_DIALOGS is being deprecated, and the only official alternative seems to be an AccessibilityService, although I didn't find any example of that. So my question is: what is the best way to close a notification now? If it is indeed through AccessibilityService, how can it be done?
EDIT: Now I realized that I was doing 2 different things in my original code: Closing the notification panel, and dismissing the notifications. The last part (cancelAll) was enough for my needs, and it also closes the panel when there is no other notification left (from other apps). However, it's important to note for future reference that it's still impossible to close the panel when there is another notification left in the tray in the same way that ACTION_CLOSE_SYSTEM_DIALOGS did.
Solution 1:[1]
As per the android official site.
If your app targets Android 12, you don't need to use ACTION_CLOSE_SYSTEM_DIALOGS in this situation. That's because, if your app calls startActivity() while a window is on top of the notification drawer, the system closes the notification drawer automatically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Manoj |