| 1 | import Foundation |
| 2 | import UserNotifications |
| 3 | #if os(iOS) |
| 4 | import UIKit |
| 5 | #else |
| 6 | import AppKit |
| 7 | #endif |
| 8 | |
| 9 | /// What the app model needs from the system's notifications. The apps use |
| 10 | /// `SystemNotifications`; tests use a fake. |
| 11 | @MainActor |
| 12 | public protocol NotificationSystem: AnyObject { |
| 13 | /// Asks to show alerts, badges and sounds (the system asks only once), |
| 14 | /// then registers with APNs for this device's token. |
| 15 | func askAndRegister() |
| 16 | /// The app icon's badge: the person's Needs you count. |
| 17 | func setBadge(_ count: Int) |
| 18 | /// Signed out: stop receiving pushes at all, and clear what was shown, |
| 19 | /// whether or not the server heard of the sign-out. |
| 20 | func unregister() |
| 21 | } |
| 22 | |
| 23 | /// Asking for permission, then registering with APNs, so that a sign-out |
| 24 | /// while the permission prompt is still open is never undone by it: each |
| 25 | /// request is stamped, and `stop` makes every earlier one stale. |
| 26 | @MainActor |
| 27 | public final class RemoteRegistration { |
| 28 | private let authorize: @MainActor () async -> Void |
| 29 | private let register: @MainActor () -> Void |
| 30 | private let unregister: @MainActor () -> Void |
| 31 | private var stamp = 0 |
| 32 | public init(authorize: @escaping @MainActor () async -> Void, register: @escaping @MainActor () -> Void, unregister: @escaping @MainActor () -> Void) { |
| 33 | self.authorize = authorize; self.register = register; self.unregister = unregister |
| 34 | } |
| 35 | /// Stamped now, before any waiting: a sign-out straight after this |
| 36 | /// call already makes it stale, even if its work has not started. |
| 37 | @discardableResult |
| 38 | public func request() -> Task<Void, Never> { |
| 39 | stamp += 1 |
| 40 | let mine = stamp |
| 41 | return Task { @MainActor in |
| 42 | guard mine == self.stamp else { return } |
| 43 | await self.authorize() |
| 44 | guard mine == self.stamp else { return } |
| 45 | self.register() |
| 46 | } |
| 47 | } |
| 48 | public func stop() { |
| 49 | stamp += 1 |
| 50 | unregister() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// UserNotifications registration and foreground, unlock-required responses. |
| 55 | /// Old and unknown payloads retain open-only navigation. |
| 56 | @MainActor |
| 57 | public final class SystemNotifications: NSObject, NotificationSystem, UNUserNotificationCenterDelegate { |
| 58 | public static let needsYouCategory = "needs_you" |
| 59 | /// Called with where a tapped notification goes. |
| 60 | public var onResponse: ((NotificationResponse) -> Void)? |
| 61 | public var onRoute: ((PushRoute) -> Void)? |
| 62 | private let remote = RemoteRegistration( |
| 63 | authorize: { _ = try? await UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) }, |
| 64 | register: { |
| 65 | #if os(iOS) |
| 66 | UIApplication.shared.registerForRemoteNotifications() |
| 67 | #else |
| 68 | NSApplication.shared.registerForRemoteNotifications() |
| 69 | #endif |
| 70 | }, |
| 71 | unregister: { |
| 72 | let center = UNUserNotificationCenter.current() |
| 73 | center.removeAllDeliveredNotifications() |
| 74 | center.removeAllPendingNotificationRequests() |
| 75 | #if os(iOS) |
| 76 | UIApplication.shared.unregisterForRemoteNotifications() |
| 77 | #else |
| 78 | NSApplication.shared.unregisterForRemoteNotifications() |
| 79 | #endif |
| 80 | }) |
| 81 | |
| 82 | public override init() { |
| 83 | super.init() |
| 84 | let center = UNUserNotificationCenter.current() |
| 85 | center.delegate = self |
| 86 | center.setNotificationCategories(NotificationCategories.all) |
| 87 | } |
| 88 | |
| 89 | public func askAndRegister() { |
| 90 | remote.request() |
| 91 | } |
| 92 | |
| 93 | public func setBadge(_ count: Int) { |
| 94 | UNUserNotificationCenter.current().setBadgeCount(max(0, count)) |
| 95 | } |
| 96 | |
| 97 | public func unregister() { remote.stop() } |
| 98 | |
| 99 | /// In front, a card or a finished task still shows; a reply goes to the |
| 100 | /// list only, since the conversation is right there. |
| 101 | public nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions { |
| 102 | let route = PushRoute(userInfo: notification.request.content.userInfo) |
| 103 | return route == .conversation ? [.list, .badge] : [.banner, .list, .sound, .badge] |
| 104 | } |
| 105 | |
| 106 | public nonisolated func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async { |
| 107 | guard response.actionIdentifier != UNNotificationDismissActionIdentifier else { return } |
| 108 | let content = response.notification.request.content |
| 109 | if let action = NotificationResponse(userInfo: content.userInfo, category: content.categoryIdentifier, |
| 110 | action: response.actionIdentifier, text: (response as? UNTextInputNotificationResponse)?.userText) { |
| 111 | await MainActor.run { self.onResponse?(action) } |
| 112 | } else if let route = PushRoute(userInfo: content.userInfo) { |
| 113 | await MainActor.run { self.onRoute?(route) } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |