Skip to content

Commit

Permalink
- added logs for ios notifications logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rodvar committed Dec 4, 2024
1 parent 0cb54c9 commit 7fe879c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
67 changes: 67 additions & 0 deletions iosClient/iosClient/iosClient.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
import SwiftUI
import domain
import presentation

@main
struct iosClient: App {

// private let notificationServiceController: NotificationServiceController?
private let notificationHandler = NotificationHandler()

init() {
DependenciesProviderHelper().doInitKoin()
// TODO not working
// notificationServiceController = get()
// if (notificationServiceController != nil) {
// notificationHandler.setNotificationHandlerImpl(notificationServiceController!)
// }
// // Request notification permissions and set the delegate
// configureNotifications()
}

var body: some Scene {
WindowGroup {
ContentView()
}
}

private func configureNotifications() {
let center = UNUserNotificationCenter.current()
center.delegate = notificationHandler // Custom delegate

center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if let error = error {
print("Error requesting notifications permission: \(error)")
}
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
if settings.authorizationStatus == .authorized {
print("Notifications are authorized.")
} else {
print("Notifications are not authorized.")
}
}

// simulation
let content = UNMutableNotificationContent()
content.title = "Test Notification"
content.body = "This is a test notification."
content.sound = .default

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Failed to add notification: \(error)")
} else {
print("Notification added successfully")
}
}
}
}

// Custom notification handler that conforms to `UNUserNotificationCenterDelegate`
class NotificationHandler: NSObject, UNUserNotificationCenterDelegate {
private var impl: NotificationServiceController?

func setNotificationHandlerImpl(_ controller: NotificationServiceController) {
self.impl = controller
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Show the notification while in foreground
print("Foreground notification received")
completionHandler([.banner, .sound, .badge])
impl?.pushNotification(title: "pepe", message: "parada")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
@Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING")
actual class NotificationServiceController: ServiceController, Logging {

companion object {
const val BACKGROUND_TASK_ID = "network.bisq.mobile.ios.backgroundtask"
}

private var isRunning = false

private val logScope = CoroutineScope(Dispatchers.Main)
Expand Down Expand Up @@ -41,7 +45,7 @@
logDebug("Notification permission granted.")

// TODO need to move to iOS callback -> didFinishLaunchingWithOptions
BGTaskScheduler.sharedScheduler.registerForTaskWithIdentifier(identifier = "network.bisq.mobile.ios.backgroundtask", usingQueue = null) { task ->
BGTaskScheduler.sharedScheduler.registerForTaskWithIdentifier(identifier = BACKGROUND_TASK_ID, usingQueue = null) { task ->
handleBackgroundTask(task as BGProcessingTask)
}
scheduleBackgroundTask()
Expand Down Expand Up @@ -92,7 +96,7 @@

@OptIn(ExperimentalForeignApi::class)
private fun scheduleBackgroundTask() {
val request = BGProcessingTaskRequest("com.yourapp.backgroundtask").apply {
val request = BGProcessingTaskRequest(BACKGROUND_TASK_ID).apply {
requiresNetworkConnectivity = true
}
BGTaskScheduler.sharedScheduler.submitTaskRequest(request, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import network.bisq.mobile.android.node.BuildNodeConfig
import network.bisq.mobile.client.shared.BuildConfig
import network.bisq.mobile.domain.data.BackgroundDispatcher
import network.bisq.mobile.domain.getPlatformInfo
import network.bisq.mobile.domain.service.controller.NotificationServiceController
import network.bisq.mobile.presentation.ui.AppPresenter
Expand Down

0 comments on commit 7fe879c

Please sign in to comment.