macOS: split AppMain into focused modules
This commit is contained in:
31
apps/macos/Sources/Clawdis/NotificationManager.swift
Normal file
31
apps/macos/Sources/Clawdis/NotificationManager.swift
Normal file
@@ -0,0 +1,31 @@
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
|
||||
@MainActor
|
||||
struct NotificationManager {
|
||||
func send(title: String, body: String, sound: String?) 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))
|
||||
}
|
||||
|
||||
let req = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
||||
do {
|
||||
try await center.add(req)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user