Files
clawdbot/apps/macos/Sources/Clawdis/NotificationManager.swift
2025-12-12 18:58:07 +00:00

53 lines
1.9 KiB
Swift

import ClawdisIPC
import Foundation
import Security
import UserNotifications
@MainActor
struct NotificationManager {
private static let hasTimeSensitiveEntitlement: Bool = {
guard let task = SecTaskCreateFromSelf(nil) else { return false }
let key = "com.apple.developer.usernotifications.time-sensitive" as CFString
guard let val = SecTaskCopyValueForEntitlement(task, key, nil) else { return false }
return (val as? Bool) == true
}()
func send(title: String, body: String, sound: String?, priority: NotificationPriority? = nil) async -> Bool {
let center = UNUserNotificationCenter.current()
let status = await center.notificationSettings()
if status.authorizationStatus == .notDetermined {
let granted = try? await center.requestAuthorization(options: [.alert, .sound, .badge])
if granted != true { return false }
} else if status.authorizationStatus != .authorized {
return false
}
let content = UNMutableNotificationContent()
content.title = title
content.body = body
if let soundName = sound, !soundName.isEmpty {
content.sound = UNNotificationSound(named: UNNotificationSoundName(soundName))
}
// Set interruption level based on priority
if let priority {
switch priority {
case .passive:
content.interruptionLevel = .passive
case .active:
content.interruptionLevel = .active
case .timeSensitive:
content.interruptionLevel = Self.hasTimeSensitiveEntitlement ? .timeSensitive : .active
}
}
let req = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
do {
try await center.add(req)
return true
} catch {
return false
}
}
}