Fix thread safety issue for notifications

- Wrap UIApplication.applicationState access in DispatchQueue.main.async
- Network notifications bypass app state check entirely
- Added more detailed logging for debugging
- Notifications now handled properly from background threads
This commit is contained in:
jack
2025-07-22 16:41:43 +02:00
parent 537d489daa
commit 9f42d91903
+48 -41
View File
@@ -30,35 +30,37 @@ class NotificationService {
}
func sendLocalNotification(title: String, body: String, identifier: String) {
// Check if app is in foreground
#if os(iOS)
guard UIApplication.shared.applicationState != .active else {
// App is active/foreground, skipping notification
return
}
// App state checked, sending notification
#elseif os(macOS)
// On macOS, check if app is active
guard !NSApplication.shared.isActive else {
// App is active/foreground, skipping notification
return
}
// App is not active, sending notification
#endif
// Check if app is in foreground (must be on main thread)
DispatchQueue.main.async {
#if os(iOS)
guard UIApplication.shared.applicationState != .active else {
// App is active/foreground, skipping notification
return
}
// App state checked, sending notification
#elseif os(macOS)
// On macOS, check if app is active
guard !NSApplication.shared.isActive else {
// App is active/foreground, skipping notification
return
}
// App is not active, sending notification
#endif
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
let request = UNNotificationRequest(
identifier: identifier,
content: content,
trigger: nil // Deliver immediately
)
let request = UNNotificationRequest(
identifier: identifier,
content: content,
trigger: nil // Deliver immediately
)
UNUserNotificationCenter.current().add(request) { _ in
// Notification added
UNUserNotificationCenter.current().add(request) { _ in
// Notification added
}
}
}
@@ -87,6 +89,8 @@ class NotificationService {
}
func sendNetworkAvailableNotification(peerCount: Int) {
print("📱 sendNetworkAvailableNotification called with peerCount: \(peerCount)")
let title = "👥 bitchatters nearby!"
let body = peerCount == 1 ? "1 person around" : "\(peerCount) people around"
let identifier = "network-available-\(Date().timeIntervalSince1970)"
@@ -94,23 +98,26 @@ class NotificationService {
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
// No app state check - let the notification delegate handle presentation
DispatchQueue.main.async {
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
)
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")
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("📱 Error sending network notification: \(error)")
} else {
print("📱 Network notification sent successfully")
}
}
}
}