|
| 1 | +import CioInternalCommon |
| 2 | +import Foundation |
| 3 | +#if canImport(UserNotifications) |
| 4 | +import UserNotifications |
| 5 | +#endif |
| 6 | + |
| 7 | +/// Abstraction for rich push (start and stop) so the coordinator can be tested with a mock. |
| 8 | +protocol RichPushRequestHandling { |
| 9 | + func startRequest(push: PushNotification, completionHandler: @escaping (PushNotification) -> Void) |
| 10 | + func stopAll() |
| 11 | +} |
| 12 | + |
| 13 | +/// Coordinates delivery metric request and rich push modification in parallel for the Notification Service Extension. |
| 14 | +/// Holds all NSE logic: delivery tracking (with logging), rich push, parallel run, single contentHandler call, and cancel on expiry. |
| 15 | +/// Create one coordinator and call `handle(request:withContentHandler:autoTrackDelivery:)` to process a notification; call `cancel()` on expiry. |
| 16 | +/// Dependencies are injected for easier testing. |
| 17 | +actor NSEPushCoordinator { |
| 18 | + private let deliveryTracker: RichPushDeliveryTracker |
| 19 | + private let pushLogger: PushNotificationLogger |
| 20 | + private let richPushHandler: RichPushRequestHandling |
| 21 | + private let httpClient: HttpClient |
| 22 | + |
| 23 | + private var originalContent: UNNotificationContent? |
| 24 | + private var contentHandler: ((UNNotificationContent) -> Void)? |
| 25 | + private var contentHandlerCalled = false |
| 26 | + private var richPushResult: PushNotification? |
| 27 | + |
| 28 | + init( |
| 29 | + deliveryTracker: RichPushDeliveryTracker, |
| 30 | + pushLogger: PushNotificationLogger, |
| 31 | + richPushHandler: RichPushRequestHandling, |
| 32 | + httpClient: HttpClient |
| 33 | + ) { |
| 34 | + self.deliveryTracker = deliveryTracker |
| 35 | + self.pushLogger = pushLogger |
| 36 | + self.richPushHandler = richPushHandler |
| 37 | + self.httpClient = httpClient |
| 38 | + } |
| 39 | + |
| 40 | + /// Handles the notification: runs delivery (if autoTrackDelivery) and rich push in parallel, waits for both, then calls `contentHandler` exactly once. |
| 41 | + /// Call this once per notification. Expects a CIO push (cioDelivery non-nil); if not, calls contentHandler with request.content and returns. |
| 42 | + /// Final content: rich push modified content if present, otherwise original. |
| 43 | + func handle( |
| 44 | + request: UNNotificationRequest, |
| 45 | + withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void, |
| 46 | + autoTrackDelivery: Bool |
| 47 | + ) async { |
| 48 | + let push = UNNotificationWrapper(notificationRequest: request) |
| 49 | + guard let info = push.cioDelivery else { |
| 50 | + contentHandler(request.content) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + self.originalContent = request.content |
| 55 | + self.contentHandler = contentHandler |
| 56 | + |
| 57 | + async let delivery: Void = trackDeliveryIfNeeded( |
| 58 | + autoTrackDelivery: autoTrackDelivery, |
| 59 | + token: info.token, |
| 60 | + deliveryID: info.id |
| 61 | + ) |
| 62 | + |
| 63 | + async let richPush: PushNotification? = runRichPush(push) |
| 64 | + |
| 65 | + let result = await richPush |
| 66 | + richPushResult = result |
| 67 | + |
| 68 | + _ = await delivery |
| 69 | + |
| 70 | + guard let originalContent = originalContent else { return } |
| 71 | + finishWithContent(contentFrom(result, originalContent: originalContent)) |
| 72 | + } |
| 73 | + |
| 74 | + /// Call when NSE time will expire. Signals cancellation to both operations and calls `contentHandler` exactly once with available content. |
| 75 | + func cancel() { |
| 76 | + guard !contentHandlerCalled else { return } |
| 77 | + guard let originalContent = originalContent else { return } |
| 78 | + |
| 79 | + contentHandlerCalled = true |
| 80 | + richPushHandler.stopAll() |
| 81 | + httpClient.cancel(finishTasks: false) |
| 82 | + |
| 83 | + contentHandler?(contentFrom(richPushResult, originalContent: originalContent)) |
| 84 | + } |
| 85 | + |
| 86 | + private func trackDeliveryIfNeeded( |
| 87 | + autoTrackDelivery: Bool, |
| 88 | + token: String, |
| 89 | + deliveryID: String |
| 90 | + ) async { |
| 91 | + guard autoTrackDelivery else { return } |
| 92 | + |
| 93 | + await withCheckedContinuation { (cont: CheckedContinuation<Void, Never>) in |
| 94 | + deliveryTracker.trackMetric( |
| 95 | + token: token, |
| 96 | + event: .delivered, |
| 97 | + deliveryId: deliveryID, |
| 98 | + timestamp: nil |
| 99 | + ) { result in |
| 100 | + switch result { |
| 101 | + case .success: |
| 102 | + self.pushLogger.logPushMetricTracked( |
| 103 | + deliveryId: deliveryID, |
| 104 | + event: Metric.delivered.rawValue |
| 105 | + ) |
| 106 | + case .failure(let error): |
| 107 | + self.pushLogger.logPushMetricTrackingFailed( |
| 108 | + deliveryId: deliveryID, |
| 109 | + event: Metric.delivered.rawValue, |
| 110 | + error: error |
| 111 | + ) |
| 112 | + } |
| 113 | + cont.resume() |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private func runRichPush(_ push: PushNotification) async -> PushNotification? { |
| 119 | + await withCheckedContinuation { cont in |
| 120 | + richPushHandler.startRequest(push: push) { composed in |
| 121 | + cont.resume(returning: composed) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private func finishWithContent(_ content: UNNotificationContent) { |
| 127 | + guard !contentHandlerCalled else { return } |
| 128 | + contentHandlerCalled = true |
| 129 | + contentHandler?(content) |
| 130 | + } |
| 131 | + |
| 132 | + private func contentFrom( |
| 133 | + _ push: PushNotification?, |
| 134 | + originalContent: UNNotificationContent |
| 135 | + ) -> UNNotificationContent { |
| 136 | + if let wrapper = push as? UNNotificationWrapper { |
| 137 | + return wrapper.notificationContent |
| 138 | + } |
| 139 | + return originalContent |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +extension RichPushRequestHandler: RichPushRequestHandling {} |
0 commit comments