Open In App

How to Enable Notification Runtime Permission in Android 13?

Last Updated : 26 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Android 13 starts a new realm of new app permissions which are focussed on users’ privacy and peace of mind, one of which is the permission to send notifications to the user. Notifications are an integral part of the Android Operating System, but when you have tons of apps installed, then receiving notifications from them starts getting worse. Starting from this Android Version, one can op-tin to which notification they want and which they don’t, something which Apple has now been doing for quite a time.

In order to take use of the extra control and flexibility offered by this feature, we strongly advise that you target Android 13 or higher as soon as feasible. Continued targeting of 12L (API level 32) or lower limits your ability to ask for authorization in the context of the functionality of your app.

Declare the Authorization

Update your program to target Android 13 and use the same steps as when seeking other runtime rights, as explained in the following sections, to ask for the new notification permission. As with all the other permissions which we request throughout developing for Android Ecosystem, we will, first of all, update the Manifest File to tell the system that we are now using the newer permissions and then move forward. The first step would be to add the permission line to our App’s Manifest! To add the permission, do this:

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

Note: To launch a foreground service, apps do not need to ask for the POST NOTIFICATIONS permission. Similar to earlier versions of Android, apps must include a notification when they launch a foreground service.

User decisions in the permissions dialogue determine the app’s capabilities:

When permission is required from the user while the app is running the user is presented with the following actions which the user can take:

  • Choose “allow“.
  • Choose “don’t allow”.
  • Push neither of the buttons as you swipe away from the dialogue. 

According to the user’s behavior, the following sections describe how your program acts.

The user chooses “Allow”

The following is possible for your app if the user chooses the allow option:

  • Send out alerts. 
  • All notification methods are acceptable.
  • Notify users of foreground services via postings. 
  • The notification drawer displays these notifications.

The user chooses “Don’t allow”

The worst case scenario for your app would be when there is a denial to your request, then the following channel activates

  • Your app cannot send alerts if the user chooses the don’t allow option unless it is exempt from this requirement. 
  • With the exception of a few specified positions, all notification channels are banned. 
  • This behavior is comparable to what happens when a user manually disables every notification for your app in the system settings.

What if the user simply swipes to go back to the home without choosing anything?

When such a situation arises, then the Android System does not assign any state to your application, and the prompts will re-appear when the user opens the app the next time!

Note: If your application targets 12L or lower and a user taps Don’t allow even once, they aren’t prompted again until your app is updated to support Android 13 or above.

Eligibility for pre-grant permission

Your app must already have a notification channel and not have its notifications expressly turned off by the user on a device running 12L or lower in order to qualify for an automatic pre-grant. When a device updates to Android 13 or higher, the denial still stands if the user deactivated alerts for your app on a device running Android 12L or lower.

Asking for permissions on Runtime:

When the user has started using your app you can use the below code to ask for permission to show the notifications, it’s upon them now to either accept or not based on their decision.

Kotlin




override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.gfg_main_activity)
    val gfgMessage = intent?.getStringExtra(NOTIFICATION_GFGMESSAGE_TAG)
    findViewById<TextView>(R.id.tv_gfgMessage).text = gfgMessage
    findViewById<Button>(R.id.click).setOnClickListener {
        when {
            ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
                PackageManager.PERMISSION_GRANTED -> {
                Log.e(TAG, "User accepted the notifications!")
                sendNotification(this)
            }
            shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) -> {
                Snackbar.make(
                        findViewById(R.id.parent_layout),
                        "The user denied the notifications ):",
                        Snackbar.LENGTH_LONG
                    )
                    .setAction("Settings") {
                        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        val uri: Uri =
                            Uri.fromParts("com.onesilisondiode.geeksforgeeks", packageName, null)
                        intent.data = uri
                        startActivity(intent)
                    }
                    .show()
            }
            else -> {
                requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
            }
        }
    }
}


By adding this code, you will be able to present the user with the pop-up to ask for permission to show notifications, if the user enables the notifications you will be able to deliver your notifications as you want, otherwise sadly all your App notifications will be blocked.

Conclusion

The app may be explored by new users so they may experience first-hand the advantages of each specific notification request. User activity can start a permissions prompt. Once you have been given the authorization to send notifications, use it wisely. The number of daily notifications sent by your app is visible to users, and they have the option to remove permission at any moment.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads