Android 13中registerForActivityResult请求通知权限无回调问题
Android 13推送通知权限请求回调未触发问题原因分析
问题场景
实现代码
class MainActivity : ComponentActivity(), LocationListener { val notificationPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean -> if (isGranted) { // Permission is granted. Continue the action or workflow in your // app. } else { // Explain to the user that the feature is unavailable because the // feature requires a permission that the user has denied. At the // same time, respect the user's decision. Don't link to system // settings in an effort to convince the user to change their // decision. } } private fun requestPushNotificationPermissions(){ if ((ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED)) { // granted }else { // not granted, ask for permission notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } } }
实际现象
- 用户首次安装应用后,
checkSelfPermission返回未授予权限,启动权限请求启动器 - 用户在Android 13设备上看到权限弹窗
- 用户选择“允许”或“不允许”后,
registerForActivityResult的回调始终未触发
依赖版本
implementation 'androidx.activity:activity-ktx:1.2.0-alpha07' implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha07'
问题原因
使用的androidx.activity和androidx.fragment版本过低,不支持Android 13(API 33)新增的POST_NOTIFICATIONS权限请求流程:
POST_NOTIFICATIONS权限是Android 13才引入的能力,而你使用的activity-ktx:1.2.0-alpha07和fragment-ktx:1.3.0-alpha07是2020年的早期预览版本,当时并未适配后续Android版本新增的这类权限类型。- 旧版本的Activity Result API实现无法正确处理Android 13的通知权限请求回调逻辑,导致用户完成权限选择操作后,对应的回调没有被触发。
内容的提问来源于stack exchange,提问作者mkto
相关产品推荐
相关产品推荐

