11import Foundation
22import Sub2APIStatusCore
3- import UserNotifications
3+ @ preconcurrency import UserNotifications
44
55@MainActor
66final class InsightNotifier {
@@ -56,12 +56,8 @@ final class InsightNotifier {
5656
5757 func requestAuthorization( ) async -> InsightNotificationAuthorization {
5858 let center = UNUserNotificationCenter . current ( )
59- do {
60- _ = try await center. requestAuthorization ( options: [ . alert, . sound] )
61- } catch {
62- return await authorization ( )
63- }
64- return await authorization ( )
59+ _ = await requestNotificationAuthorization ( center: center)
60+ return await currentAuthorization ( center: center)
6561 }
6662
6763 nonisolated static func authorization( from status: UNAuthorizationStatus ) -> InsightNotificationAuthorization {
@@ -79,33 +75,44 @@ final class InsightNotifier {
7975
8076 private func deliver( _ alert: InsightAlert ) async -> Bool {
8177 let center = UNUserNotificationCenter . current ( )
82- do {
83- let authorization = await currentAuthorization ( center: center)
84- guard authorization != . denied else {
78+ let authorization = await currentAuthorization ( center: center)
79+ guard authorization != . denied else {
80+ return false
81+ }
82+
83+ if authorization == . notDetermined {
84+ let granted = await requestNotificationAuthorization ( center: center)
85+ guard granted else {
8586 return false
8687 }
88+ }
89+
90+ let content = UNMutableNotificationContent ( )
91+ content. title = " Sub2API \( alert. title) "
92+ content. body = alert. body
93+ content. sound = . default
94+
95+ let request = UNNotificationRequest (
96+ identifier: " sub2api-insight- \( alert. fingerprint) " ,
97+ content: content,
98+ trigger: nil
99+ )
100+ return await addNotification ( request, center: center)
101+ }
87102
88- if authorization == . notDetermined {
89- let granted = try await center. requestAuthorization ( options: [ . alert, . sound] )
90- guard granted else {
91- return false
92- }
103+ private func requestNotificationAuthorization( center: UNUserNotificationCenter ) async -> Bool {
104+ await withCheckedContinuation { continuation in
105+ center. requestAuthorization ( options: [ . alert, . sound] ) { granted, _ in
106+ continuation. resume ( returning: granted)
93107 }
108+ }
109+ }
94110
95- let content = UNMutableNotificationContent ( )
96- content. title = " Sub2API \( alert. title) "
97- content. body = alert. body
98- content. sound = . default
99-
100- let request = UNNotificationRequest (
101- identifier: " sub2api-insight- \( alert. fingerprint) " ,
102- content: content,
103- trigger: nil
104- )
105- try await center. add ( request)
106- return true
107- } catch {
108- return false
111+ private func addNotification( _ request: UNNotificationRequest , center: UNUserNotificationCenter ) async -> Bool {
112+ await withCheckedContinuation { continuation in
113+ center. add ( request) { error in
114+ continuation. resume ( returning: error == nil )
115+ }
109116 }
110117 }
111118
0 commit comments