2

I'm facing unwelcome behavior when asking new POST_NOTIFICATIONS permission on some particular device owned by remote tester - Pixel 4a (Android 13). Sadly don't have logs and "cable access". Freshly installed app - still targetting API30, but also tried with target set to 33, just for test - should show custom splashscreen Activity, after that shows "main" Activity, which in onResume tries to create NotificationChannel. This should cause perm dialog pop up

If your app targets 12L (API level 32) or lower, the system shows the permission dialog the first time your app starts an activity after you create a notification channel, or when your app starts an activity and then creates its first notification channel. This is usually on app startup.

Well, not on this Pixel 4a with Android 13, meanwhile on Pixel 6 with Android 13 dialog shows up...

Funniest thing is... When tester install app, runs first time, no dialog, then kill it, navigate to system settings and clear data/cache (or even won't make first run, just clean after installation), then dialogs shows up at "first" run...

Why?!

Edit: so now I can reproduce problem also on Pixel 6. I've introduced middle-Dialog with info about content in pushes/notifications and simple yes/no buttons. "Yes" is creating (first) NotificationChannel and this doesn't cause POST_NOTIFICATIONS perm dialog appearance...

@RequiresApi(Build.VERSION_CODES.O)
fun addStaticNotificationChannel(channelId: String, nameResId: Int, descriptionResId: Int,
                                 importance: Int, soundOn: Boolean = true, forceRecreate: Boolean = false): String {
    val name = context.resources.getText(nameResId).toString()
    val description = context.resources.getText(descriptionResId).toString()

    /*if (manager.getNotificationChannel(channelId) != null) {
        if (forceRecreate) manager.deleteNotificationChannel(channelId)
        else return channelId
    }*/
    val channel = NotificationChannel(channelId, name, importance)

    channel.description = description
    channel.lockscreenVisibility = VISIBILITY_PUBLIC
    channel.setShowBadge(true)
    channel.enableLights(true)
    channel.lightColor = ContextCompat.getColor(context, R.color.tsi_blue)

    if (!soundOn)
        channel.setSound(null, null)

    Log.i(this.javaClass.simpleName, "createNotificationChannel channeldId:$channelId")
    manager.createNotificationChannel(channel)
    return channelId
}
snachmsm
  • 17,866
  • 3
  • 32
  • 74

1 Answers1

4

I've messed up targetSdk and branches of my project, and turned out that:

  • asking perm straight (ContextCompat.request...) when targetting < 33 won't show any dialog
  • creating channel when targetting 33+ and no permission granted (yet?) also won't show any dialog

so comprehensive future-proof code for those, who can't jump to 33 right away would be

val targetSdkVersion: Int = activity.applicationContext.applicationInfo.targetSdkVersion
if (targetSdkVersion < 33) {
    // behavior when targeting < 33 (Android 13), first channel creation will show perm prompt
    Log.i(this.javaClass.simpleName, "trying to create channel, when no perm")
    addStaticNotificationChannel(SOME_CHANNEL_ID, R.string.notification_some_channel_title,
            R.string.notification_some_channel_desc, IMPORTANCE_LOW, forceRecreate = true)
} else {
    Log.i(this.javaClass.simpleName, "asking for notification perm")
    ActivityCompat.requestPermissions(activity, arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 89)
}
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I have issue when target 33 below, when I create notification channel, it just doesn't prompt until I put the app into recent tasks and bring back to foreground, did you face that before ? – brendan Dec 20 '22 at 03:26
  • sadly no idea for your case... inspect system logs, check when `Intent` opening dialog is called (I'm sure that there was a system log for that, this dialog is in fact styled `Activity` afaik) – snachmsm Dec 20 '22 at 09:06
  • alright thanks, i think i i will try target 33 and do runtime then :') – brendan Dec 21 '22 at 01:04