androidpermissionsandroid-jetpack-composejetpack-compose-accompanist

How to use Compose Accompanist to work with new permissions on old devices?


Using Jetpack Compose Permissions from the Accompanist Permission library (https://google.github.io/accompanist/permissions/) is a nice reactive way. However, it's unclear to me how to deal with permissions which depend on the OS version its being used on.

For instance, the opt-out for notification changed with Android Tiramisu/33 så that it's now opt-in using POST_NOTIFICATIONS permission. The question then is, how do you use

val permissionState: PermissionState = rememberPermissionState(
        permission = Manifest.permission.POST_NOTIFICATIONS
    )

...when the POST_NOTIFICATIONS API can't be referenced on say Android 9. The only workaround I can think of, which feels like a hack, is to fall-back to some other permission which is auto-granted by default i.e. the INTERNET permission. Like so:

val permissionState: PermissionState = rememberPermissionState(
        permission = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
            Manifest.permission.POST_NOTIFICATIONS else Manifest.permission.INTERNET
    )

Is there a better solution than the one mentioned above?


Solution

  • There is a better workaround:

     val permissionState = rememberMultiplePermissionsState(
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            listOf(Manifest.permission.POST_NOTIFICATIONS)
        } else {
            // permission not needed
            emptyList()
        }
     )