This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PushHelper.swift
344 lines (298 loc) · 12.2 KB
/
PushHelper.swift
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import Foundation
private let completionHandlerIdKey = "completionHandlerId"
extension UNAuthorizationStatus {
var description : String {
switch self {
case .notDetermined:
return "NotDetermined"
case .denied:
return "Denied"
case .authorized:
return "Authorized"
case .provisional:
return "Provisional"
case .ephemeral:
return "Ephemeral"
@unknown default:
return "NotDetermined"
}
}
}
enum NativeEvent {
case tokenReceived
case notificationOpened
case launchNotificationOpened
case backgroundMessageReceived
case foregroundMessageReceived
var key: String {
switch(self) {
case .tokenReceived:
return "TOKEN_RECEIVED"
case .notificationOpened:
return "NOTIFICATION_OPENED"
case .launchNotificationOpened:
return "LAUNCH_NOTIFICATION_OPENED"
case .backgroundMessageReceived:
return "BACKGROUND_MESSAGE_RECEIVED"
case .foregroundMessageReceived:
return "FOREGROUND_MESSAGE_RECEIVED"
}
}
var name: String {
switch(self) {
case .tokenReceived:
return "TokenReceived"
case .notificationOpened:
return "NotificationOpened"
case .launchNotificationOpened:
return "LaunchNotificationOpened"
case .foregroundMessageReceived:
return "ForegroundMessageReceived"
case .backgroundMessageReceived:
return "BackgroundMessageReceived"
}
}
}
struct PushEvent {
var type: NativeEvent
var payload: Any
}
class PushEventManager {
static let shared = PushEventManager()
private var eventQueue: [PushEvent] = []
private var sendEvent: ((PushEvent) -> Void)?
func setSendEvent(sendEvent: @escaping (PushEvent) -> Void) {
self.sendEvent = sendEvent
flushQueuedEvents()
}
func sendEventToJS(_ event: PushEvent) {
if let sendEvent = self.sendEvent {
sendEvent(event)
} else {
eventQueue.append(event)
}
}
private func flushQueuedEvents() {
while (!eventQueue.isEmpty) {
sendEventToJS(eventQueue.removeFirst())
}
}
}
final class PushNotificationManager {
static let shared = PushNotificationManager()
private var cachedDeviceToken: String?
private var launchNotification: [AnyHashable: Any]?
private var remoteNotificationCompletionHandlers: [String: (UIBackgroundFetchResult) -> Void] = [:]
private let sharedEventManager: PushEventManager
init() {
sharedEventManager = PushEventManager.shared
setUpObservers()
}
deinit {
removeObservers()
}
func handleLaunchOptions(launchOptions: [AnyHashable: Any]) {
// 1. The host App launch is caused by a notification
if let remoteNotification = launchOptions[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any],
let application = RCTSharedApplication() {
// 2. The host App is launched from terminated state to the foreground
// (including transitioning to foregound), i.e. .active .inactive.
// This happens under one of below conditions:
// a. Remote notifications are not able to launch the host App (without `content-available: 1`)
// b. Remote notifications background mode was not enabled on the host App
// c. The end user disabled background refresh of the host App
// 3. This notification must be tapped by an end user which is recorded as the launch notification
if application.applicationState != .background {
launchNotification = remoteNotification
// NOTE: the notification payload will also be passed into didReceiveRemoteNotification below after
// this delegate method, didFinishLaunchingWithOptions completes.
// As this notification will already be recorded as the launch notification, it should not be sent as
// notificationOpened event, this check is handled in didReceiveRemoteNotification.
}
// Otherwise the host App is launched in the background, this notification will be sent to react-native
// as backgroundMessageReceived event in didReceiveRemoteNotification below.
// After the host App launched in the background, didFinishLaunchingWithOptions will no longer
// be fired when an end user taps a notification.
// After the host App launched in the background, it runs developers' react-native code as well.
}
}
func requestPermissions(
_ permissions: [AnyHashable: Any],
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {
if RCTRunningInAppExtension() {
reject("ERROR", "requestPermissions can not be called in App Extensions", nil)
return
}
Task {
var options: UNAuthorizationOptions = []
if permissions["alert"] as? Bool == true {
options.insert(.alert)
}
if permissions["badge"] as? Bool == true {
options.insert(.badge)
}
if permissions["sound"] as? Bool == true {
options.insert(.sound)
}
if permissions["criticalAlert"] as? Bool == true {
options.insert(.criticalAlert)
}
if permissions["provisional"] as? Bool == true {
options.insert(.provisional)
}
do {
let granted = try await AUNotificationPermissions.request(options)
resolve(granted)
} catch {
reject("ERROR", error.localizedDescription, error)
}
}
}
func getPermissionStatus(
_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {
Task {
let status = await AUNotificationPermissions.status
resolve(status.description)
}
}
func getLaunchNotification(
_ resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
let launchNotification = self.launchNotification
self.launchNotification = nil
resolve(launchNotification == nil ? NSNull() : launchNotification)
}
func setBadgeCount(_ count: Int) {
DispatchQueue.main.async {
RCTSharedApplication()?.applicationIconBadgeNumber = count
}
}
func getBadgeCount(
_ resolve: @escaping RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
DispatchQueue.main.async {
resolve(RCTSharedApplication()?.applicationIconBadgeNumber ?? 0)
}
}
func didRegisterForRemoteNotificationsWithDeviceToken(deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
// Reduce frequency of tokenReceived event emitting to RN
if (cachedDeviceToken != token) {
cachedDeviceToken = token
sharedEventManager.sendEventToJS(
PushEvent(type: NativeEvent.tokenReceived, payload: ["token": cachedDeviceToken])
)
}
}
func didFailToRegisterForRemoteNotificationsWithError(error: Error) {
print("Register for remote notifications failed due to \(error).")
}
func didReceiveRemoteNotification(
userInfo: [AnyHashable: Any],
completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
if let application = RCTSharedApplication() {
switch application.applicationState {
case .background:
let completionHandlerId = UUID().uuidString
var userInfoCopy = userInfo
remoteNotificationCompletionHandlers[completionHandlerIdKey] = completionHandler
userInfoCopy[completionHandlerIdKey] = completionHandlerId
sharedEventManager.sendEventToJS(
PushEvent(type: NativeEvent.backgroundMessageReceived, payload: userInfoCopy)
)
// Expecting remoteNotificationCompletionHandlers[completionHandlerIdKey] to be called from JS to complete
// the background notification
case .inactive:
if let launchNotification = launchNotification {
if NSDictionary(dictionary: launchNotification).isEqual(to: userInfo) {
// When the last tapped notification is the same as the launch notification,
// it's sent as launchNotificationOpened event, and retrievable via getLaunchNotification.
PushEventManager.shared.sendEventToJS(
PushEvent(type: NativeEvent.launchNotificationOpened, payload: launchNotification)
)
} else {
// When a launch notification is recorded in handleLaunchOptions above,
// but the last tapped notification is not the recorded launch notification, the last
// tapped notification will be sent to react-native as notificationOpened event.
// This may happen when an end user rapidly tapped on multiple notifications.
self.launchNotification = nil
sharedEventManager.sendEventToJS(
PushEvent(type: NativeEvent.notificationOpened, payload: userInfo)
)
}
} else {
// When there is no launch notification recorded, the last tapped notification
// will be sent to react-native as notificationOpened event.
sharedEventManager.sendEventToJS(
PushEvent(type: NativeEvent.notificationOpened, payload: userInfo)
)
}
completionHandler(.noData)
case .active:
sharedEventManager.sendEventToJS(
PushEvent(type: NativeEvent.foregroundMessageReceived, payload: userInfo)
)
completionHandler(.noData)
@unknown default: break // we don't handle any possible new state added in the future for now
}
}
}
func completeNotification(_ completionHandlerId: String) {
if let completionHandler = remoteNotificationCompletionHandlers[completionHandlerId] {
completionHandler(.noData)
remoteNotificationCompletionHandlers.removeValue(forKey: completionHandlerId)
}
}
private func setUpObservers() {
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(applicationDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
}
private func removeObservers() {
NotificationCenter.default.removeObserver(
self,
name: UIApplication.didBecomeActiveNotification,
object: nil
)
NotificationCenter.default.removeObserver(
self,
name: UIApplication.didEnterBackgroundNotification,
object: nil
)
}
@objc
private func applicationDidBecomeActive() {
registerForRemoteNotifications()
}
@objc
private func applicationDidEnterBackground() {
// When App enters background we remove the cached launchNotification
// as when the App reopens after this point, there won't be a notification
// that launched the App.
launchNotification = nil
}
private func registerForRemoteNotifications() {
if RCTRunningInAppExtension() {
return
}
DispatchQueue.main.async {
RCTSharedApplication()?.registerForRemoteNotifications()
}
}
}