diff --git a/bitchat/Services/BluetoothMeshService.swift b/bitchat/Services/BluetoothMeshService.swift index 4fb0484d..2db27492 100644 --- a/bitchat/Services/BluetoothMeshService.swift +++ b/bitchat/Services/BluetoothMeshService.swift @@ -1353,16 +1353,19 @@ class BluetoothMeshService: NSObject { // Check if we should reset the notification flag if let emptyTime = networkBecameEmptyTime { let currentNetworkSize = collectionsQueue.sync { activePeers.count } + print("📱 Checking network empty state: size=\(currentNetworkSize), emptyTime=\(emptyTime)") if currentNetworkSize == 0 { // Network is still empty, check if enough time has passed let timeSinceEmpty = Date().timeIntervalSince(emptyTime) if timeSinceEmpty >= networkEmptyResetDelay { // Reset the flag after network has been empty for the delay period + print("📱 Resetting notification flag after \(timeSinceEmpty)s of empty network") hasNotifiedNetworkAvailable = false // Keep the empty time set so we don't immediately notify again } } else { // Network is no longer empty, clear the empty time + print("📱 Network no longer empty, clearing empty time") networkBecameEmptyTime = nil } } @@ -1975,6 +1978,7 @@ class BluetoothMeshService: NSObject { // Check if we've already announced this peer let isFirstAnnounce = !announcedPeers.contains(senderID) + print("📱 Peer announce: \(senderID), isFirstAnnounce: \(isFirstAnnounce), wasInserted: \(wasInserted ?? false)") // Clean up stale peer IDs with the same nickname collectionsQueue.sync(flags: .barrier) { @@ -2115,6 +2119,8 @@ class BluetoothMeshService: NSObject { // Send network available notification if appropriate let currentNetworkSize = collectionsQueue.sync { self.activePeers.count } + print("📱 Network size after peer announce: \(currentNetworkSize), hasNotified: \(hasNotifiedNetworkAvailable)") + if currentNetworkSize > 0 { // Clear empty time since network is active networkBecameEmptyTime = nil @@ -2126,17 +2132,24 @@ class BluetoothMeshService: NSObject { if let lastNotification = lastNetworkNotificationTime { let timeSinceLastNotification = now.timeIntervalSince(lastNotification) + print("📱 Time since last notification: \(timeSinceLastNotification)s, cooldown: \(networkNotificationCooldown)s") if timeSinceLastNotification < networkNotificationCooldown { // Too soon to send another notification shouldSendNotification = false + print("📱 Skipping notification - cooldown period active") } } if shouldSendNotification { + print("📱 Triggering network availability notification for \(currentNetworkSize) peers") hasNotifiedNetworkAvailable = true lastNetworkNotificationTime = now NotificationService.shared.sendNetworkAvailableNotification(peerCount: currentNetworkSize) + } else { + print("📱 Not sending notification - cooldown active") } + } else { + print("📱 Not sending notification - already notified") } } diff --git a/bitchat/Services/NotificationService.swift b/bitchat/Services/NotificationService.swift index 74930709..e76bc932 100644 --- a/bitchat/Services/NotificationService.swift +++ b/bitchat/Services/NotificationService.swift @@ -20,9 +20,11 @@ class NotificationService { private init() {} func requestAuthorization() { - UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, _ in + UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in if granted { - // Permission granted + print("📱 Notification permission granted") + } else { + print("📱 Notification permission denied: \(error?.localizedDescription ?? "Unknown")") } } } @@ -87,8 +89,29 @@ class NotificationService { func sendNetworkAvailableNotification(peerCount: Int) { let title = "👥 bitchatters nearby!" let body = peerCount == 1 ? "1 person around" : "\(peerCount) people around" - let identifier = "network-available" + let identifier = "network-available-\(Date().timeIntervalSince1970)" - sendLocalNotification(title: title, body: body, identifier: identifier) + print("📱 Sending network notification: \(body)") + + // For network notifications, we want to show them even in foreground + let content = UNMutableNotificationContent() + content.title = title + content.body = body + content.sound = .default + content.interruptionLevel = .timeSensitive // Make it more prominent + + let request = UNNotificationRequest( + identifier: identifier, + content: content, + trigger: nil // Deliver immediately + ) + + UNUserNotificationCenter.current().add(request) { error in + if let error = error { + print("📱 Error sending network notification: \(error)") + } else { + print("📱 Network notification sent successfully") + } + } } }