From 9f639baf1b9e224ad42971d25a0a243021864d21 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 6 Jun 2026 14:56:13 +0700 Subject: [PATCH] prevent run multiple notification job --- .../kotlin/su/reya/coop/NostrForegroundService.kt | 11 +++++++---- shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt index a627578..9f5936f 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/NostrForegroundService.kt @@ -10,13 +10,13 @@ import android.content.pm.ServiceInfo import android.os.Build import android.os.IBinder import android.util.Log -import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import androidx.core.net.toUri import androidx.lifecycle.Lifecycle import androidx.lifecycle.ProcessLifecycleOwner import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch @@ -25,6 +25,7 @@ import java.io.File class NostrForegroundService : Service() { private val serviceScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private val nostr by lazy { NostrManager.instance } + private var notificationJob: Job? = null override fun onBind(intent: Intent?): IBinder? = null @@ -46,10 +47,12 @@ class NostrForegroundService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - serviceScope.launch { + if (notificationJob?.isActive == true) return START_STICKY + + notificationJob = serviceScope.launch { try { Log.d("Coop", "Starting Nostr in background") - + // Create a database directory val dbDir = File(filesDir, "nostr") dbDir.mkdirs() @@ -82,10 +85,10 @@ class NostrForegroundService : Service() { Log.e("Coop", "Failed to start Nostr", e) } } + return START_STICKY } - @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannel() { val manager = getSystemService(NotificationManager::class.java) diff --git a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt index ee5a364..6e624ba 100644 --- a/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt +++ b/shared/src/commonMain/kotlin/su/reya/coop/Nostr.kt @@ -99,12 +99,15 @@ class Nostr { suspend fun emitContactListUpdate(contacts: List) = _contactListUpdates.emit(contacts) - suspend fun init(dbPath: String) { + suspend fun init( + dbPath: String, + logLevel: LogLevel = LogLevel.WARN + ) { try { if (isInitialized.value) return // Initialize the logger for nostr client - initLogger(LogLevel.DEBUG) + initLogger(logLevel) // Initialize the database and gossip instance val lmdb = NostrDatabase.lmdb(dbPath)