Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request Notification Permission in Jetpack Compose with a button?

I am creating an app which is a Healthy Life application. In the app I want to send some notifications. I don't want to request permission like Accept for this app or accept for this time or deny. When app is opened a page will welcome users. This page will contain some information text about notification and below it there will be a button. When user click that button the permission will be accepted then the flow will go. There are some examples about requesting permission but as I said they have 3 options and contains some rationale if you deny for the first time etc. Briefly I want to request a notification permission with a button. Thank you.

like image 380
Yunus Emrah Uluçay Avatar asked Oct 17 '25 21:10

Yunus Emrah Uluçay


2 Answers

You can use rememberLauncherForActivityResult for requesting permission for notifications as

val context = LocalContext.current

var hasNotificationPermission by remember {
    mutableStateOf(
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {

            ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.POST_NOTIFICATIONS
            ) == PackageManager.PERMISSION_GRANTED
        } else true
    )
}

val permissionRequest =
    rememberLauncherForActivityResult(contract = ActivityResultContracts.RequestPermission()) { result ->
        hasNotificationPermission = result
        // if granted you can show notification here
        if(hasNotificationPermission) showNotification(context)
    }

And in Button's click callback use

if (hasNotificationPermission) {
    showNotification(context)
} else {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        permissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS)
    }
}
like image 56
Thracian Avatar answered Oct 19 '25 12:10

Thracian


You can start doing it with adding the dependency in build.gradle(app-level) file.

Jetpack Compose allows you to integrate the permission request flow seamlessly into your app.

implementation("com.google.accompanist:accompanist-permissions:0.33.1-alpha")

Add the following permission in AndroidManifest.xml

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

Complete Code Here

  1. Define a state variable which keeps track of permission.
  2. Check whether permission is given or not. if not ask for permission
  3. Update UI based on permission.

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
@OptIn(ExperimentalPermissionsApi::class)
@Composable

fun PermissionDemo() {

    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,
                fontSize = 16.sp
            )
        }
    }
}
like image 22
Chirag Thummar Avatar answered Oct 19 '25 11:10

Chirag Thummar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!