From 5f184cb45e4668f8c9c5edac6ca71d723a65a382 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 06:03:46 +0000 Subject: [PATCH] app: request notification permission for updates Turning on "Check for updates" didn't request the notification permission, so on Android 13+ the update notifications could never be shown. Instead, every creation of MainActivity, including rotations, requested the permission when the setting was on, and turned the setting off if it was denied, while the settings switch still showed it as on. Request the permission when the setting is turned on, and only enable it when granted. Only request it on startup when the activity is first created, and refresh the switch from the setting when the settings tab resumes. Assisted-by: Claude Opus 5.5 --- .../com/topjohnwu/magisk/ui/MainActivity.kt | 3 +- .../magisk/ui/component/Permissions.kt | 33 +++++++++++++++++++ .../magisk/ui/settings/SettingsScreen.kt | 7 +++- .../magisk/ui/settings/SettingsViewModel.kt | 2 ++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index 18f8829e8..9410192f6 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -112,7 +112,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost { override fun onCreateUi(savedInstanceState: Bundle?) { showUnsupportedMessage() - if (Config.checkUpdate) { + // Only ask on startup, not every time the activity is recreated + if (savedInstanceState == null && Config.checkUpdate) { extension.withPermission(Manifest.permission.POST_NOTIFICATIONS) { Config.checkUpdate = it } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Permissions.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Permissions.kt index e06054c7d..d32f8ea91 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Permissions.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/component/Permissions.kt @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.ui.component +import android.Manifest.permission.POST_NOTIFICATIONS import android.Manifest.permission.WRITE_EXTERNAL_STORAGE import android.content.pm.PackageManager import android.os.Build @@ -42,3 +43,35 @@ fun rememberExternalStoragePermissionLauncher(onGranted: () -> Unit): () -> Unit } } } + +/** + * Returns a function that runs [onGranted] once the app is allowed to post notifications, + * asking for the permission first on Android 13+, and tells the user if it is denied. + */ +@Composable +fun rememberNotificationPermissionLauncher(onGranted: () -> Unit): () -> Unit { + val context = LocalContext.current + val currentOnGranted by rememberUpdatedState(onGranted) + + val launcher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.RequestPermission() + ) { granted -> + if (granted) { + currentOnGranted() + } else { + context.toast(R.string.post_notifications_denied, Toast.LENGTH_SHORT) + } + } + + return remember(launcher, context) { + { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || + ContextCompat.checkSelfPermission(context, POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED + ) { + currentOnGranted() + } else { + launcher.launch(POST_NOTIFICATIONS) + } + } + } +} diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsScreen.kt index aac66ca13..88fefc8ee 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsScreen.kt @@ -45,6 +45,7 @@ import com.topjohnwu.magisk.ui.component.SettingsSwitch import com.topjohnwu.magisk.ui.component.SmallTitle import com.topjohnwu.magisk.ui.component.onlyHorizontal import com.topjohnwu.magisk.ui.component.rememberBiometricAuth +import com.topjohnwu.magisk.ui.component.rememberNotificationPermissionLauncher import com.topjohnwu.magisk.ui.component.verticalScrollbar import com.topjohnwu.magisk.core.R as CoreR @@ -56,6 +57,7 @@ fun SettingsScreen( val uiState by viewModel.uiState.collectAsStateWithLifecycle() val context = LocalContext.current val requireAuth = rememberBiometricAuth() + val enableCheckUpdate = rememberNotificationPermissionLauncher { viewModel.setCheckUpdate(true) } LifecycleResumeEffect(viewModel) { viewModel.refreshEnvironment() @@ -73,7 +75,10 @@ fun SettingsScreen( onOpenCustomUrlDialog = viewModel::openCustomChannelUrlDialog, onSaveCustomUrl = viewModel::saveCustomChannelUrl, onToggleDoh = viewModel::setDoh, - onToggleCheckUpdate = viewModel::setCheckUpdate, + // Update notifications need the notification permission + onToggleCheckUpdate = { enabled -> + if (enabled) enableCheckUpdate() else viewModel.setCheckUpdate(false) + }, onOpenDownloadPathDialog = viewModel::openDownloadPathDialog, onSaveDownloadPath = viewModel::saveDownloadPath, onToggleRandomPackageName = viewModel::setRandomPackageName, diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt index 2f2313b58..08f7a3e9b 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/settings/SettingsViewModel.kt @@ -83,6 +83,8 @@ class SettingsViewModel : BaseViewModel() { isDeviceSecure = Info.isDeviceSecure, // Config.suAuth also depends on whether the device is secure suAuthEnabled = Config.suAuth, + // Turned off when the notification permission is denied on startup + checkUpdateEnabled = Config.checkUpdate, ) }