Ask permission for push notification

Viewed 81290

I'm working an application and I need to use push notification. I know that push notification are a normal permission so I can't ask it at run time. But I would insert in the permission when the user downloads and installs the application, and the notice that the application should send a push notification.

How can I do it? Do I have to insert something in the manifest?

5 Answers

If your app targets Android 13 (Tiramisu) or higher, you must declare the POST_NOTIFICATIONS permission as follows:

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

For more details, see the Notification runtime permission page in the Android Developers documentation.

Here's the answer to new update: In API 33 we need runtime permission for push notification: Full documentation is available here but you can simply use like below :

in app level:

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

you can trigger the prompt using this:

pushNotificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)

handle notification result:

private val pushNotificationPermissionLauncher = registerForActivityResult(RequestPermission()) { granted ->
            viewModel.inputs.onTurnOnNotificationsClicked(granted)
        }
Related