Merge pull request #202 from ouchadam/bug/android-13-notifications

Fix missing notifications on android 13
This commit is contained in:
Adam Brown 2022-10-12 23:06:34 +01:00 committed by GitHub
commit ab163001db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 { protected suspend fun ensurePermission(permission: String): PermissionResult {
return when { return when {
checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED -> PermissionResult.Granted

View File

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

View File

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

View File

@ -1,5 +1,7 @@
package app.dapk.st.home package app.dapk.st.home
import android.Manifest
import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.AlertDialog import androidx.compose.material3.AlertDialog
@ -27,10 +29,11 @@ class MainActivity : DapkActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val pushPermissionLauncher = registerPushPermission()
homeViewModel.events.onEach { homeViewModel.events.onEach {
when (it) { when (it) {
HomeEvent.Relaunch -> recreate() HomeEvent.Relaunch -> recreate()
HomeEvent.OnShowContent -> pushPermissionLauncher?.invoke()
} }
}.launchIn(lifecycleScope) }.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 @Composable
private fun BetaUpgradeDialog() { private fun BetaUpgradeDialog() {
AlertDialog( AlertDialog(

View File

@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?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>