google / accompanist

A collection of extension libraries for Jetpack Compose
https://google.github.io/accompanist
Apache License 2.0
7.42k stars 597 forks source link

onPermissionsResult isn't triggered on POST_NOTIFICATIONS permission request (API 33). #1740

Closed lupsyn closed 2 months ago

lupsyn commented 8 months ago

Describe the bug

onPermissionsResult call back isn't triggered on android.permission.POST_NOTIFICATIONS on allow/deny (API 33)

To Reproduce

Use a generic impl as the following one to trigger the permission request.

fun UiContent.toPermissionDialog(
    rationale: @Composable () -> Unit = { },
    onPermissionResultCallback: (allPermissionsGranted: Boolean) -> Unit = {},
) {
    val permissionState = rememberMultiplePermissionsState(
        permissions = permissions,
        onPermissionsResult = { permissionGrantMap ->

            onPermissionResultCallback(permissionGrantMap.values.all { it })
        }
    )

    if (permissionState.allPermissionsGranted) {
        onPermissionResultCallback(true)
    } else {
        if (permissionState.shouldShowRationale) {
            rationale.invoke()
        }

        LaunchedEffect(key1 = this) {
            permissionState.launchMultiplePermissionRequest()
        }
    }
}

Expected behavior

onPermissionsResult should be triggered

Environment:

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

chubecode commented 5 months ago

how it's goin ?

sprokipchyn commented 4 months ago

same with CAMERA permission request. Is there any benefit of this library if I cannot get back any result of permission request?

PrimoDev23 commented 4 months ago

This seems to work just fine for both permission types when using the attached code. Anyways the attached code is not following best practices, since you are calling a normal function in a compose context, which might result in calling it every recomposition. You should prefer something like this

val permissionState = rememberMultiplePermissionsState(
      permissions = listOf(Manifest.permission.CAMERA),
      onPermissionsResult = { result ->
          val allGranted = result.values.all { it }

          if (allGranted) {
              println("GRANTED")
          } else {
              println("DENIED")
          }
      }
)

if (permissionState.shouldShowRationale) {
      Text(
          modifier = Modifier.clickable {
              permissionState.launchMultiplePermissionRequest()
          },
          text = "PLEASE OBI WAN, GIVE ME SOME POWER!"
      )
} else if (!permissionState.allPermissionsGranted) {
      LaunchedEffect(Unit) {
          permissionState.launchMultiplePermissionRequest()
      }
}
sprokipchyn commented 4 months ago

@PrimoDev23 I have the exact code and for some reason onPermissionsResult is not being called. So I see the standard permission popup (camera in my case). I click Allow for this time only and that's it: popup disappears, nothing happens, the callback is not triggered. Interesting fact: next time I call this code - permission is already granted and I can use the camera.

PrimoDev23 commented 4 months ago

@PrimoDev23 I have the exact code and for some reason onPermissionsResult is not being called. So I see the standard permission popup (camera in my case). I click Allow for this time only and that's it: popup disappears, nothing happens, the callback is not triggered. Interesting fact: next time I call this code - permission is already granted and I can use the camera.

It's quite unclear why this is happening. Did you test this using the emulator? I tested this with API 34 and 33 and it worked just fine in both cases for me

github-actions[bot] commented 3 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.