Fix remaining thread safety issues in notifications

- Favorite notifications now bypass sendLocalNotification to avoid thread issues
- All notification code properly wrapped in DispatchQueue.main.async
- Added logging for favorite notifications
- Network and favorite notifications now work independently
This commit is contained in:
jack
2025-07-22 16:44:34 +02:00
parent 9f42d91903
commit 7b81dd4c0d
+44 -23
View File
@@ -30,23 +30,7 @@ class NotificationService {
} }
func sendLocalNotification(title: String, body: String, identifier: String) { func sendLocalNotification(title: String, body: String, identifier: String) {
// Check if app is in foreground (must be on main thread) // Create notification content first (thread-safe)
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() let content = UNMutableNotificationContent()
content.title = title content.title = title
content.body = body content.body = body
@@ -58,8 +42,27 @@ class NotificationService {
trigger: nil // Deliver immediately trigger: nil // Deliver immediately
) )
UNUserNotificationCenter.current().add(request) { _ in // Check app state on main thread and send notification
// Notification added DispatchQueue.main.async {
#if os(iOS)
guard UIApplication.shared.applicationState != .active else {
// App is active/foreground, skipping notification
print("📱 Skipping notification - app is in foreground")
return
}
#elseif os(macOS)
guard !NSApplication.shared.isActive else {
// App is active/foreground, skipping notification
print("📱 Skipping notification - app is in foreground")
return
}
#endif
// App is in background/inactive, send notification
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("📱 Error sending local notification: \(error)")
}
} }
} }
} }
@@ -81,11 +84,29 @@ class NotificationService {
} }
func sendFavoriteOnlineNotification(nickname: String) { func sendFavoriteOnlineNotification(nickname: String) {
let title = "\(nickname) is online!" print("📱 sendFavoriteOnlineNotification called for: \(nickname)")
let body = "wanna get in there?"
let identifier = "favorite-online-\(UUID().uuidString)"
sendLocalNotification(title: title, body: body, identifier: identifier) // Send directly without checking app state for favorites
DispatchQueue.main.async {
let content = UNMutableNotificationContent()
content.title = "\(nickname) is online!"
content.body = "wanna get in there?"
content.sound = .default
let request = UNNotificationRequest(
identifier: "favorite-online-\(UUID().uuidString)",
content: content,
trigger: nil
)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("📱 Error sending favorite notification: \(error)")
} else {
print("📱 Favorite notification sent successfully")
}
}
}
} }
func sendNetworkAvailableNotification(peerCount: Int) { func sendNetworkAvailableNotification(peerCount: Int) {