Today we will implement a new Runtime Permission for Notification that was added in Android 13 (Tiramisu).
Notification Runtime Permission is required from Android 13 to post the notification, which was not needed below Android 13. After this change in the new android 13, users will have to grant this permission so your app can post notifications.
Let’s get into the code.
Before implementing this in your existing code, make sure. Your app’s target and compile SDK version is 33 or above.
It will look something like this.
android {
namespace '...'
compileSdk 33
defaultConfig {
applicationId "..."
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
Do not forget to sync after changing this SDK version.
If you have a little bit of experience with how to ask for other permissions like Storage permission Camera Permission, or Location Permission, then this is nothing new to learn; it's straightforward like others.
Let’s declare runtime permission in the Manifest.xml file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
//………
</manifest>
We will use jetpack compose here for this project. Let’s see how it works.
Create one Application class where we will create a Notification Channel.
Like this…
class MyApp: Application() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"your_channel_id",
"your_channel_name",
NotificationManager.IMPORTANCE_HIGH
)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
}
}
}
Do not forget to add this in the Manifest.xml file under the application tag.
<application
android:name=".MyApp"
Ok, Till now we are good. What’s next?
Create one activity in which we want to ask for this runtime permission. Here we will create MainActivity.kt
We need this permission only if our application’s target SDK version is 13 or above 13.
var hasNotificationPermission by remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mutableStateOf(
ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
)
} else mutableStateOf(true)
}
So, here if permission is not granted already it will show permission dialogue to grant the permission. Then users can choose if they want an application to post the notification or not.
So, the final code looks something like this.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NotificationPermissionsTheme {
val context = LocalContext.current
var hasNotificationPermission by remember {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mutableStateOf(
ContextCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
)
} else mutableStateOf(true)
}
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted ->
hasNotificationPermission = isGranted
}
)
Button(onClick = {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}) {
Text(text = "Request permission")
}
Button(onClick = {
if(hasNotificationPermission) {
// showNotification()
}
}) {
Text(text = "Show notification")
}
}
}
}
}
}
This will work as expected. See you next time.