11

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.

Leandro 86
  • 157
  • 1
  • 9

1 Answers1

5

ACTION_CLOSE_SYSTEM_DIALOGS is for system and preinstalled apps. Needs signature permission.

AccessibilityService.GLOBAL_ACTION_DISMISS_NOTIFICATION_SHADE is an overkill. is like, "give me keys to your home so I can clean up junk next to your school."

StatusBarManager collapsePanels() is hidden API. you have to use double reflection to use it (AndroidStudio is smart enough to see through simple reflection). Didn't work for me. It throws an exception, but it is hidden by reflection.

Sorry I couldn't give you a solution, but at least I gave you places where I searched and failed.

skr
  • 1,700
  • 1
  • 15
  • 39
windruf
  • 51
  • 1
  • 3