prevent run multiple notification job

This commit is contained in:
2026-06-06 14:56:13 +07:00
parent 74a37320fe
commit 9f639baf1b
2 changed files with 12 additions and 6 deletions

View File

@@ -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,7 +47,9 @@ 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")
@@ -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)

View File

@@ -99,12 +99,15 @@ class Nostr {
suspend fun emitContactListUpdate(contacts: List<PublicKey>) =
_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)