7

Google says that all apps targeting Android 13 (API level 33) in order to be able to work with notifications must ask for Manifest.permission.POST_NOTIFICATIONS permission.

My question is: which is a proper way to check if that permission is already granted?

  1. using NotificationManagerCompat.areNotificationsEnabled()
  2. using somthing like ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)

Are there any differences between them? It seems like these two options depend on each other: for example, if I go to the device app settings and disable Notifications then I see that Notification permission is also automatically revoked from the app:

enter image description here

enter image description here

and vice versa.

2 Answers 2

6

The both mentioned ways of checking permissions: NotificationManagerCompat.from(this).areNotificationsEnabled()

and

ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED

returns exactly the same result in all three options in case of:

  • granted permission (true)
  • denied permission (false)
  • forever denied permission (false)

So there are no differences between them, and you can choose the option that you prefer more.

  • The first one is a more concise way and you don't have to surround your code with the if(Build.VERSION.SDK_INT >= 33) check
  • The second option is a more general one. Also, used for asking about other permissions, so you can create an extension function for it to make this option easier to use. For example:
private fun Context.permissionGranted(permission: String): Boolean =
        ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED

Unfortunately, with the second option you still have to surround your code with the SDK check, so it would look like:

if (Build.VERSION.SDK_INT >= 33) permissionGranted(Manifest.permission.POST_NOTIFICATIONS)

And also, at the end, some general info about the notifications and the app settings:

  • Yes, the permission and the notification options depends on each other
  • The notifications are by default turned off staring from Android 13 (SDK 33)
  • After clearing the app data all permissions are cleared, also the notification one.
0
3

returns exactly the same result in all three options

Apparently not on all devices. I consistently got PERMISSION_DENIED, despite permissions being enabled in the settings, when using ContextCompat.checkSelfPermission.

Changing to NotificationManagerCompat.from(this).areNotificationsEnabled() matches up to settings.

Might be the Samsung android version that isn't set up correctly or something, but NotificationManagerCompat seems like the safer choice.

1
  • 1
    ContextCompat.checkSelfPermission() has fallback to the NotificationManagerCompat.areNotificationsEnabled() if the Android < 13, otherwise a permission check is performed by process id and user id
    – plplmax
    Jan 18 at 3:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.