request notification permission on android 13 when showing home content
This commit is contained in:
parent
3444285592
commit
b8689292c3
|
@ -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
|
||||
|
|
|
@ -21,5 +21,6 @@ sealed interface HomeScreenState {
|
|||
|
||||
sealed interface HomeEvent {
|
||||
object Relaunch : HomeEvent
|
||||
object OnShowContent : HomeEvent
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue