-
-
Save Arunshaik2001/00cdf6220981b98359dda0ca3d205aaf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
NotificationsSampleAppTheme { | |
Surface( | |
modifier = Modifier.fillMaxSize(), | |
color = MaterialTheme.colors.background | |
) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | |
Column( | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
RequestNotificationPermission() | |
} | |
} | |
} | |
} | |
} | |
} | |
@RequiresApi(Build.VERSION_CODES.TIRAMISU) | |
@OptIn(ExperimentalPermissionsApi::class) | |
@Composable | |
private fun RequestNotificationPermission() { | |
val notificationPermissionState = | |
rememberPermissionState(android.Manifest.permission.POST_NOTIFICATIONS) | |
if (notificationPermissionState.status.isGranted) { | |
Text("Notification permission Granted") | |
} else { | |
Column { | |
val textToShow = if (notificationPermissionState.status.shouldShowRationale) { | |
"The notification permission is important for this app. Please grant the permission." | |
} else { | |
"notifications not available" | |
} | |
Text(textToShow) | |
Spacer(modifier = Modifier.height(8.dp)) | |
Button(onClick = { notificationPermissionState.launchPermissionRequest() }) { | |
Text("Request permission") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment