Skip to content

Commit 9fa33dd

Browse files
authored
Convert completion handlers to async API for push and AppIntegration (#3305)
* converted AppIntegration delegate usage * converted push notification delegate * converted some internal interfaces
1 parent eb391bb commit 9fa33dd

23 files changed

+400
-679
lines changed

Airship/AirshipAutomation/Source/InAppAutomation.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,14 @@ extension InAppAutomation {
141141
}
142142

143143
func receivedRemoteNotification(
144-
_ notification: [AnyHashable: Any],
145-
completionHandler: @Sendable @escaping (UIBackgroundFetchResult) -> Void
146-
) {
147-
self._legacyInAppMessaging.receivedRemoteNotification(notification, completionHandler: completionHandler)
144+
_ notification: AirshipJSON // wrapped [AnyHashable: Any]
145+
) async -> UIBackgroundFetchResult {
146+
return await self._legacyInAppMessaging.receivedRemoteNotification(notification)
148147
}
149148

150149
#if !os(tvOS)
151-
func receivedNotificationResponse(_ response: UNNotificationResponse, completionHandler: @Sendable @escaping () -> Void) {
152-
self._legacyInAppMessaging.receivedNotificationResponse(response, completionHandler: completionHandler)
150+
func receivedNotificationResponse(_ response: UNNotificationResponse) async {
151+
await self._legacyInAppMessaging.receivedNotificationResponse(response)
153152
}
154153
#endif
155154
}

Airship/AirshipAutomation/Source/InAppAutomationComponent.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* Copyright Airship and Contributors */
22

33
import Foundation
4+
@preconcurrency
5+
import UserNotifications
46

57
#if canImport(AirshipCore)
68
import AirshipCore
@@ -21,15 +23,14 @@ final class InAppAutomationComponent: AirshipComponent, AirshipPushableComponent
2123
}
2224

2325
func receivedRemoteNotification(
24-
_ notification: [AnyHashable: Any],
25-
completionHandler: @Sendable @escaping (UIBackgroundFetchResult) -> Void
26-
) {
27-
self.inAppAutomation.receivedRemoteNotification(notification, completionHandler: completionHandler)
26+
_ notification: AirshipJSON
27+
) async -> UIBackgroundFetchResult {
28+
return await self.inAppAutomation.receivedRemoteNotification(notification)
2829
}
2930

3031
#if !os(tvOS)
31-
func receivedNotificationResponse(_ response: UNNotificationResponse, completionHandler: @Sendable @escaping () -> Void) {
32-
self.inAppAutomation.receivedNotificationResponse(response, completionHandler: completionHandler)
32+
func receivedNotificationResponse(_ response: UNNotificationResponse) async {
33+
await self.inAppAutomation.receivedNotificationResponse(response)
3334
}
3435
#endif
3536
}

Airship/AirshipAutomation/Source/InAppMessage/Legacy/LegacyInAppMessaging.swift

