Repository Public
1import AppKit2import Foundation3import UserNotifications4import WorkspaceCore5import os67/// Native notifications for verified Claude events. Each notification deep-links to the pane via8/// `userInfo["pane"]`; the decision itself always stays in the Claude Code interface.9@MainActor10final class NotificationService: NSObject, UNUserNotificationCenterDelegate {11 static let logger = Logger(subsystem: "ai.spboucher.nexterm", category: "notifications")12 private weak var controller: WorkspaceController?13 private var authorized = false14 private var requested = false1516 init(controller: WorkspaceController) {17 self.controller = controller18 super.init()19 UNUserNotificationCenter.current().delegate = self20 }2122 func requestAuthorizationIfNeeded() {23 guard !requested else { return }24 requested = true25 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in26 Task { @MainActor in27 self?.authorized = granted28 if let error { Self.logger.error("authorization error: \(error.localizedDescription, privacy: .public)") }29 }30 }31 }3233 func notify(title: String, body: String, pane: PaneID?, identifier: String, sound: Bool = true) {34 requestAuthorizationIfNeeded()35 let content = UNMutableNotificationContent()36 content.title = title37 content.body = body38 if sound { content.sound = .default }39 if let pane { content.userInfo = ["pane": pane.rawValue.uuidString] }40 let request = UNNotificationRequest(identifier: identifier, content: content, trigger: nil)41 UNUserNotificationCenter.current().add(request) { error in42 if let error { Self.logger.error("notification failed: \(error.localizedDescription, privacy: .public)") }43 }44 }4546 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {47 let paneString = response.notification.request.content.userInfo["pane"] as? String48 await MainActor.run {49 NSApp.activate()50 if let paneString, let uuid = UUID(uuidString: paneString) {51 controller?.reveal(pane: PaneID(rawValue: uuid))52 }53 }54 }5556 nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {57 [.banner, .sound, .list]58 }59}60