mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 18:45:20 +00:00
Enable notifications on macOS
- Add macOS support to NotificationService - Use NSApplication.shared.isActive to check app state on macOS - Move notification logic outside of iOS-only block in ChatViewModel - Notifications now work for both mentions and private messages on macOS - Keep haptic feedback iOS-only as it's not available on macOS
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
import UserNotifications
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
import UIKit
|
import UIKit
|
||||||
import UserNotifications
|
#elseif os(macOS)
|
||||||
#else
|
import AppKit
|
||||||
import UserNotifications
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class NotificationService {
|
class NotificationService {
|
||||||
@@ -12,7 +12,6 @@ class NotificationService {
|
|||||||
private init() {}
|
private init() {}
|
||||||
|
|
||||||
func requestAuthorization() {
|
func requestAuthorization() {
|
||||||
#if os(iOS)
|
|
||||||
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
||||||
if granted {
|
if granted {
|
||||||
print("[NOTIFICATIONS] Permission granted")
|
print("[NOTIFICATIONS] Permission granted")
|
||||||
@@ -20,18 +19,24 @@ class NotificationService {
|
|||||||
print("[NOTIFICATIONS] Permission error: \(error)")
|
print("[NOTIFICATIONS] Permission error: \(error)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendLocalNotification(title: String, body: String, identifier: String) {
|
func sendLocalNotification(title: String, body: String, identifier: String) {
|
||||||
|
// Check if app is in foreground
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
// Send notification if app is not active (background or inactive)
|
|
||||||
guard UIApplication.shared.applicationState != .active else {
|
guard UIApplication.shared.applicationState != .active else {
|
||||||
print("[NOTIFICATIONS] App is active/foreground, skipping notification")
|
print("[NOTIFICATIONS] App is active/foreground, skipping notification")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
print("[NOTIFICATIONS] App state: \(UIApplication.shared.applicationState.rawValue), sending notification")
|
print("[NOTIFICATIONS] App state: \(UIApplication.shared.applicationState.rawValue), sending notification")
|
||||||
|
#elseif os(macOS)
|
||||||
|
// On macOS, check if app is active
|
||||||
|
guard !NSApplication.shared.isActive else {
|
||||||
|
print("[NOTIFICATIONS] App is active/foreground, skipping notification")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
print("[NOTIFICATIONS] App is not active, sending notification")
|
||||||
|
#endif
|
||||||
|
|
||||||
let content = UNMutableNotificationContent()
|
let content = UNMutableNotificationContent()
|
||||||
content.title = title
|
content.title = title
|
||||||
@@ -51,7 +56,6 @@ class NotificationService {
|
|||||||
print("[NOTIFICATIONS] Notification sent: \(title)")
|
print("[NOTIFICATIONS] Notification sent: \(title)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendMentionNotification(from sender: String, message: String) {
|
func sendMentionNotification(from sender: String, message: String) {
|
||||||
|
|||||||
@@ -430,14 +430,19 @@ extension ChatViewModel: BitchatDelegate {
|
|||||||
messages.append(message)
|
messages.append(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
#if os(iOS)
|
|
||||||
// Check if we're mentioned
|
// Check if we're mentioned
|
||||||
let isMentioned = message.mentions?.contains(nickname) ?? false
|
let isMentioned = message.mentions?.contains(nickname) ?? false
|
||||||
|
|
||||||
// Send notifications for mentions and private messages when app is in background
|
// Send notifications for mentions and private messages when app is in background
|
||||||
if isMentioned && message.sender != nickname {
|
if isMentioned && message.sender != nickname {
|
||||||
NotificationService.shared.sendMentionNotification(from: message.sender, message: message.content)
|
NotificationService.shared.sendMentionNotification(from: message.sender, message: message.content)
|
||||||
|
} else if message.isPrivate && message.sender != nickname {
|
||||||
|
NotificationService.shared.sendPrivateMessageNotification(from: message.sender, message: message.content)
|
||||||
|
}
|
||||||
|
|
||||||
|
#if os(iOS)
|
||||||
|
// Haptic feedback for iOS only
|
||||||
|
if isMentioned && message.sender != nickname {
|
||||||
// Very prominent haptic for @mentions - triple tap with heavy impact
|
// Very prominent haptic for @mentions - triple tap with heavy impact
|
||||||
let impactFeedback = UIImpactFeedbackGenerator(style: .heavy)
|
let impactFeedback = UIImpactFeedbackGenerator(style: .heavy)
|
||||||
impactFeedback.prepare()
|
impactFeedback.prepare()
|
||||||
@@ -450,8 +455,6 @@ extension ChatViewModel: BitchatDelegate {
|
|||||||
impactFeedback.impactOccurred()
|
impactFeedback.impactOccurred()
|
||||||
}
|
}
|
||||||
} else if message.isPrivate && message.sender != nickname {
|
} else if message.isPrivate && message.sender != nickname {
|
||||||
NotificationService.shared.sendPrivateMessageNotification(from: message.sender, message: message.content)
|
|
||||||
|
|
||||||
// Heavy haptic for private messages - more pronounced
|
// Heavy haptic for private messages - more pronounced
|
||||||
let impactFeedback = UIImpactFeedbackGenerator(style: .heavy)
|
let impactFeedback = UIImpactFeedbackGenerator(style: .heavy)
|
||||||
impactFeedback.prepare()
|
impactFeedback.prepare()
|
||||||
|
|||||||
Reference in New Issue
Block a user