From f24035a99d436f44e27ca4e9a3f6cc744a1277a5 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Wed, 8 Feb 2023 13:48:19 +0300 Subject: [PATCH] android: prevent showing system alert after start (Android 13+) (#1909) * android: prevent showing system alert after start (Android 13+) * refactor * added comments and renamed function * rename * rename --------- Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../java/chat/simplex/app/model/NtfManager.kt | 21 ++++++++++++++----- .../chat/simplex/app/views/WelcomeView.kt | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt index fab42b1b3..e3076b900 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt @@ -44,11 +44,7 @@ class NtfManager(val context: Context, private val appPreferences: AppPreference private val msgNtfTimeoutMs = 30000L init { - manager.createNotificationChannel(NotificationChannel(MessageChannel, generalGetString(R.string.ntf_channel_messages), NotificationManager.IMPORTANCE_HIGH)) - manager.createNotificationChannel(callNotificationChannel(CallChannel, generalGetString(R.string.ntf_channel_calls))) - // Remove old channels since they can't be edited - manager.deleteNotificationChannel("chat.simplex.app.CALL_NOTIFICATION") - manager.deleteNotificationChannel("chat.simplex.app.LOCK_SCREEN_CALL_NOTIFICATION") + if (manager.areNotificationsEnabled()) createNtfChannelsMaybeShowAlert() } enum class NotificationAction { @@ -268,6 +264,21 @@ class NtfManager(val context: Context, private val appPreferences: AppPreference } } + /** + * This function creates notifications channels. On Android 13+ calling it for the first time will trigger system alert, + * The alert asks a user to allow or disallow to show notifications for the app. That's why it should be called only when the user + * already saw such alert or when you want to trigger showing the alert. + * On the first app launch the channels will be created after user profile is created. Subsequent calls will create new channels and delete + * old ones if needed + * */ + fun createNtfChannelsMaybeShowAlert() { + manager.createNotificationChannel(NotificationChannel(MessageChannel, generalGetString(R.string.ntf_channel_messages), NotificationManager.IMPORTANCE_HIGH)) + manager.createNotificationChannel(callNotificationChannel(CallChannel, generalGetString(R.string.ntf_channel_calls))) + // Remove old channels since they can't be edited + manager.deleteNotificationChannel("chat.simplex.app.CALL_NOTIFICATION") + manager.deleteNotificationChannel("chat.simplex.app.LOCK_SCREEN_CALL_NOTIFICATION") + } + /** * Processes every action specified by [NotificationCompat.Builder.addAction] that comes with [NotificationAction] * and [ChatInfo.id] as [ChatIdKey] in extra diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt index ec2c159a6..b60df05f9 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/WelcomeView.kt @@ -21,8 +21,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp +import chat.simplex.app.* import chat.simplex.app.R -import chat.simplex.app.SimplexService import chat.simplex.app.model.ChatModel import chat.simplex.app.model.Profile import chat.simplex.app.ui.theme.* @@ -116,6 +116,7 @@ fun createProfile(chatModel: ChatModel, displayName: String, fullName: String, c if (chatModel.users.isEmpty()) { chatModel.controller.startChat(user) chatModel.onboardingStage.value = OnboardingStage.Step3_SetNotificationsMode + SimplexApp.context.chatModel.controller.ntfManager.createNtfChannelsMaybeShowAlert() } else { val users = chatModel.controller.listUsers() chatModel.users.clear()