mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 14:05:18 +00:00
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:
@@ -30,36 +30,39 @@ 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)
|
||||||
|
let content = UNMutableNotificationContent()
|
||||||
|
content.title = title
|
||||||
|
content.body = body
|
||||||
|
content.sound = .default
|
||||||
|
|
||||||
|
let request = UNNotificationRequest(
|
||||||
|
identifier: identifier,
|
||||||
|
content: content,
|
||||||
|
trigger: nil // Deliver immediately
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check app state on main thread and send notification
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
guard UIApplication.shared.applicationState != .active else {
|
guard UIApplication.shared.applicationState != .active else {
|
||||||
// App is active/foreground, skipping notification
|
// App is active/foreground, skipping notification
|
||||||
|
print("📱 Skipping notification - app is in foreground")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// App state checked, sending notification
|
|
||||||
#elseif os(macOS)
|
#elseif os(macOS)
|
||||||
// On macOS, check if app is active
|
|
||||||
guard !NSApplication.shared.isActive else {
|
guard !NSApplication.shared.isActive else {
|
||||||
// App is active/foreground, skipping notification
|
// App is active/foreground, skipping notification
|
||||||
|
print("📱 Skipping notification - app is in foreground")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// App is not active, sending notification
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
let content = UNMutableNotificationContent()
|
// App is in background/inactive, send notification
|
||||||
content.title = title
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
content.body = body
|
if let error = error {
|
||||||
content.sound = .default
|
print("📱 Error sending local notification: \(error)")
|
||||||
|
}
|
||||||
let request = UNNotificationRequest(
|
|
||||||
identifier: identifier,
|
|
||||||
content: content,
|
|
||||||
trigger: nil // Deliver immediately
|
|
||||||
)
|
|
||||||
|
|
||||||
UNUserNotificationCenter.current().add(request) { _ in
|
|
||||||
// Notification added
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user