@onesignal/capacitor-plugin 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,639 @@
1
+ import Foundation
2
+ import Capacitor
3
+ import OneSignalFramework
4
+ import OneSignalLiveActivities
5
+
6
+ @objc(OneSignalCapacitorPlugin)
7
+ public class OneSignalCapacitorPlugin: CAPPlugin, CAPBridgedPlugin,
8
+ OSNotificationPermissionObserver,
9
+ OSNotificationLifecycleListener,
10
+ OSNotificationClickListener,
11
+ OSPushSubscriptionObserver,
12
+ OSInAppMessageLifecycleListener,
13
+ OSInAppMessageClickListener,
14
+ OSUserStateObserver {
15
+
16
+ public let identifier = "OneSignalCapacitorPlugin"
17
+ public let jsName = "OneSignalCapacitor"
18
+ public let pluginMethods: [CAPPluginMethod] = [
19
+ CAPPluginMethod(name: "initialize", returnType: CAPPluginReturnPromise),
20
+ CAPPluginMethod(name: "login", returnType: CAPPluginReturnPromise),
21
+ CAPPluginMethod(name: "logout", returnType: CAPPluginReturnPromise),
22
+ CAPPluginMethod(name: "setConsentRequired", returnType: CAPPluginReturnPromise),
23
+ CAPPluginMethod(name: "setConsentGiven", returnType: CAPPluginReturnPromise),
24
+ CAPPluginMethod(name: "setLogLevel", returnType: CAPPluginReturnPromise),
25
+ CAPPluginMethod(name: "setAlertLevel", returnType: CAPPluginReturnPromise),
26
+ CAPPluginMethod(name: "setLanguage", returnType: CAPPluginReturnPromise),
27
+ CAPPluginMethod(name: "addAliases", returnType: CAPPluginReturnPromise),
28
+ CAPPluginMethod(name: "removeAliases", returnType: CAPPluginReturnPromise),
29
+ CAPPluginMethod(name: "addEmail", returnType: CAPPluginReturnPromise),
30
+ CAPPluginMethod(name: "removeEmail", returnType: CAPPluginReturnPromise),
31
+ CAPPluginMethod(name: "addSms", returnType: CAPPluginReturnPromise),
32
+ CAPPluginMethod(name: "removeSms", returnType: CAPPluginReturnPromise),
33
+ CAPPluginMethod(name: "addTags", returnType: CAPPluginReturnPromise),
34
+ CAPPluginMethod(name: "removeTags", returnType: CAPPluginReturnPromise),
35
+ CAPPluginMethod(name: "getTags", returnType: CAPPluginReturnPromise),
36
+ CAPPluginMethod(name: "getOnesignalId", returnType: CAPPluginReturnPromise),
37
+ CAPPluginMethod(name: "getExternalId", returnType: CAPPluginReturnPromise),
38
+ CAPPluginMethod(name: "trackEvent", returnType: CAPPluginReturnPromise),
39
+ CAPPluginMethod(name: "getPushSubscriptionId", returnType: CAPPluginReturnPromise),
40
+ CAPPluginMethod(name: "getPushSubscriptionToken", returnType: CAPPluginReturnPromise),
41
+ CAPPluginMethod(name: "getPushSubscriptionOptedIn", returnType: CAPPluginReturnPromise),
42
+ CAPPluginMethod(name: "optInPushSubscription", returnType: CAPPluginReturnPromise),
43
+ CAPPluginMethod(name: "optOutPushSubscription", returnType: CAPPluginReturnPromise),
44
+ CAPPluginMethod(name: "getPermission", returnType: CAPPluginReturnPromise),
45
+ CAPPluginMethod(name: "permissionNative", returnType: CAPPluginReturnPromise),
46
+ CAPPluginMethod(name: "requestPermission", returnType: CAPPluginReturnPromise),
47
+ CAPPluginMethod(name: "canRequestPermission", returnType: CAPPluginReturnPromise),
48
+ CAPPluginMethod(name: "registerForProvisionalAuthorization", returnType: CAPPluginReturnPromise),
49
+ CAPPluginMethod(name: "clearAllNotifications", returnType: CAPPluginReturnPromise),
50
+ CAPPluginMethod(name: "removeNotification", returnType: CAPPluginReturnPromise),
51
+ CAPPluginMethod(name: "removeGroupedNotifications", returnType: CAPPluginReturnPromise),
52
+ CAPPluginMethod(name: "preventDefault", returnType: CAPPluginReturnPromise),
53
+ CAPPluginMethod(name: "proceedWithWillDisplay", returnType: CAPPluginReturnPromise),
54
+ CAPPluginMethod(name: "displayNotification", returnType: CAPPluginReturnPromise),
55
+ CAPPluginMethod(name: "addTriggers", returnType: CAPPluginReturnPromise),
56
+ CAPPluginMethod(name: "removeTriggers", returnType: CAPPluginReturnPromise),
57
+ CAPPluginMethod(name: "clearTriggers", returnType: CAPPluginReturnPromise),
58
+ CAPPluginMethod(name: "setPaused", returnType: CAPPluginReturnPromise),
59
+ CAPPluginMethod(name: "isPaused", returnType: CAPPluginReturnPromise),
60
+ CAPPluginMethod(name: "addOutcome", returnType: CAPPluginReturnPromise),
61
+ CAPPluginMethod(name: "addUniqueOutcome", returnType: CAPPluginReturnPromise),
62
+ CAPPluginMethod(name: "addOutcomeWithValue", returnType: CAPPluginReturnPromise),
63
+ CAPPluginMethod(name: "requestLocationPermission", returnType: CAPPluginReturnPromise),
64
+ CAPPluginMethod(name: "setLocationShared", returnType: CAPPluginReturnPromise),
65
+ CAPPluginMethod(name: "isLocationShared", returnType: CAPPluginReturnPromise),
66
+ CAPPluginMethod(name: "enterLiveActivity", returnType: CAPPluginReturnPromise),
67
+ CAPPluginMethod(name: "exitLiveActivity", returnType: CAPPluginReturnPromise),
68
+ CAPPluginMethod(name: "setPushToStartToken", returnType: CAPPluginReturnPromise),
69
+ CAPPluginMethod(name: "removePushToStartToken", returnType: CAPPluginReturnPromise),
70
+ CAPPluginMethod(name: "setupDefaultLiveActivity", returnType: CAPPluginReturnPromise),
71
+ CAPPluginMethod(name: "startDefaultLiveActivity", returnType: CAPPluginReturnPromise),
72
+ ]
73
+
74
+ private var notificationWillDisplayCache = [String: OSNotificationWillDisplayEvent]()
75
+ private var preventDefaultCache = [String: OSNotificationWillDisplayEvent]()
76
+ private var pendingClickEvent: OSNotificationClickEvent?
77
+
78
+ // MARK: - Core
79
+
80
+ @objc func initialize(_ call: CAPPluginCall) {
81
+ guard let appId = call.getString("appId") else {
82
+ call.reject("appId is required")
83
+ return
84
+ }
85
+ OneSignalWrapper.sdkType = "capacitor"
86
+ OneSignalWrapper.sdkVersion = "010000"
87
+ OneSignal.initialize(appId, withLaunchOptions: nil)
88
+ OneSignal.Notifications.addPermissionObserver(self)
89
+ OneSignal.Notifications.addForegroundLifecycleListener(self)
90
+ OneSignal.Notifications.addClickListener(self)
91
+ OneSignal.User.pushSubscription.addObserver(self)
92
+ OneSignal.User.addObserver(self)
93
+ OneSignal.InAppMessages.addLifecycleListener(self)
94
+ OneSignal.InAppMessages.addClickListener(self)
95
+
96
+ if let pending = pendingClickEvent {
97
+ sendNotificationClickEvent(pending)
98
+ pendingClickEvent = nil
99
+ }
100
+ call.resolve()
101
+ }
102
+
103
+ @objc func login(_ call: CAPPluginCall) {
104
+ guard let externalId = call.getString("externalId") else {
105
+ call.reject("externalId is required")
106
+ return
107
+ }
108
+ OneSignal.login(externalId)
109
+ call.resolve()
110
+ }
111
+
112
+ @objc func logout(_ call: CAPPluginCall) {
113
+ OneSignal.logout()
114
+ call.resolve()
115
+ }
116
+
117
+ @objc func setConsentRequired(_ call: CAPPluginCall) {
118
+ let required = call.getBool("required") ?? false
119
+ OneSignal.setConsentRequired(required)
120
+ call.resolve()
121
+ }
122
+
123
+ @objc func setConsentGiven(_ call: CAPPluginCall) {
124
+ let granted = call.getBool("granted") ?? false
125
+ OneSignal.setConsentGiven(granted)
126
+ call.resolve()
127
+ }
128
+
129
+ // MARK: - Debug
130
+
131
+ @objc func setLogLevel(_ call: CAPPluginCall) {
132
+ let level = call.getInt("logLevel") ?? 0
133
+ OneSignal.Debug.setLogLevel(ONE_S_LOG_LEVEL(rawValue: UInt(level))!)
134
+ call.resolve()
135
+ }
136
+
137
+ @objc func setAlertLevel(_ call: CAPPluginCall) {
138
+ let level = call.getInt("logLevel") ?? 0
139
+ OneSignal.Debug.setAlertLevel(ONE_S_LOG_LEVEL(rawValue: UInt(level))!)
140
+ call.resolve()
141
+ }
142
+
143
+ // MARK: - User
144
+
145
+ @objc func setLanguage(_ call: CAPPluginCall) {
146
+ guard let language = call.getString("language") else {
147
+ call.reject("language is required")
148
+ return
149
+ }
150
+ OneSignal.User.setLanguage(language)
151
+ call.resolve()
152
+ }
153
+
154
+ @objc func addAliases(_ call: CAPPluginCall) {
155
+ guard let aliases = call.getObject("aliases") as? [String: String] else {
156
+ call.reject("aliases is required")
157
+ return
158
+ }
159
+ OneSignal.User.addAliases(aliases)
160
+ call.resolve()
161
+ }
162
+
163
+ @objc func removeAliases(_ call: CAPPluginCall) {
164
+ guard let labels = call.getArray("labels", String.self) else {
165
+ call.reject("labels is required")
166
+ return
167
+ }
168
+ OneSignal.User.removeAliases(labels)
169
+ call.resolve()
170
+ }
171
+
172
+ @objc func addEmail(_ call: CAPPluginCall) {
173
+ guard let email = call.getString("email") else {
174
+ call.reject("email is required")
175
+ return
176
+ }
177
+ OneSignal.User.addEmail(email)
178
+ call.resolve()
179
+ }
180
+
181
+ @objc func removeEmail(_ call: CAPPluginCall) {
182
+ guard let email = call.getString("email") else {
183
+ call.reject("email is required")
184
+ return
185
+ }
186
+ OneSignal.User.removeEmail(email)
187
+ call.resolve()
188
+ }
189
+
190
+ @objc func addSms(_ call: CAPPluginCall) {
191
+ guard let smsNumber = call.getString("smsNumber") else {
192
+ call.reject("smsNumber is required")
193
+ return
194
+ }
195
+ OneSignal.User.addSms(smsNumber)
196
+ call.resolve()
197
+ }
198
+
199
+ @objc func removeSms(_ call: CAPPluginCall) {
200
+ guard let smsNumber = call.getString("smsNumber") else {
201
+ call.reject("smsNumber is required")
202
+ return
203
+ }
204
+ OneSignal.User.removeSms(smsNumber)
205
+ call.resolve()
206
+ }
207
+
208
+ @objc func addTags(_ call: CAPPluginCall) {
209
+ guard let tags = call.getObject("tags") as? [String: String] else {
210
+ call.reject("tags is required")
211
+ return
212
+ }
213
+ OneSignal.User.addTags(tags)
214
+ call.resolve()
215
+ }
216
+
217
+ @objc func removeTags(_ call: CAPPluginCall) {
218
+ guard let keys = call.getArray("keys", String.self) else {
219
+ call.reject("keys is required")
220
+ return
221
+ }
222
+ OneSignal.User.removeTags(keys)
223
+ call.resolve()
224
+ }
225
+
226
+ @objc func getTags(_ call: CAPPluginCall) {
227
+ let tags = OneSignal.User.getTags()
228
+ call.resolve(["tags": tags])
229
+ }
230
+
231
+ @objc func getOnesignalId(_ call: CAPPluginCall) {
232
+ call.resolve(["onesignalId": OneSignal.User.onesignalId ?? NSNull()])
233
+ }
234
+
235
+ @objc func getExternalId(_ call: CAPPluginCall) {
236
+ call.resolve(["externalId": OneSignal.User.externalId ?? NSNull()])
237
+ }
238
+
239
+ @objc func trackEvent(_ call: CAPPluginCall) {
240
+ guard let name = call.getString("name") else {
241
+ call.reject("name is required")
242
+ return
243
+ }
244
+ let properties = call.getObject("properties")
245
+ OneSignal.User.trackEvent(name: name, properties: properties)
246
+ call.resolve()
247
+ }
248
+
249
+ // MARK: - Push Subscription
250
+
251
+ @objc func getPushSubscriptionId(_ call: CAPPluginCall) {
252
+ call.resolve(["id": OneSignal.User.pushSubscription.id ?? NSNull()])
253
+ }
254
+
255
+ @objc func getPushSubscriptionToken(_ call: CAPPluginCall) {
256
+ call.resolve(["token": OneSignal.User.pushSubscription.token ?? NSNull()])
257
+ }
258
+
259
+ @objc func getPushSubscriptionOptedIn(_ call: CAPPluginCall) {
260
+ call.resolve(["optedIn": OneSignal.User.pushSubscription.optedIn])
261
+ }
262
+
263
+ @objc func optInPushSubscription(_ call: CAPPluginCall) {
264
+ OneSignal.User.pushSubscription.optIn()
265
+ call.resolve()
266
+ }
267
+
268
+ @objc func optOutPushSubscription(_ call: CAPPluginCall) {
269
+ OneSignal.User.pushSubscription.optOut()
270
+ call.resolve()
271
+ }
272
+
273
+ // MARK: - Notifications
274
+
275
+ @objc func getPermission(_ call: CAPPluginCall) {
276
+ call.resolve(["permission": OneSignal.Notifications.permission])
277
+ }
278
+
279
+ @objc func permissionNative(_ call: CAPPluginCall) {
280
+ call.resolve(["permission": OneSignal.Notifications.permissionNative.rawValue])
281
+ }
282
+
283
+ @objc func requestPermission(_ call: CAPPluginCall) {
284
+ let fallback = call.getBool("fallbackToSettings") ?? false
285
+ OneSignal.Notifications.requestPermission({ accepted in
286
+ call.resolve(["permission": accepted])
287
+ }, fallbackToSettings: fallback)
288
+ }
289
+
290
+ @objc func canRequestPermission(_ call: CAPPluginCall) {
291
+ call.resolve(["canRequest": OneSignal.Notifications.canRequestPermission])
292
+ }
293
+
294
+ @objc func registerForProvisionalAuthorization(_ call: CAPPluginCall) {
295
+ OneSignal.Notifications.registerForProvisionalAuthorization { accepted in
296
+ call.resolve(["accepted": accepted])
297
+ }
298
+ }
299
+
300
+ @objc func clearAllNotifications(_ call: CAPPluginCall) {
301
+ OneSignal.Notifications.clearAll()
302
+ call.resolve()
303
+ }
304
+
305
+ @objc func removeNotification(_ call: CAPPluginCall) {
306
+ call.resolve()
307
+ }
308
+
309
+ @objc func removeGroupedNotifications(_ call: CAPPluginCall) {
310
+ call.resolve()
311
+ }
312
+
313
+ @objc func preventDefault(_ call: CAPPluginCall) {
314
+ guard let notificationId = call.getString("notificationId") else {
315
+ call.reject("notificationId is required")
316
+ return
317
+ }
318
+ guard let event = notificationWillDisplayCache[notificationId] else {
319
+ call.reject("Could not find notification will display event")
320
+ return
321
+ }
322
+ event.preventDefault()
323
+ preventDefaultCache[notificationId] = event
324
+ call.resolve()
325
+ }
326
+
327
+ @objc func proceedWithWillDisplay(_ call: CAPPluginCall) {
328
+ guard let notificationId = call.getString("notificationId") else {
329
+ call.reject("notificationId is required")
330
+ return
331
+ }
332
+ guard let event = notificationWillDisplayCache[notificationId] else {
333
+ call.reject("Could not find notification will display event")
334
+ return
335
+ }
336
+ if preventDefaultCache[notificationId] == nil {
337
+ event.notification.display()
338
+ }
339
+ call.resolve()
340
+ }
341
+
342
+ @objc func displayNotification(_ call: CAPPluginCall) {
343
+ guard let notificationId = call.getString("notificationId") else {
344
+ call.reject("notificationId is required")
345
+ return
346
+ }
347
+ guard let event = notificationWillDisplayCache[notificationId] else {
348
+ call.reject("Could not find notification will display event")
349
+ return
350
+ }
351
+ event.notification.display()
352
+ call.resolve()
353
+ }
354
+
355
+ // MARK: - In-App Messages
356
+
357
+ @objc func addTriggers(_ call: CAPPluginCall) {
358
+ guard let triggers = call.getObject("triggers") as? [String: String] else {
359
+ call.reject("triggers is required")
360
+ return
361
+ }
362
+ OneSignal.InAppMessages.addTriggers(triggers)
363
+ call.resolve()
364
+ }
365
+
366
+ @objc func removeTriggers(_ call: CAPPluginCall) {
367
+ guard let keys = call.getArray("keys", String.self) else {
368
+ call.reject("keys is required")
369
+ return
370
+ }
371
+ OneSignal.InAppMessages.removeTriggers(keys)
372
+ call.resolve()
373
+ }
374
+
375
+ @objc func clearTriggers(_ call: CAPPluginCall) {
376
+ OneSignal.InAppMessages.clearTriggers()
377
+ call.resolve()
378
+ }
379
+
380
+ @objc func setPaused(_ call: CAPPluginCall) {
381
+ let pause = call.getBool("pause") ?? false
382
+ OneSignal.InAppMessages.paused = pause
383
+ call.resolve()
384
+ }
385
+
386
+ @objc func isPaused(_ call: CAPPluginCall) {
387
+ call.resolve(["paused": OneSignal.InAppMessages.paused])
388
+ }
389
+
390
+ // MARK: - Session / Outcomes
391
+
392
+ @objc func addOutcome(_ call: CAPPluginCall) {
393
+ guard let name = call.getString("name") else {
394
+ call.reject("name is required")
395
+ return
396
+ }
397
+ OneSignal.Session.addOutcome(name)
398
+ call.resolve()
399
+ }
400
+
401
+ @objc func addUniqueOutcome(_ call: CAPPluginCall) {
402
+ guard let name = call.getString("name") else {
403
+ call.reject("name is required")
404
+ return
405
+ }
406
+ OneSignal.Session.addUniqueOutcome(name)
407
+ call.resolve()
408
+ }
409
+
410
+ @objc func addOutcomeWithValue(_ call: CAPPluginCall) {
411
+ guard let name = call.getString("name") else {
412
+ call.reject("name is required")
413
+ return
414
+ }
415
+ let value = call.getFloat("value") ?? 0
416
+ OneSignal.Session.addOutcome(name, NSNumber(value: value))
417
+ call.resolve()
418
+ }
419
+
420
+ // MARK: - Location
421
+
422
+ @objc func requestLocationPermission(_ call: CAPPluginCall) {
423
+ OneSignal.Location.requestPermission()
424
+ call.resolve()
425
+ }
426
+
427
+ @objc func setLocationShared(_ call: CAPPluginCall) {
428
+ let shared = call.getBool("shared") ?? false
429
+ OneSignal.Location.isShared = shared
430
+ call.resolve()
431
+ }
432
+
433
+ @objc func isLocationShared(_ call: CAPPluginCall) {
434
+ call.resolve(["shared": OneSignal.Location.isShared])
435
+ }
436
+
437
+ // MARK: - Live Activities
438
+
439
+ @objc func enterLiveActivity(_ call: CAPPluginCall) {
440
+ guard let activityId = call.getString("activityId"),
441
+ let token = call.getString("token") else {
442
+ call.reject("activityId and token are required")
443
+ return
444
+ }
445
+ OneSignal.LiveActivities.enter(activityId, withToken: token, withSuccess: { _ in
446
+ call.resolve()
447
+ }, withFailure: { error in
448
+ call.reject(error?.localizedDescription ?? "Unknown error")
449
+ })
450
+ }
451
+
452
+ @objc func exitLiveActivity(_ call: CAPPluginCall) {
453
+ guard let activityId = call.getString("activityId") else {
454
+ call.reject("activityId is required")
455
+ return
456
+ }
457
+ OneSignal.LiveActivities.exit(activityId, withSuccess: { _ in
458
+ call.resolve()
459
+ }, withFailure: { error in
460
+ call.reject(error?.localizedDescription ?? "Unknown error")
461
+ })
462
+ }
463
+
464
+ @objc func setPushToStartToken(_ call: CAPPluginCall) {
465
+ #if !targetEnvironment(macCatalyst)
466
+ guard let activityType = call.getString("activityType"),
467
+ let token = call.getString("token") else {
468
+ call.reject("activityType and token are required")
469
+ return
470
+ }
471
+ if #available(iOS 17.2, *) {
472
+ do {
473
+ try OneSignalLiveActivitiesManagerImpl.setPushToStartToken(activityType, withToken: token)
474
+ } catch {
475
+ call.reject("activityType must be the name of your ActivityAttributes struct")
476
+ return
477
+ }
478
+ }
479
+ #endif
480
+ call.resolve()
481
+ }
482
+
483
+ @objc func removePushToStartToken(_ call: CAPPluginCall) {
484
+ #if !targetEnvironment(macCatalyst)
485
+ guard let activityType = call.getString("activityType") else {
486
+ call.reject("activityType is required")
487
+ return
488
+ }
489
+ if #available(iOS 17.2, *) {
490
+ do {
491
+ try OneSignalLiveActivitiesManagerImpl.removePushToStartToken(activityType)
492
+ } catch {
493
+ call.reject("activityType must be the name of your ActivityAttributes struct")
494
+ return
495
+ }
496
+ }
497
+ #endif
498
+ call.resolve()
499
+ }
500
+
501
+ @objc func setupDefaultLiveActivity(_ call: CAPPluginCall) {
502
+ #if !targetEnvironment(macCatalyst)
503
+ if #available(iOS 16.1, *) {
504
+ var laOptions: LiveActivitySetupOptions? = nil
505
+ if let enablePushToStart = call.getBool("enablePushToStart"),
506
+ let enablePushToUpdate = call.getBool("enablePushToUpdate") {
507
+ laOptions = LiveActivitySetupOptions()
508
+ laOptions?.enablePushToStart = enablePushToStart
509
+ laOptions?.enablePushToUpdate = enablePushToUpdate
510
+ }
511
+ OneSignalLiveActivitiesManagerImpl.setupDefault(options: laOptions)
512
+ }
513
+ #endif
514
+ call.resolve()
515
+ }
516
+
517
+ @objc func startDefaultLiveActivity(_ call: CAPPluginCall) {
518
+ #if !targetEnvironment(macCatalyst)
519
+ guard let activityId = call.getString("activityId"),
520
+ let attributes = call.getObject("attributes"),
521
+ let content = call.getObject("content") else {
522
+ call.reject("activityId, attributes, and content are required")
523
+ return
524
+ }
525
+ if #available(iOS 16.1, *) {
526
+ OneSignalLiveActivitiesManagerImpl.startDefault(activityId, attributes: attributes, content: content)
527
+ }
528
+ #endif
529
+ call.resolve()
530
+ }
531
+
532
+ // MARK: - Observer Callbacks
533
+
534
+ public func onNotificationPermissionDidChange(_ permission: Bool) {
535
+ notifyListeners("permissionChange", data: ["permission": permission])
536
+ }
537
+
538
+ public func onPushSubscriptionDidChange(state: OSPushSubscriptionChangedState) {
539
+ var previous: [String: Any] = ["optedIn": state.previous.optedIn]
540
+ if let id = state.previous.id { previous["id"] = id }
541
+ if let token = state.previous.token { previous["token"] = token }
542
+
543
+ var current: [String: Any] = ["optedIn": state.current.optedIn]
544
+ if let id = state.current.id { current["id"] = id }
545
+ if let token = state.current.token { current["token"] = token }
546
+
547
+ notifyListeners("pushSubscriptionChange", data: [
548
+ "previous": previous,
549
+ "current": current
550
+ ])
551
+ }
552
+
553
+ public func onUserStateDidChange(state: OSUserChangedState) {
554
+ var current: [String: Any] = [:]
555
+ if let onesignalId = state.current.onesignalId { current["onesignalId"] = onesignalId }
556
+ if let externalId = state.current.externalId { current["externalId"] = externalId }
557
+
558
+ notifyListeners("userStateChange", data: ["current": current])
559
+ }
560
+
561
+ public func onWillDisplay(event: OSNotificationWillDisplayEvent) {
562
+ guard let notificationId = event.notification.notificationId else { return }
563
+ notificationWillDisplayCache[notificationId] = event
564
+ event.preventDefault()
565
+ if let data = event.notification.stringify().data(using: .utf8),
566
+ let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
567
+ notifyListeners("notificationForegroundWillDisplay", data: json)
568
+ }
569
+ }
570
+
571
+ public func onClick(event: OSNotificationClickEvent) {
572
+ if bridge != nil {
573
+ sendNotificationClickEvent(event)
574
+ } else {
575
+ pendingClickEvent = event
576
+ }
577
+ }
578
+
579
+ private func sendNotificationClickEvent(_ event: OSNotificationClickEvent) {
580
+ if let data = event.stringify().data(using: .utf8),
581
+ let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
582
+ notifyListeners("notificationClick", data: json)
583
+ }
584
+ }
585
+
586
+ @objc(onWillDisplayInAppMessage:)
587
+ public func onWillDisplay(event: OSInAppMessageWillDisplayEvent) {
588
+ notifyListeners("inAppMessageWillDisplay", data: [
589
+ "message": ["messageId": event.message.messageId]
590
+ ])
591
+ }
592
+
593
+ @objc(onDidDisplayInAppMessage:)
594
+ public func onDidDisplay(event: OSInAppMessageDidDisplayEvent) {
595
+ notifyListeners("inAppMessageDidDisplay", data: [
596
+ "message": ["messageId": event.message.messageId]
597
+ ])
598
+ }
599
+
600
+ @objc(onWillDismissInAppMessage:)
601
+ public func onWillDismiss(event: OSInAppMessageWillDismissEvent) {
602
+ notifyListeners("inAppMessageWillDismiss", data: [
603
+ "message": ["messageId": event.message.messageId]
604
+ ])
605
+ }
606
+
607
+ @objc(onDidDismissInAppMessage:)
608
+ public func onDidDismiss(event: OSInAppMessageDidDismissEvent) {
609
+ notifyListeners("inAppMessageDidDismiss", data: [
610
+ "message": ["messageId": event.message.messageId]
611
+ ])
612
+ }
613
+
614
+ public func onClick(event: OSInAppMessageClickEvent) {
615
+ let urlTargetStr: String
616
+ switch event.result.urlTarget.rawValue {
617
+ case 0: urlTargetStr = "browser"
618
+ case 1: urlTargetStr = "webview"
619
+ case 2: urlTargetStr = "replacement"
620
+ default: urlTargetStr = "browser"
621
+ }
622
+
623
+ var clickResult: [String: Any] = [
624
+ "closingMessage": event.result.closingMessage
625
+ ]
626
+ if let actionId = event.result.actionId {
627
+ clickResult["actionId"] = actionId
628
+ }
629
+ if let url = event.result.url {
630
+ clickResult["url"] = url
631
+ }
632
+ clickResult["urlTarget"] = urlTargetStr
633
+
634
+ notifyListeners("inAppMessageClick", data: [
635
+ "message": ["messageId": event.message.messageId],
636
+ "result": clickResult
637
+ ])
638
+ }
639
+ }