Search code examples
androidpermissionsnotificationsandroid-api-levelsruntime-permissions

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


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?


Solution

  • 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.