-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAirshipDelegate.swift
More file actions
251 lines (213 loc) · 7.46 KB
/
Copy pathAirshipDelegate.swift
File metadata and controls
251 lines (213 loc) · 7.46 KB
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
/* Copyright Airship and Contributors */
import Foundation
import UserNotifications
import UIKit
#if canImport(AirshipKit)
import AirshipKit
import Combine
#elseif canImport(AirshipCore)
import AirshipCore
import AirshipMessageCenter
import AirshipPreferenceCenter
import AirshipAutomation
#endif
@MainActor
final class AirshipDelegate {
let proxyStore: ProxyStore
let eventEmitter: AirshipProxyEventEmitter
init(
proxyStore: ProxyStore = ProxyStore.shared,
eventEmitter: AirshipProxyEventEmitter = AirshipProxyEventEmitter.shared
) {
self.proxyStore = proxyStore
self.eventEmitter = eventEmitter
}
func messageCenterInboxUpdated() {
AirshipLogger.trace("Message center inbox updated, emitting event")
Task {
self.eventEmitter.addEvent(
MessageCenterUpdatedEvent(
messageCount: await Airship.messageCenter.inbox.messages.count,
unreadCount: await Airship.messageCenter.inbox.unreadCount
),
replacePending: true
)
}
}
func channelCreated() {
guard let channelID = Airship.channel.identifier else {
return
}
AirshipLogger.trace("Channel created, emitting event channelId=\(channelID)")
self.eventEmitter.addEvent(
ChannelCreatedEvent(channelID)
)
}
}
extension AirshipDelegate: PushNotificationDelegate {
@MainActor
private var forwardPushDelegate: (any AirshipPluginPushNotificationDelegate)? {
AirshipPluginExtensions.shared.pushNotificationForwardDelegate
}
@MainActor
func receivedForegroundNotification(_ userInfo: [AnyHashable : Any]) async {
do {
self.eventEmitter.addEvent(
try PushReceivedEvent(
userInfo: userInfo,
isForeground: true
)
)
} catch {
AirshipLogger.error("Failed to generate PushReceivedEvent \(error)")
}
await forwardPushDelegate?.receivedForegroundNotification(userInfo)
}
@MainActor
func receivedBackgroundNotification(_ userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
do {
self.eventEmitter.addEvent(
try PushReceivedEvent(
userInfo: userInfo,
isForeground: false
)
)
} catch {
AirshipLogger.error("Failed to generate PushReceivedEvent \(error)")
}
return await forwardPushDelegate?.receivedBackgroundNotification(userInfo) ?? .noData
}
@MainActor
func receivedNotificationResponse(_ notificationResponse: UNNotificationResponse) async {
LaunchDeepLinkTracker.shared.onNotificationResponse(
userInfo: notificationResponse.notification.request.content.userInfo,
isDefaultAction: notificationResponse.actionIdentifier == UNNotificationDefaultActionIdentifier,
isAppForegrounded: AppStateTracker.shared.isForegrounded
)
do {
if (notificationResponse.actionIdentifier != UNNotificationDismissActionIdentifier) {
self.eventEmitter.addEvent(
try NotificationResponseEvent(
response: notificationResponse
)
)
}
} catch {
AirshipLogger.error("Failed to generate NotificationResponseEvent \(error)")
}
await forwardPushDelegate?.receivedNotificationResponse(notificationResponse)
}
@MainActor
func extendPresentationOptions(_ options: UNNotificationPresentationOptions, notification: UNNotification) async -> UNNotificationPresentationOptions {
let override = await AirshipPluginExtensions.shared.onWillPresentForegroundNotification?(notification)
switch(override) {
case .override(let options):
AirshipLogger.debug("Presentation options overriden by plugin hooks \(options)")
return options
case .useDefault, .none:
do {
let overrides = try await AirshipProxy.shared.push.presentationOptions(
notification: notification
)
return overrides ?? options
} catch {
AirshipLogger.error("Failed to extendPresentationOptions \(error)")
return options
}
}
}
}
extension AirshipDelegate: RegistrationDelegate {
@MainActor
private var forwardRegistrationDelegate: (any RegistrationDelegate)? {
AirshipPluginExtensions.shared.registrationForwardDelegate
}
@MainActor
func apnsRegistrationSucceeded(withDeviceToken deviceToken: Data) {
let token = AirshipUtils.deviceTokenStringFromDeviceToken(deviceToken)
self.eventEmitter.addEvent(
PushTokenReceivedEvent(
pushToken: token
),
replacePending: true
)
forwardRegistrationDelegate?.apnsRegistrationSucceeded(withDeviceToken: deviceToken)
}
@MainActor
func apnsRegistrationFailedWithError(_ error: any Error) {
forwardRegistrationDelegate?.apnsRegistrationFailedWithError(error)
}
@MainActor
func notificationAuthorizedSettingsDidChange(
_ authorizedSettings: AirshipAuthorizedNotificationSettings
) {
self.eventEmitter.addEvent(
AuthorizedNotificationSettingsChangedEvent(
authorizedSettings: authorizedSettings
),
replacePending: true
)
forwardRegistrationDelegate?.notificationAuthorizedSettingsDidChange(authorizedSettings)
}
}
extension AirshipDelegate: MessageCenterDisplayDelegate {
@MainActor
func displayMessageCenter(messageID: String) {
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display(messageID: messageID)
return
}
self.eventEmitter.addEvent(
DisplayMessageCenterEvent(
messageID: messageID
)
)
}
func displayMessageCenter() {
guard !self.proxyStore.autoDisplayMessageCenter else {
DefaultMessageCenterUI.shared.display()
return
}
self.eventEmitter.addEvent(
DisplayMessageCenterEvent()
)
}
func dismissMessageCenter() {
DefaultMessageCenterUI.shared.dismiss()
}
}
extension AirshipDelegate: PreferenceCenterOpenDelegate {
func openPreferenceCenter(
_ preferenceCenterID: String
) -> Bool {
let autoLaunch = self.proxyStore.shouldAutoLaunchPreferenceCenter(
preferenceCenterID
)
guard !autoLaunch else {
return false
}
self.eventEmitter.addEvent(
DisplayPreferenceCenterEvent(
preferenceCenterID: preferenceCenterID
)
)
return true
}
}
extension AirshipDelegate: DeepLinkDelegate {
func receivedDeepLink(
_ deepLink: URL
) async {
let override = await AirshipPluginExtensions.shared.onDeepLink?(deepLink) ?? .useDefault
switch(override) {
case .override:
AirshipLogger.debug("Deeplink handling overridden by plugin hooks \(deepLink)")
return
case .useDefault:
self.eventEmitter.addEvent(
DeepLinkEvent(deepLink)
)
return
}
}
}