Android 14 (API level 34) permission dialog not showing for Push Notifications
M

1

6

After upgrading to Android API level 34, our Android app's push notifications are blocked by default. I know this is in line with the newest changes from Android to use an "opt in" vs "opt out" model to send push notification prompts.

But the problem is, in short: My code works asking for e.g. Camera permission, but it doesn't work for Notifications. I really don't understand why. I am wondering if I am missing something or if it is something related to the platform.

I implemented a runtime permission as described here

Code:

NavigationController:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityNavigationBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.navigationBottomNavView.itemIconTintList = null
        requestPermission()
        //...
    }

val requestPermissionLauncher =
        registerForActivityResult(
            ActivityResultContracts.RequestPermission()
        )
        { isGranted: Boolean ->
            if (isGranted) {
                // todo handle success
            } else {
                // todo handle error
            }
        }

fun requestPermission() {
        when {
            ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.POST_NOTIFICATIONS
            ) == PackageManager.PERMISSION_GRANTED -> {
                // Permission is granted
            }

            ActivityCompat.shouldShowRequestPermissionRationale(
                this,
                Manifest.permission.POST_NOTIFICATIONS
            ) -> {
                // Additional rationale should be triggered
                // For now simply ask for Notification Permission for testing purpose
                requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
            }

            else -> {
                // Permission has not been asked yet
                requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
            }
        }
    }

The above works with Manifest.permission.CAMERA. It doesn't with POST_NOTIFICATIONS.

Permissions are declared in the Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.companyname.android.companystuff">

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.CAMERA" />

Did anyone experience this problem and found a solution?

Mall answered 19/10, 2023 at 12:45 Comment(5)
I wonder what exactly your problem is. What does it matter if a box pops up or not as long as you can post notifications. Unclear what happens at you. You did not tell.Jacobean
My problem is that the permission has not been granted and I can't receive push notifications. When debugging I am stepping into the else clause and it calls requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) but the dialog wouldn't show. Wondered if the system is suppressing the dialog or if I forgot to configure something else. The same code for Manifest.permission.CAMERA steps into that else clause too, calls the function launch and then shows the popup as expected.Mall
I can't receive push notifications Sorry POST_NOTIFICATIONS is for posting notifications. Noting about receiving.Jacobean
Further you did tell nothing new and you did not react on my comment at all.Jacobean
The "POST" in this context is from the app to the user (not other devices). So, if the app can't "post" anything to me, then I don't know what it has received.Reforest
L
8

The dialog of Notification permission appears only after you create a notification channel. Until you have created a notification channel, the system does not consider it necessary to request permission from the user. I hope my answer helped you, since this is not an obvious thing. I realized this after the second launch of the application, when the channel has already been created and the permission request was shown.

Lepley answered 9/2 at 9:6 Comment(1)
Thank you so much! I was stuck as of why my app was not responding after I granted permission, but if I closed it and reopened it worked just fine.Kelsey

© 2022 - 2024 — McMap. All rights reserved.