Reputation: 871
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?
NotificationManagerCompat.areNotificationsEnabled()
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:
and vice versa.
Upvotes: 11
Views: 3407
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
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
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:
So there are no differences between them, and you can choose the option that you prefer more.
if(Build.VERSION.SDK_INT >= 33)
checkprivate 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:
Upvotes: 8