1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
import Foundation
import UserNotifications
@MainActor
final class LocalNotificationService {
static let shared = LocalNotificationService()
private init() {}
static let domainUserInfoKey = "domain"
static let domainCategoryIdentifier = "domain-event"
static let reinspectActionIdentifier = "reinspect"
func configureForegroundPresentation() {
let center = UNUserNotificationCenter.current()
center.delegate = NotificationCenterDelegate.shared
let reinspect = UNNotificationAction(
identifier: Self.reinspectActionIdentifier,
title: "Re-inspect",
options: []
)
center.setNotificationCategories([
UNNotificationCategory(
identifier: Self.domainCategoryIdentifier,
actions: [reinspect],
intentIdentifiers: [],
options: []
)
])
}
func requestAuthorizationIfNeeded() async -> Bool {
let center = UNUserNotificationCenter.current()
let settings = await center.notificationSettings()
switch settings.authorizationStatus {
case .authorized, .provisional, .ephemeral:
return true
case .notDetermined:
return (try? await center.requestAuthorization(options: [.alert, .badge, .sound])) ?? false
case .denied:
return false
@unknown default:
return false
}
}
func isAuthorizedForAlerts() async -> Bool {
let settings = await UNUserNotificationCenter.current().notificationSettings()
switch settings.authorizationStatus {
case .authorized, .provisional, .ephemeral:
return true
case .denied, .notDetermined:
return false
@unknown default:
return false
}
}
func notifyDomainEvent(domain: String, message: String, severity: ChangeSeverity) async {
await schedule(
identifier: "domain-change-\(domain)",
title: domain,
body: message,
interruptionLevel: severity == .high ? .timeSensitive : .active,
domain: domain
)
}
func notifyCertificateWarning(domain: String, daysRemaining: Int) async {
await schedule(
identifier: "cert-warning-\(domain)",
title: domain,
body: "Certificate expires in \(daysRemaining) days",
interruptionLevel: .timeSensitive,
domain: domain
)
}
func notifyMonitoringAlert(
domain: String,
message: String,
severity: MonitoringAlertSeverity
) async {
let interruptionLevel: UNNotificationInterruptionLevel
switch severity {
case .critical:
interruptionLevel = .timeSensitive
case .warning, .info:
interruptionLevel = .active
}
await schedule(
identifier: "monitoring-\(domain)-\(UUID().uuidString)",
title: domain,
body: message,
interruptionLevel: interruptionLevel,
domain: domain
)
}
func notifyMonitoringSummary(
domain: String,
alerts: [MonitoringPendingAlert]
) async {
let summary = alerts
.sorted { $0.detectedAt < $1.detectedAt }
.prefix(2)
.map(\.message)
.joined(separator: " • ")
let body: String
if alerts.count <= 1 {
body = alerts.first?.message ?? "Monitoring change detected"
} else if summary.isEmpty {
body = "\(alerts.count) monitoring changes detected"
} else {
body = "\(alerts.count) monitoring changes: \(summary)"
}
let severity = alerts.map(\.severity).max() ?? .info
let interruptionLevel: UNNotificationInterruptionLevel = severity == .critical ? .timeSensitive : .active
await schedule(
identifier: "monitoring-summary-\(domain)-\(UUID().uuidString)",
title: domain,
body: body,
interruptionLevel: interruptionLevel,
domain: domain
)
}
func notifySweepComplete(summary: BatchSweepSummary) async {
let body = "\(summary.changedDomains) changed, \(summary.warningDomains) warnings, \(summary.unchangedDomains) unchanged"
await schedule(
identifier: "sweep-complete",
title: summary.source == .watchlistRefresh ? "Check All Complete" : "Batch Complete",
body: body,
interruptionLevel: .active
)
}
func notifyScheduledReportReady(domainCount: Int) async {
await schedule(
identifier: "scheduled-report-\(UUID().uuidString)",
title: "Scheduled Report Ready",
body: "Report generated for \(domainCount) domain\(domainCount == 1 ? "" : "s").",
interruptionLevel: .active
)
}
func clearAllNotifications() async {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
center.removeAllDeliveredNotifications()
}
private func schedule(
identifier: String,
title: String,
body: String,
interruptionLevel: UNNotificationInterruptionLevel,
domain: String? = nil
) async {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
content.interruptionLevel = interruptionLevel
if let domain {
// Group alerts per domain and let taps/actions route back into it.
content.threadIdentifier = domain
content.userInfo = [Self.domainUserInfoKey: domain]
content.categoryIdentifier = Self.domainCategoryIdentifier
}
let request = UNNotificationRequest(
identifier: identifier,
content: content,
trigger: UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
)
try? await UNUserNotificationCenter.current().add(request)
}
}
private final class NotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
static let shared = NotificationCenterDelegate()
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification
) async -> UNNotificationPresentationOptions {
[.banner, .list, .sound]
}
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) async {
let userInfo = response.notification.request.content.userInfo
guard let domain = userInfo[LocalNotificationService.domainUserInfoKey] as? String,
!domain.isEmpty
else { return }
let action: DomainDigDeepLink.Action
switch response.actionIdentifier {
case LocalNotificationService.reinspectActionIdentifier:
action = .inspect(domain)
default:
// Default tap: open the tracked domain's detail.
action = .detail(domain)
}
await MainActor.run {
DomainDigIntentRouter.shared.pendingAction = action
}
}
}
|