diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt index 9265d889d..93a556a01 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/notifications/NotificationRelayService.kt @@ -89,7 +89,20 @@ class NotificationRelayService : Service() { try { ContextCompat.startForegroundService(context, intent) } catch (e: Exception) { - Log.e(TAG, "Failed to start foreground service", e) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && + e is ForegroundServiceStartNotAllowedException + ) { + // Android 12+ blocks startForegroundService() from the background unless + // the caller has a temporary FGS exemption (e.g. broadcast receiver, + // exact alarm, high-priority FCM). Cold-start triggered by a flow emission + // from AlwaysOnNotificationServiceManager hits this path. The other + // notification layers (BootCompletedReceiver, ServiceWatchdogManager, + // NotificationCatchUpWorker) plus MainActivity.onResume retry from + // contexts that are allowed. + Log.w(TAG) { "Foreground service start not allowed from background; will retry from another layer" } + } else { + Log.e(TAG, "Failed to start foreground service", e) + } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index 4b1cd5789..40a464cec 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -31,6 +31,7 @@ import com.vitorpamplona.amethyst.debugState import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.service.lang.LanguageTranslatorService +import com.vitorpamplona.amethyst.service.notifications.NotificationRelayService import com.vitorpamplona.amethyst.service.playback.composable.DEFAULT_MUTED_SETTING import com.vitorpamplona.amethyst.service.playback.pip.BackgroundMedia import com.vitorpamplona.amethyst.ui.navigation.findParameterValue @@ -89,6 +90,13 @@ class MainActivity : AppCompatActivity() { // starts muted every time DEFAULT_MUTED_SETTING.value = true + + // If always-on notifications are enabled but the foreground service couldn't be + // started from the background during cold-start (Android 12+ restriction), retry + // now that the activity is in the foreground. + if (NotificationRelayService.isEnabled(this)) { + NotificationRelayService.start(this) + } } override fun onPause() {