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?
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 forManifest.permission.CAMERA
steps into that else clause too, calls the function launch and then shows the popup as expected. – MallI can't receive push notifications
Sorry POST_NOTIFICATIONS is for posting notifications. Noting about receiving. – Jacobean