DomainDig/LocalNotificationService.swift

9384a668bf36f23f71e25d05b490d1040d308845
domain-dig/DomainDig/LocalNotificationService.swift history · blame · raw

89 lines · 2939 bytes

 1import Foundation
 2import UserNotifications
 3
 4@MainActor
 5final class LocalNotificationService {
 6    static let shared = LocalNotificationService()
 7
 8    private init() {}
 9
10    func configureForegroundPresentation() {
11        UNUserNotificationCenter.current().delegate = NotificationCenterDelegate.shared
12    }
13
14    func requestAuthorizationIfNeeded() async -> Bool {
15        let center = UNUserNotificationCenter.current()
16        let settings = await center.notificationSettings()
17
18        switch settings.authorizationStatus {
19        case .authorized, .provisional, .ephemeral:
20            return true
21        case .notDetermined:
22            return (try? await center.requestAuthorization(options: [.alert, .badge, .sound])) ?? false
23        case .denied:
24            return false
25        @unknown default:
26            return false
27        }
28    }
29
30    func notifyDomainEvent(domain: String, message: String, severity: ChangeSeverity) async {
31        await schedule(
32            identifier: "domain-change-\(domain)",
33            title: domain,
34            body: message,
35            interruptionLevel: severity == .high ? .timeSensitive : .active
36        )
37    }
38
39    func notifyCertificateWarning(domain: String, daysRemaining: Int) async {
40        await schedule(
41            identifier: "cert-warning-\(domain)",
42            title: domain,
43            body: "Certificate expires in \(daysRemaining) days",
44            interruptionLevel: .timeSensitive
45        )
46    }
47
48    func notifySweepComplete(summary: BatchSweepSummary) async {
49        let body = "\(summary.changedDomains) changed, \(summary.warningDomains) warnings, \(summary.unchangedDomains) unchanged"
50        await schedule(
51            identifier: "sweep-complete",
52            title: summary.source == .watchlistRefresh ? "Check All Complete" : "Batch Complete",
53            body: body,
54            interruptionLevel: .active
55        )
56    }
57
58    private func schedule(
59        identifier: String,
60        title: String,
61        body: String,
62        interruptionLevel: UNNotificationInterruptionLevel
63    ) async {
64        let content = UNMutableNotificationContent()
65        content.title = title
66        content.body = body
67        content.sound = .default
68        content.interruptionLevel = interruptionLevel
69
70        let request = UNNotificationRequest(
71            identifier: identifier,
72            content: content,
73            trigger: UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
74        )
75
76        try? await UNUserNotificationCenter.current().add(request)
77    }
78}
79
80private final class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
81    static let shared = NotificationCenterDelegate()
82
83    func userNotificationCenter(
84        _ center: UNUserNotificationCenter,
85        willPresent notification: UNNotification
86    ) async -> UNNotificationPresentationOptions {
87        [.banner, .list, .sound]
88    }
89}