|
| 1 | +# ForgePush |
| 2 | + |
| 3 | +Push notification management for iOS — permissions, tokens, silent and visible push routing. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- iOS 16+ |
| 8 | +- Swift 6.0+ |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +### Swift Package Manager |
| 13 | + |
| 14 | +Add ForgePush to your project via Xcode: |
| 15 | + |
| 16 | +1. **File > Add Package Dependencies...** |
| 17 | +2. Enter the repository URL |
| 18 | +3. Select the version rule and add to your target |
| 19 | + |
| 20 | +Or add it directly to your `Package.swift`: |
| 21 | + |
| 22 | +```swift |
| 23 | +dependencies: [ |
| 24 | + .package(url: "https://github.com/stefanprojchev/ForgePush.git", from: "1.0.0") |
| 25 | +] |
| 26 | +``` |
| 27 | + |
| 28 | +Import everything with `ForgePush`, or pick individual modules: |
| 29 | + |
| 30 | +```swift |
| 31 | +import ForgePush // all modules |
| 32 | +import ForgePushPermission // just permission |
| 33 | +import ForgePushToken // just token management |
| 34 | +import ForgeSilentPush // just silent push routing |
| 35 | +import ForgeVisiblePush // just visible push routing |
| 36 | +``` |
| 37 | + |
| 38 | +## Quick Start |
| 39 | + |
| 40 | +```swift |
| 41 | +import ForgePush |
| 42 | + |
| 43 | +// Request permission |
| 44 | +let permission = PushPermission() |
| 45 | +let granted = try await permission.request() |
| 46 | + |
| 47 | +// Register for remote notifications |
| 48 | +let tokenManager = PushTokenManager() |
| 49 | +await tokenManager.registerForRemoteNotifications() |
| 50 | + |
| 51 | +// Route silent pushes |
| 52 | +let silentRouter = SilentPushRouter( |
| 53 | + connectivity: connectivityObserver, |
| 54 | + protectedData: protectedDataObserver |
| 55 | +) |
| 56 | +silentRouter.addHandler(DataSyncHandler()) |
| 57 | + |
| 58 | +// Route tapped notifications |
| 59 | +let visibleRouter = VisiblePushRouter( |
| 60 | + connectivity: connectivityObserver, |
| 61 | + protectedData: protectedDataObserver |
| 62 | +) |
| 63 | +visibleRouter.addHandler(DeepLinkHandler()) |
| 64 | +``` |
| 65 | + |
| 66 | +## ForgePushPermission |
| 67 | + |
| 68 | +Wraps `UNUserNotificationCenter` for requesting and checking authorization: |
| 69 | + |
| 70 | +```swift |
| 71 | +let permission = PushPermission() |
| 72 | + |
| 73 | +// Request (defaults to alert, badge, sound) |
| 74 | +let granted = try await permission.request() |
| 75 | +let granted = try await permission.request([.alert, .sound, .criticalAlert]) |
| 76 | + |
| 77 | +// Check current status |
| 78 | +let status = await permission.status() // .authorized, .denied, .notDetermined, ... |
| 79 | + |
| 80 | +// Open Settings.app notification page |
| 81 | +await permission.openSettings() |
| 82 | +``` |
| 83 | + |
| 84 | +## ForgePushToken |
| 85 | + |
| 86 | +Manages the device push token lifecycle. Provides the current token as a hex string and an `AsyncStream` for changes: |
| 87 | + |
| 88 | +```swift |
| 89 | +let tokenManager = PushTokenManager() |
| 90 | +await tokenManager.registerForRemoteNotifications() |
| 91 | + |
| 92 | +// Current token |
| 93 | +if let token = tokenManager.token { |
| 94 | + await sendToServer(token) |
| 95 | +} |
| 96 | + |
| 97 | +// Stream token changes |
| 98 | +for await token in tokenManager.tokenStream { |
| 99 | + if let token { |
| 100 | + await sendToServer(token) |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Wire up the AppDelegate callbacks: |
| 106 | + |
| 107 | +```swift |
| 108 | +func application(_ app: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken token: Data) { |
| 109 | + tokenManager.didRegister(deviceToken: token) |
| 110 | +} |
| 111 | + |
| 112 | +func application(_ app: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { |
| 113 | + tokenManager.didFailToRegister(error: error) |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## ForgeSilentPush |
| 118 | + |
| 119 | +Routes silent push notifications to registered handlers concurrently. Each handler declares which payloads it matches and returns a `SilentPushResult` (`.newData`, `.noData`, `.failed`). The router aggregates results — most optimistic wins. |
| 120 | + |
| 121 | +```swift |
| 122 | +struct DataSyncHandler: SilentPushHandler { |
| 123 | + let id = "sync.data" |
| 124 | + |
| 125 | + func matchesPayload(_ payload: [AnyHashable: Any]) -> Bool { |
| 126 | + payload["type"] as? String == "sync" |
| 127 | + } |
| 128 | + |
| 129 | + func handle(_ payload: [AnyHashable: Any], context: SilentPushContext) async -> SilentPushResult { |
| 130 | + guard context.connectivity.isConnected else { return .failed } |
| 131 | + await performSync() |
| 132 | + return .newData |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// In AppDelegate |
| 137 | +func application(_ app: UIApplication, didReceiveRemoteNotification payload: [AnyHashable: Any], |
| 138 | + fetchCompletionHandler handler: @escaping (UIBackgroundFetchResult) -> Void) { |
| 139 | + silentRouter.handlePush(payload: payload, completionHandler: handler) |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +## ForgeVisiblePush |
| 144 | + |
| 145 | +Routes tapped push notifications to registered handlers concurrently: |
| 146 | + |
| 147 | +```swift |
| 148 | +struct DeepLinkHandler: VisiblePushHandler { |
| 149 | + let id = "deeplink" |
| 150 | + |
| 151 | + func matches(_ response: UNNotificationResponse) -> Bool { |
| 152 | + response.notification.request.content.userInfo["deeplink"] != nil |
| 153 | + } |
| 154 | + |
| 155 | + func handle(_ response: UNNotificationResponse, context: VisiblePushContext) async { |
| 156 | + let link = response.notification.request.content.userInfo["deeplink"] as! String |
| 157 | + await navigate(to: link) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// In UNUserNotificationCenterDelegate |
| 162 | +func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, |
| 163 | + withCompletionHandler handler: @escaping () -> Void) { |
| 164 | + visibleRouter.handleResponse(response, completionHandler: handler) |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +## Thread Safety |
| 169 | + |
| 170 | +All types are `Sendable`. `PushTokenManager` protects state with `LockedState`. `SilentPushRouter` and `VisiblePushRouter` protect handler lists with `LockedState` and dispatch work via `TaskGroup`. `PushPermission` is a stateless struct. |
| 171 | + |
| 172 | +## Forge Ecosystem |
| 173 | + |
| 174 | +ForgePush is part of the **Forge** family of Swift packages for iOS: |
| 175 | + |
| 176 | +| Package | Description | |
| 177 | +|---------|-------------| |
| 178 | +| [ForgeCore](https://github.com/stefanprojchev/ForgeCore) | Thread-safe utilities — `LockedState` and `SendableFileManager` | |
| 179 | +| [ForgeInject](https://github.com/stefanprojchev/ForgeInject) | Lightweight dependency injection with property wrapper | |
| 180 | +| [ForgeObservers](https://github.com/stefanprojchev/ForgeObservers) | Reactive system observers (connectivity, lifecycle, keyboard, and more) | |
| 181 | +| [ForgeStorage](https://github.com/stefanprojchev/ForgeStorage) | Type-safe persistence — key-value, file storage, and Keychain | |
| 182 | +| [ForgeBackgroundTasks](https://github.com/stefanprojchev/ForgeBackgroundTasks) | BGTaskScheduler registration, scheduling, and dispatch | |
| 183 | +| [ForgeLocation](https://github.com/stefanprojchev/ForgeLocation) | Location-based triggers — geofencing, significant changes, visits | |
| 184 | +| **ForgePush** | Push notification management — permissions, tokens, silent and visible routing | |
| 185 | +| [ForgeOrchestrator](https://github.com/stefanprojchev/ForgeOrchestrator) | Sequence, pipeline, and monitor orchestrators for iOS app flows | |
| 186 | + |
| 187 | +## License |
| 188 | + |
| 189 | +MIT License. See [LICENSE](LICENSE) for details. |
0 commit comments