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.

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159
porlicus
  • 739
  • 1
  • 7
  • 14

2 Answers2

5

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.
Patryk Kubiak
  • 1,679
  • 2
  • 11
  • 17
2

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.

Rubberduck
  • 1,107
  • 2
  • 13
  • 26
  • 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 '23 at 03:11