1

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?

Lionheart
  • 21
  • 5

3 Answers3

0

Step # 1

Can you change this

== PackageManager.PERMISSION_DENIED

to

!= PackageManager.PERMISSION_GRANTED

Step # 2

Ask for shouldShowRequestPermissionRationale, if user denies permission for once, on this official documentation

Sohaib Ahmed
  • 1,990
  • 1
  • 5
  • 23
0

I had a similar problem and had forgotten to update my gradle build files to set the target SDK to 33. Updating the target SDK fixed the problem for me.

Gregory Hassett
  • 306
  • 3
  • 12
0

Check that your target SDK is 33 or higher