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() + } + } } diff --git a/shared/src/commonMain/kotlin/su/reya/coop/nostr/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/nostr/Nostr.kt index 2580334..ce06e65 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/nostr/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/nostr/Nostr.kt @@ -154,7 +154,10 @@ class Nostr( // Trigger new message notification if (rumor != null) { - if (rumor.createdAt().asSecs() >= now.asSecs()) { + val isSelfMessage = rumor.author() == signer.publicKeyFlow.value + val isNew = rumor.createdAt().asSecs() >= now.asSecs() + + if (isNew && !isSelfMessage) { onNewMessage(rumor) } }