我通常会在我的应用程序中添加一个 post_notification 权限请求。 奇怪的是,当我直接请求时,对话框不会弹出。 但是如果我先创建Notification通道,就可以正常跳出对话框, 有没有人遇到过同样的情况?还是我的设置有问题?谢谢!
AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
片段:
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
super.onViewCreated(view, savedInstanceState)
requestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (it) {
LogUtil.d(TAG, "Grant POST_NOTIFICATION permission")
} else {
LogUtil.d(TAG, "Denied POST_NOTIFICATION permission")
}
}
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onResume()
{
if (ContextCompat.checkSelfPermission(
activity!!.applicationContext,
Manifest.permission.POST_NOTIFICATIONS,
) == PackageManager.PERMISSION_DENIED
) {
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
情况一
如果我在请求许可之前添加
createNotificationChannel()
,它会起作用。
private fun createNotificationChannel() {
val channel = NotificationChannel(
CHANNEL_ID,
"Important Notification Channel",
NotificationManager.IMPORTANCE_HIGH,
).apply {
description = "This notification contains important announcement, etc."
}
notificationManager.createNotificationChannel(channel)
}
情况2
当我标记
createNotificationChannel()
时,对话框将不再显示。
添加Log后发现, 执行requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)后, 没有日志跳出, 只需获取 registerForActivityResult = false 的返回值, 但是我是第一次打开app,并没有拒绝通知权限
我想知道如何修复它?
你能改变这个吗
== PackageManager.PERMISSION_DENIED
到
!= PackageManager.PERMISSION_GRANTED
请求
shouldShowRequestPermissionRationale
,如果用户拒绝一次许可,在这个官方文档
我遇到了类似的问题,忘记更新我的 gradle 构建文件以将目标 SDK 设置为 33。更新目标 SDK 为我解决了这个问题。