mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 03:25:20 +00:00
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:
@@ -30,35 +30,37 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendLocalNotification(title: String, body: String, identifier: String) {
|
func sendLocalNotification(title: String, body: String, identifier: String) {
|
||||||
// Check if app is in foreground
|
// Check if app is in foreground (must be on main thread)
|
||||||
#if os(iOS)
|
DispatchQueue.main.async {
|
||||||
guard UIApplication.shared.applicationState != .active else {
|
#if os(iOS)
|
||||||
// App is active/foreground, skipping notification
|
guard UIApplication.shared.applicationState != .active else {
|
||||||
return
|
// App is active/foreground, skipping notification
|
||||||
}
|
return
|
||||||
// App state checked, sending notification
|
}
|
||||||
#elseif os(macOS)
|
// App state checked, sending notification
|
||||||
// On macOS, check if app is active
|
#elseif os(macOS)
|
||||||
guard !NSApplication.shared.isActive else {
|
// On macOS, check if app is active
|
||||||
// App is active/foreground, skipping notification
|
guard !NSApplication.shared.isActive else {
|
||||||
return
|
// App is active/foreground, skipping notification
|
||||||
}
|
return
|
||||||
// App is not active, sending notification
|
}
|
||||||
#endif
|
// 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
|
||||||
content.sound = .default
|
content.sound = .default
|
||||||
|
|
||||||
let request = UNNotificationRequest(
|
let request = UNNotificationRequest(
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
content: content,
|
content: content,
|
||||||
trigger: nil // Deliver immediately
|
trigger: nil // Deliver immediately
|
||||||
)
|
)
|
||||||
|
|
||||||
UNUserNotificationCenter.current().add(request) { _ in
|
UNUserNotificationCenter.current().add(request) { _ in
|
||||||
// Notification added
|
// Notification added
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,6 +89,8 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendNetworkAvailableNotification(peerCount: Int) {
|
func sendNetworkAvailableNotification(peerCount: Int) {
|
||||||
|
print("📱 sendNetworkAvailableNotification called with peerCount: \(peerCount)")
|
||||||
|
|
||||||
let title = "👥 bitchatters nearby!"
|
let title = "👥 bitchatters nearby!"
|
||||||
let body = peerCount == 1 ? "1 person around" : "\(peerCount) people around"
|
let body = peerCount == 1 ? "1 person around" : "\(peerCount) people around"
|
||||||
let identifier = "network-available-\(Date().timeIntervalSince1970)"
|
let identifier = "network-available-\(Date().timeIntervalSince1970)"
|
||||||
@@ -94,23 +98,26 @@ class NotificationService {
|
|||||||
print("📱 Sending network notification: \(body)")
|
print("📱 Sending network notification: \(body)")
|
||||||
|
|
||||||
// For network notifications, we want to show them even in foreground
|
// For network notifications, we want to show them even in foreground
|
||||||
let content = UNMutableNotificationContent()
|
// No app state check - let the notification delegate handle presentation
|
||||||
content.title = title
|
DispatchQueue.main.async {
|
||||||
content.body = body
|
let content = UNMutableNotificationContent()
|
||||||
content.sound = .default
|
content.title = title
|
||||||
content.interruptionLevel = .timeSensitive // Make it more prominent
|
content.body = body
|
||||||
|
content.sound = .default
|
||||||
|
content.interruptionLevel = .timeSensitive // Make it more prominent
|
||||||
|
|
||||||
let request = UNNotificationRequest(
|
let request = UNNotificationRequest(
|
||||||
identifier: identifier,
|
identifier: identifier,
|
||||||
content: content,
|
content: content,
|
||||||
trigger: nil // Deliver immediately
|
trigger: nil // Deliver immediately
|
||||||
)
|
)
|
||||||
|
|
||||||
UNUserNotificationCenter.current().add(request) { error in
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
if let error = error {
|
if let error = error {
|
||||||
print("📱 Error sending network notification: \(error)")
|
print("📱 Error sending network notification: \(error)")
|
||||||
} else {
|
} else {
|
||||||
print("📱 Network notification sent successfully")
|
print("📱 Network notification sent successfully")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user