From 3624dd4854da72170c456cc9946b692ff261c3f2 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Mon, 20 Jul 2026 13:47:58 +0700 Subject: [PATCH] stop foreground task if notification disabled --- .../kotlin/su/reya/coop/NostrForegroundService.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt index be04f39..347727d 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt @@ -11,10 +11,10 @@ import android.os.Build import android.os.IBinder import android.util.Log import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import androidx.core.net.toUri import androidx.lifecycle.Lifecycle import androidx.lifecycle.ProcessLifecycleOwner -import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -28,8 +28,6 @@ private const val GROUP_KEY_MESSAGES = "su.reya.coop.MESSAGES" class NostrForegroundService : Service() { private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) - - var ioDispatcher: CoroutineDispatcher = Dispatchers.IO private val nostr by lazy { NostrManager.instance } private var notificationJob: Job? = null @@ -191,4 +189,13 @@ class NostrForegroundService : Service() { super.onDestroy() serviceScope.cancel() } + + override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) + if (!NotificationManagerCompat.from(this).areNotificationsEnabled()) { + Log.d("Coop", "Stopping service on task removed because notifications are disabled") + stopForeground(STOP_FOREGROUND_REMOVE) + stopSelf() + } + } }