-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Update push auto config to be static
- Loading branch information
Showing
5 changed files
with
103 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// | ||
// PushAutoConfig.swift | ||
// AppcuesKit | ||
// | ||
// Created by Matt on 2024-04-15. | ||
// Copyright © 2024 Appcues. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
internal enum PushAutoConfig { | ||
// This is an array to support the (rare) case of multiple SDK instances supporting push | ||
private static var pushMonitors: [WeakPushMonitoring] = [] | ||
|
||
static func register(observer: PushMonitoring) { | ||
pushMonitors.append(WeakPushMonitoring(observer)) | ||
} | ||
|
||
static func remove(observer: PushMonitoring) { | ||
pushMonitors.removeAll { $0.value == nil || $0.value === observer } | ||
} | ||
|
||
static func configureAutomatically() { | ||
UIApplication.swizzleDidRegisterForDeviceToken() | ||
UIApplication.shared.registerForRemoteNotifications() | ||
|
||
UNUserNotificationCenter.swizzleNotificationCenterGetDelegate() | ||
} | ||
|
||
static func didRegister(deviceToken: Data) { | ||
// Pass device token to all observing PushMonitor instances | ||
pushMonitors.forEach { weakPushMonitor in | ||
if let pushMonitor = weakPushMonitor.value { | ||
pushMonitor.setPushToken(deviceToken) | ||
} | ||
} | ||
} | ||
|
||
// Shared instance is called from the swizzled method | ||
static func didReceive( | ||
_ response: UNNotificationResponse, | ||
withCompletionHandler completionHandler: @escaping () -> Void | ||
) { | ||
// Stop at the first PushMonitor that successfully handles the notification | ||
_ = pushMonitors.first { weakPushMonitor in | ||
if let pushMonitor = weakPushMonitor.value { | ||
return pushMonitor.didReceiveNotification(response: response, completionHandler: completionHandler) | ||
} | ||
return false | ||
} | ||
} | ||
|
||
// Shared instance is called from the swizzled method | ||
static func willPresent( | ||
_ parsedNotification: ParsedNotification, | ||
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void | ||
) { | ||
// Behavior for all Appcues notification | ||
if #available(iOS 14.0, *) { | ||
completionHandler([.banner, .list]) | ||
} else { | ||
completionHandler(.alert) | ||
} | ||
} | ||
} | ||
|
||
extension PushAutoConfig { | ||
class WeakPushMonitoring { | ||
weak var value: PushMonitoring? | ||
|
||
init (_ wrapping: PushMonitoring) { self.value = wrapping } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters