In a previous tutorial, we learned how to create a notification on Android. In that tutorial, we did not have to request any permission because applications did not need permission to post notifications on Android 12 and below.
It has been almost a year since the release of that tutorial and Android 13 came along, bringing with it some important changes. Starting on Android 13 (Android API level 33), most applications will have notifications turned off by default.
In this tutorial, we will learn how to request permission from the user to turn on notification permission.
At the end of the tutorial, you would have learned:
- How to request notification permission on Android 13+.
- Android Studio. The version used in this tutorial is Android Studio Dolphin | 2021.3.1 Patch 1.
- Intermediate Android.
- Notification Basics.
To follow along with the tutorial, perform the steps below:
-
Create a new Android project with the default Empty Activity.
-
In the module build.gradle file, change the
compileSdk
andtargetSdk
to33
. -
Add the vector asset below as ic_baseline_notifications_24.xml into the project’s
drawable
directory. This will be used as the notification icon.<vector android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> <path android:fillColor="@android:color/white" android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/> </vector>
-
Add the strings below into your project’s strings.xml file.
<string name="channel_id">123</string> <string name="channel_name">My Channel Name</string> <string name="notification_title">My …