porlicus
porlicus

Reputation: 871

Is there a difference between "areNotificationsEnabled()" and "checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)"

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.

Upvotes: 11

Views: 3407

Answers (3)

tynn
tynn

Reputation: 39853

On systems before Android 13 (API level 33), the compat version of the checkSelfPermission(this, POST_NOTIFICATIONS) function delegates to the compat version of the areNotificationsEnabled() function.

So looking at the AOSP sources for the NotificationManagerService you'll notice that the call to areNotificationsEnabled() eventually delegates to mPermissionHelper.hasPermission(uid)

/**
 * Returns whether the given uid holds the notification permission. Must not be called
 * with a lock held.
 */
public boolean hasPermission(int uid) {
    final long callingId = Binder.clearCallingIdentity();
    try {
        return mPmi.checkUidPermission(uid, NOTIFICATION_PERMISSION) == PERMISSION_GRANTED;
    } finally {
        Binder.restoreCallingIdentity(callingId);
    }
}

So I'd say it's safe to assume that the areNotificationsEnabled() function and the more generic call to the checkSelfPermission(this, POST_NOTIFICATIONS) function could be considered equivalent.

Upvotes: 4

Rubberduck
Rubberduck

Reputation: 1155

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.

Upvotes: 4

Patryk Kubiak
Patryk Kubiak

Reputation: 1969

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.

Upvotes: 8

Related Questions