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.