+21-27
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ public protocol LegacyInAppMessagingProtocol: AnyObject, Sendable {
3232

3333
protocol InternalLegacyInAppMessagingProtocol: LegacyInAppMessagingProtocol {
3434
#if !os(tvOS)
35-
func receivedNotificationResponse(_ response: UNNotificationResponse, completionHandler: @Sendable @escaping () -> Void)
35+
func receivedNotificationResponse(_ response: UNNotificationResponse) async
3636
#endif
3737

38-
func receivedRemoteNotification(_ notification: [AnyHashable : Any],
39-
completionHandler: @Sendable @escaping (UIBackgroundFetchResult) -> Void)
38+
func receivedRemoteNotification(
39+
_ notification: AirshipJSON // wrapped [AnyHashable : Any]
40+
) async -> UIBackgroundFetchResult
4041
}
4142

4243
final class LegacyInAppMessaging: LegacyInAppMessagingProtocol, @unchecked Sendable {
@@ -209,47 +210,43 @@ final class LegacyInAppMessaging: LegacyInAppMessagingProtocol, @unchecked Senda
209210
extension LegacyInAppMessaging: InternalLegacyInAppMessagingProtocol {
210211

211212
#if !os(tvOS)
212-
func receivedNotificationResponse(_ response: UNNotificationResponse, completionHandler: @Sendable @escaping () -> Void) {
213+
func receivedNotificationResponse(_ response: UNNotificationResponse) async {
213214
let userInfo = response.notification.request.content.userInfo
214215

215216
guard
216217
userInfo.keys.contains(Keys.incomingMessageKey.rawValue),
217218
let messageID = userInfo["_"] as? String,
218219
messageID == self.pendingMessageID
219220
else {
220-
completionHandler()
221221
return
222222
}
223223

224224
self.pendingMessageID = nil
225225

226-
Task {
227-
if await self.scheduleExists(identifier: messageID) {
228-
AirshipLogger.debug("Pending in-app message replaced")
229-
self.analytics.recordDirectOpenEvent(scheduleID: messageID)
230-
}
231-
232-
await self.cancelSchedule(identifier: messageID)
233-
completionHandler()
226+
if await self.scheduleExists(identifier: messageID) {
227+
AirshipLogger.debug("Pending in-app message replaced")
228+
self.analytics.recordDirectOpenEvent(scheduleID: messageID)
234229
}
230+
231+
await self.cancelSchedule(identifier: messageID)
235232
}
236233
#endif
237234

238-
func receivedRemoteNotification(_ notification: [AnyHashable : Any],
239-
completionHandler: @Sendable @escaping (UIBackgroundFetchResult) -> Void) {
240-
guard let payload = notification[Keys.incomingMessageKey.rawValue] as? [String: Any] else {
241-
completionHandler(.noData)
242-
return
235+
func receivedRemoteNotification(_ notification: AirshipJSON) async -> UIBackgroundFetchResult {
236+
guard
237+
let userInfo = notification.unWrap() as? [AnyHashable: Any],
238+
let payload = userInfo[Keys.incomingMessageKey.rawValue] as? [String: Any] else {
239+
return .noData
243240
}
244241

245-
let overrideId = notification["_"] as? String
242+
let overrideId = userInfo["_"] as? String
246243
let messageCenterAction: AirshipJSON?
247244

248245
if
249-
let actionRaw = notification[Keys.messageCenterActionKey.rawValue] as? [String: Any],
246+
let actionRaw = userInfo[Keys.messageCenterActionKey.rawValue] as? [String: Any],
250247
let action = try? AirshipJSON.wrap(actionRaw) {
251248
messageCenterAction = action
252-
} else if let messageId = notification[Keys.messageCenterActionKey.rawValue] as? String {
249+
} else if let messageId = userInfo[Keys.messageCenterActionKey.rawValue] as? String {
253250
messageCenterAction = .object([Keys.messageCenterActionKey.rawValue: .string(messageId)])
254251
} else {
255252
messageCenterAction = nil
@@ -263,13 +260,10 @@ extension LegacyInAppMessaging: InternalLegacyInAppMessagingProtocol {
263260
)
264261

265262
if let message = message {
266-
Task {
267-
await schedule(message: message)
268-
completionHandler(.noData)
269-
}
270-
} else {
271-
completionHandler(.noData)
263+
await schedule(message: message)
272264
}
265+
266+
return .noData
273267
}
274268

275269
private enum Keys: String {

Airship/AirshipAutomation/Tests/Actions/CancelSchedulesActionTest.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ final class TestLegacyInAppMessaging: InternalLegacyInAppMessagingProtocol, @unc
222222
self.displayASAPEnabled = displayASAPEnabled
223223
}
224224

225-
func receivedNotificationResponse(_ response: UNNotificationResponse, completionHandler: @escaping () -> Void) {
225+
func receivedNotificationResponse(_ response: UNNotificationResponse) async {
226226

227227
}
228228

229-
func receivedRemoteNotification(_ notification: [AnyHashable : Any], completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
230-
229+
func receivedRemoteNotification(_ notification: AirshipJSON) async -> UIBackgroundFetchResult {
230+
return .noData
231231
}
232232

233233
var customMessageConverter: AirshipAutomation.MessageConvertor?

0 commit comments

Comments
 (0)