I usually add a post_notification permission request to my app. The strange thing is that when I request directly, the dialog will not pop up. But if I create the Notification channel first, I can jump out of the dialog normally, Has anyone encountered the same situation? Or is there something wrong with my settings? Thanks!
AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Fragement:
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)
}
}
Situation 1
if I add createNotificationChannel()
before request permisssion,it work.
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)
}
Situation 2
When I mark createNotificationChannel()
,the dialog would not showing anymore.
After adding the Log, I found that, After executing requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS), there is no log jumping out, just get the return of registerForActivityResult = false, but I opened the app for the first time, and I have not rejected the notifcaiton permission.
I want to know how to fix it?