From 98c5adebd6cc42532ae6abb9a1db858601c071fa Mon Sep 17 00:00:00 2001 From: Matt Hayashida Date: Tue, 18 Jun 2024 13:25:16 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Ensure=20latest=20device?= =?UTF-8?q?=20prop=20values=20are=20always=20sent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Appcues/AppcuesRequestPushAction.swift | 2 +- .../Presentation/Debugger/PushVerifier.swift | 2 +- Sources/AppcuesKit/Push/PushMonitor.swift | 16 +++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/AppcuesKit/Presentation/Actions/Appcues/AppcuesRequestPushAction.swift b/Sources/AppcuesKit/Presentation/Actions/Appcues/AppcuesRequestPushAction.swift index 94a3416f4..d3bfc74cb 100644 --- a/Sources/AppcuesKit/Presentation/Actions/Appcues/AppcuesRequestPushAction.swift +++ b/Sources/AppcuesKit/Presentation/Actions/Appcues/AppcuesRequestPushAction.swift @@ -32,7 +32,7 @@ internal class AppcuesRequestPushAction: AppcuesExperienceAction { } let pushMonitor = appcues.container.resolve(PushMonitoring.self) - pushMonitor.refreshPushStatus(publishChange: true) { _ in + pushMonitor.refreshPushStatus { _ in completion() } } diff --git a/Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift b/Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift index 0d13930c0..49169b348 100644 --- a/Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift +++ b/Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift @@ -147,7 +147,7 @@ internal class PushVerifier { private func requestPush() { let options: UNAuthorizationOptions = [.alert, .sound, .badge] UNUserNotificationCenter.current().requestAuthorization(options: options) { _, _ in - self.pushMonitor.refreshPushStatus(publishChange: true) { _ in + self.pushMonitor.refreshPushStatus { _ in DispatchQueue.main.async { self.verifyPush() } diff --git a/Sources/AppcuesKit/Push/PushMonitor.swift b/Sources/AppcuesKit/Push/PushMonitor.swift index 961372c4f..acadce4fa 100644 --- a/Sources/AppcuesKit/Push/PushMonitor.swift +++ b/Sources/AppcuesKit/Push/PushMonitor.swift @@ -17,7 +17,7 @@ internal protocol PushMonitoring: AnyObject { func setPushToken(_ deviceToken: Data?) - func refreshPushStatus(publishChange: Bool, completion: ((UNAuthorizationStatus) -> Void)?) + func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)?) func didReceiveNotification(response: UNNotificationResponse, completionHandler: @escaping () -> Void) -> Bool @discardableResult @@ -55,7 +55,7 @@ internal class PushMonitor: PushMonitoring { self.storage = container.resolve(DataStoring.self) self.analyticsPublisher = container.resolve(AnalyticsPublishing.self) - refreshPushStatus(publishChange: false) + refreshPushStatus() getPushEnvironment() NotificationCenter.default.addObserver( @@ -70,13 +70,15 @@ internal class PushMonitor: PushMonitoring { @objc private func applicationWillEnterForeground(notification: Notification) { - refreshPushStatus(publishChange: true) + refreshPushStatus() } func setPushToken(_ deviceToken: Data?) { - storage.pushToken = deviceToken?.map { String(format: "%02x", $0) }.joined() + let newToken = deviceToken?.map { String(format: "%02x", $0) }.joined() + let shouldPublish = storage.pushToken != newToken + storage.pushToken = newToken - if appcues?.sessionID != nil { + if appcues?.sessionID != nil && shouldPublish { analyticsPublisher.publish(TrackingUpdate( type: .event(name: Events.Device.deviceUpdated.rawValue, interactive: true), isInternal: true @@ -84,7 +86,7 @@ internal class PushMonitor: PushMonitoring { } } - func refreshPushStatus(publishChange: Bool, completion: ((UNAuthorizationStatus) -> Void)? = nil) { + func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)? = nil) { // Skip call to UNUserNotificationCenter.current() in tests to avoid crashing in package tests #if DEBUG guard ProcessInfo.processInfo.environment["XCTestBundlePath"] == nil else { @@ -94,7 +96,7 @@ internal class PushMonitor: PushMonitoring { #endif UNUserNotificationCenter.current().getNotificationSettings { [weak self] settings in - let shouldPublish = publishChange && self?.pushAuthorizationStatus != settings.authorizationStatus + let shouldPublish = self?.appcues?.sessionID != nil && self?.pushAuthorizationStatus != settings.authorizationStatus self?.pushAuthorizationStatus = settings.authorizationStatus if shouldPublish {