diff --git a/shared/domain/src/iosMain/kotlin/network/bisq/mobile/domain/service/controller/NotificationServiceController.ios.kt b/shared/domain/src/iosMain/kotlin/network/bisq/mobile/domain/service/controller/NotificationServiceController.ios.kt index f000abf1..d8eddada 100644 --- a/shared/domain/src/iosMain/kotlin/network/bisq/mobile/domain/service/controller/NotificationServiceController.ios.kt +++ b/shared/domain/src/iosMain/kotlin/network/bisq/mobile/domain/service/controller/NotificationServiceController.ios.kt @@ -3,6 +3,7 @@ package network.bisq.mobile.domain.service.controller import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import network.bisq.mobile.utils.Logging import platform.BackgroundTasks.* @@ -18,13 +19,14 @@ actual class NotificationServiceController: ServiceController, Logging { companion object { const val BACKGROUND_TASK_ID = "network.bisq.mobile.iosUC4273Y485" + const val CHECK_NOTIFICATIONS_DELAY = 15 * 10000L } private var isRunning = false private var isBackgroundTaskRegistered = false private val logScope = CoroutineScope(Dispatchers.Main) - init { + private fun setupDelegate() { val delegate = object : NSObject(), UNUserNotificationCenterDelegateProtocol { override fun userNotificationCenter( center: UNUserNotificationCenter, @@ -46,10 +48,10 @@ actual class NotificationServiceController: ServiceController, Logging { // Handle the response when the user taps the notification withCompletionHandler() } - } UNUserNotificationCenter.currentNotificationCenter().delegate = delegate + logDebug("Notification center delegate applied") } @@ -57,26 +59,27 @@ actual class NotificationServiceController: ServiceController, Logging { if (isRunning) { return } - registerBackgroundTask() logDebug("Starting background service") UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions( UNAuthorizationOptionAlert or UNAuthorizationOptionSound or UNAuthorizationOptionBadge ) { granted, error -> if (granted) { - println("Notification permission granted.") + logDebug("Notification permission granted.") + setupDelegate() + registerBackgroundTask() // Once permission is granted, you can start scheduling background tasks - scheduleBackgroundTask() - println("Background service started") + startBackgroundTaskLoop() + logDebug("Background service started") isRunning = true } else { - println("Notification permission denied: ${error?.localizedDescription}") + logDebug("Notification permission denied: ${error?.localizedDescription}") } } } actual override fun stopService() { BGTaskScheduler.sharedScheduler.cancelAllTaskRequests() - println("Background service stopped") + logDebug("Background service stopped") isRunning = false } @@ -89,17 +92,17 @@ actual class NotificationServiceController: ServiceController, Logging { val trigger = UNTimeIntervalNotificationTrigger.triggerWithTimeInterval(5.0, repeats = false) + val requestId = NSUUID().UUIDString val request = UNNotificationRequest.requestWithIdentifier( - NSUUID().UUIDString, // Generates a unique identifier + requestId, content, - trigger // Trigger can be set to null for immediate delivery + trigger ) - println("getting called every 10 sec") UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request) { error -> if (error != null) { - println("Error adding notification request: ${error.localizedDescription}") + logDebug("Error adding notification request: ${error.localizedDescription}") } else { - println("Notification added successfully") + logDebug("Notification $requestId added successfully") } } } @@ -110,11 +113,18 @@ actual class NotificationServiceController: ServiceController, Logging { } private fun handleBackgroundTask(task: BGProcessingTask) { - logDebug("Executing background task") - pushNotification("Background Notification", "This notification was triggered in the background") + task.setTaskCompletedWithSuccess(true) // Mark the task as completed + logDebug("Background task completed successfully") + scheduleBackgroundTask() // Re-schedule the next task + } - task.setTaskCompletedWithSuccess(true) -// scheduleBackgroundTask() // Reschedule if needed + private fun startBackgroundTaskLoop() { + CoroutineScope(Dispatchers.Default).launch { + while (isRunning) { + scheduleBackgroundTask() + delay(CHECK_NOTIFICATIONS_DELAY) // Check notifications every min + } + } } @OptIn(ExperimentalForeignApi::class) @@ -124,20 +134,24 @@ actual class NotificationServiceController: ServiceController, Logging { earliestBeginDate = NSDate(timeIntervalSinceReferenceDate = 10.0) } BGTaskScheduler.sharedScheduler.submitTaskRequest(request, null) - println("Background task scheduled") + logDebug("Background task scheduled") } +// fun setupTaskHandlers() { +// BGTaskScheduler.sharedScheduler.registerForTaskWithIdentifier(BACKGROUND_TASK_ID, BGProcessingTask.class, ::handleBackgroundTask) +// } + private fun logDebug(message: String) { logScope.launch { - log.d(message) + log.d { message } } } private fun registerBackgroundTask() { if (isBackgroundTaskRegistered) { - println("Background task is already registered.") + logDebug("Background task is already registered.") return } @@ -150,7 +164,6 @@ actual class NotificationServiceController: ServiceController, Logging { } isBackgroundTaskRegistered = true - // TODO logger is not working - println("Background task handler registered.") + logDebug("Background task handler registered.") } } diff --git a/shared/presentation/src/commonMain/kotlin/network/bisq/mobile/presentation/MainPresenter.kt b/shared/presentation/src/commonMain/kotlin/network/bisq/mobile/presentation/MainPresenter.kt index 193a585c..dde2192d 100644 --- a/shared/presentation/src/commonMain/kotlin/network/bisq/mobile/presentation/MainPresenter.kt +++ b/shared/presentation/src/commonMain/kotlin/network/bisq/mobile/presentation/MainPresenter.kt @@ -21,6 +21,10 @@ import kotlin.random.Random */ open class MainPresenter(private val notificationServiceController: NotificationServiceController) : BasePresenter(null), AppPresenter { + companion object { + const val PUSH_DELAY = 60000L + } + lateinit var navController: NavHostController private set @@ -58,8 +62,8 @@ open class MainPresenter(private val notificationServiceController: Notification val randomTitle = "Title ${Random.nextInt(1, 100)}" val randomMessage = "Message ${Random.nextInt(1, 100)}" notificationServiceController.pushNotification(randomTitle, randomMessage) - println("Pushed: $randomTitle - $randomMessage") - delay(10000) // 10 seconds + log.d {"Pushed: $randomTitle - $randomMessage" } + delay(PUSH_DELAY) // 1 min } } }