Skip to content

Commit eb6cda8

Browse files
will-osborneclaude
andcommitted
Fix macOS notifications: add delegate, use NSApp.isActive, log errors
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 33cf5b1 commit eb6cda8

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

desktop/Orbitor/Orbitor/State/ChatState.swift

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1+
import AppKit
12
import Foundation
23
import UserNotifications
34

5+
/// Delegate that allows notifications to be displayed even when the app is frontmost.
6+
final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
7+
static let shared = NotificationDelegate()
8+
9+
func userNotificationCenter(
10+
_ center: UNUserNotificationCenter,
11+
willPresent notification: UNNotification,
12+
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
13+
) {
14+
completionHandler([.banner, .sound])
15+
}
16+
}
17+
418
@Observable
519
final class ChatState {
620
/// The visible messages — a tail window of allMessages.
@@ -39,17 +53,30 @@ final class ChatState {
3953
}
4054

4155
private func requestNotificationPermission() {
42-
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { _, _ in }
56+
let center = UNUserNotificationCenter.current()
57+
center.delegate = NotificationDelegate.shared
58+
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
59+
if let error {
60+
print("[Notifications] authorization error: \(error)")
61+
} else if !granted {
62+
print("[Notifications] user denied notification permission")
63+
}
64+
}
4365
}
4466

4567
private func postNotification(title: String, body: String) {
46-
guard !isAppFocused else { return }
68+
// Skip if the app window is currently active
69+
guard !NSApp.isActive else { return }
4770
let content = UNMutableNotificationContent()
4871
content.title = title
4972
content.body = body
5073
content.sound = .default
5174
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
52-
UNUserNotificationCenter.current().add(request)
75+
UNUserNotificationCenter.current().add(request) { error in
76+
if let error {
77+
print("[Notifications] delivery error: \(error)")
78+
}
79+
}
5380
}
5481

5582
func updateBaseURL(_ url: URL) {

0 commit comments

Comments
 (0)