Member-only story

Android Notification permission

Andres Sandoval
2 min readAug 13, 2024

--

Tutorial shows how to add the runtime notification permission to receive app notifications. We know in Android 13 and above, we can (and have to) ask for Post Notification permission at runtime. Also, we know that in Android 12 and earlier, the Post Notification are allowed by default.

Followed the steps below.

  1. Inside the file build.gradle.kts(:app)
plugins {
..
id("com.google.gms.google-services")
}


android{
..
dependencies {
..
implementation("com.google.accompanist:accompanist-permissions:0.35.1-alpha")
}
}

2. Add the method

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun NotificationPermissionDemo() {
val notificationPermission = rememberPermissionState(
permission = Manifest.permission.POST_NOTIFICATIONS
)
val context = LocalContext.current

Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column {
Button(onClick = {
if (!notificationPermission.status.isGranted) {
notificationPermission.launchPermissionRequest()
} else {
Toast.makeText(context, "Permission Given Already", Toast.LENGTH_SHORT).show()
}
}) {
Text(text = "Ask for permission")
}
Text(
text = "Permission Status : ${notificationPermission.status.isGranted}",
fontWeight = FontWeight.Bold…

--

--

Andres Sandoval
Andres Sandoval

Written by Andres Sandoval

I'm a passionate Android Software Engineer with over 11 years of experience. andresand.github.io/andres-about-me/ buymeacoffee.com/andresand

No responses yet