request notification permission on android 13 when showing home content

This commit is contained in:
Adam Brown 2022-10-12 22:59:31 +01:00
parent 3444285592
commit b8689292c3
5 changed files with 28 additions and 2 deletions

View File

@ -75,6 +75,16 @@ abstract class DapkActivity : ComponentActivity(), EffectScope {
}
}
protected fun registerForPermission(permission: String, callback: () -> Unit = {}): () -> Unit {
val resultCallback: (result: Boolean) -> Unit = { result ->
if (result) {
callback()
}
}
val launcher = registerForActivityResult(ActivityResultContracts.RequestPermission(), resultCallback)
return { launcher.launch(permission) }
}
protected suspend fun ensurePermission(permission: String): PermissionResult {
return when {
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted

View File

@ -21,5 +21,6 @@ sealed interface HomeScreenState {
sealed interface HomeEvent {
object Relaunch : HomeEvent
object OnShowContent : HomeEvent
}

View File

@ -39,6 +39,7 @@ class HomeViewModel(
fun start() {
viewModelScope.launch {
state = if (credentialsProvider.isSignedIn()) {
_events.emit(HomeEvent.OnShowContent)
initialHomeContent()
} else {
SignedOut
@ -62,6 +63,7 @@ class HomeViewModel(
fun loggedIn() {
viewModelScope.launch {
state = initialHomeContent()
_events.emit(HomeEvent.OnShowContent)
listenForInviteChanges()
}
}

View File

@ -1,5 +1,7 @@
package app.dapk.st.home
import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.AlertDialog
@ -27,10 +29,11 @@ class MainActivity : DapkActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val pushPermissionLauncher = registerPushPermission()
homeViewModel.events.onEach {
when (it) {
HomeEvent.Relaunch -> recreate()
HomeEvent.OnShowContent -> pushPermissionLauncher?.invoke()
}
}.launchIn(lifecycleScope)
@ -45,6 +48,14 @@ class MainActivity : DapkActivity() {
}
}
private fun registerPushPermission(): (() -> Unit)? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerForPermission(Manifest.permission.POST_NOTIFICATIONS)
} else {
null
}
}
@Composable
private fun BetaUpgradeDialog() {
AlertDialog(

View File

@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="app.dapk.st.notifications"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.dapk.st.notifications">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
</manifest>