androidandroid-permissionsandroid-push-notificationandroid-12l

How to I request for Post Notification permission for Android 12L or lower?


We know in Android 13 and above, we can (and have to) ask for Post Notification permission as per https://stackoverflow.com/a/72981519/3286489

Also, we know that in Android 12L and earlier, the Post Notification are allowed by default. As app developer, we don't need to ask for it.

Nonetheless, in Android 12L and earlier, the user can turn off the Post Notification entirely through the App Setting.

In such an event, I am hoping to still ask the User to re-enable it in our App.

I tried the Android 13 approach at https://stackoverflow.com/a/72981519/3286489, and it clearly doesn't work on Android 12L devices.

So I tried the permission request applicable for Android 12L same as the way we ask for other permission (e.g. Location Permission), as below.

The checking works, but the Dialog asking for permission won't show.

            val permissionState =
                ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
            if (permissionState == PackageManager.PERMISSION_DENIED) {
                Toast.makeText(this, "Try granting notification Android 12 or lower", Toast.LENGTH_SHORT).show()
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                    1
                )
            }

How can I ask for Post Notification permission for Android 12L and earlier?


Solution

  • How can I ask for Post Notification permission for Android 12L and earlier?

    That permission does not exist on Android 12L and earlier. You cannot request it.

    Nonetheless, in Android 12L and earlier, the user can turn off the Post Notification entirely through the App Setting.

    That is independent from the POST_NOTIFICATION permission. Users can still disable your app's ability to post notifications, overall or by channel, even if they granted the permission.

    In such an event, I am hoping to still ask the User to re-enable it in our App.

    You would need to create your own explanatory user interface for that, then try launching the associated Settings screen.