Skip to content

Commit

Permalink
♻️ Send device_updated event on push status change
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaatttt authored and iujames committed Sep 17, 2024
1 parent 8c35a9e commit 5f1e881
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ internal class AppcuesRequestPushAction: AppcuesExperienceAction {
}

let pushMonitor = appcues.container.resolve(PushMonitoring.self)
let analyticsPublisher = appcues.container.resolve(AnalyticsPublishing.self)

pushMonitor.refreshPushStatus { _ in
analyticsPublisher.publish(TrackingUpdate(
type: .event(name: Events.Device.deviceUpdated.rawValue, interactive: false),
isInternal: true
))

pushMonitor.refreshPushStatus(publishChange: true) { _ in
completion()
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AppcuesKit/Presentation/Debugger/PushVerifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import Combine

// For mock objects
final class KeyedArchiver: NSKeyedArchiver {
private final class KeyedArchiver: NSKeyedArchiver {
override func decodeObject(forKey _: String) -> Any { "" }

deinit {
Expand Down Expand Up @@ -136,7 +136,7 @@ internal class PushVerifier {
private func requestPush() {
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
UNUserNotificationCenter.current().requestAuthorization(options: options) { _, _ in
self.pushMonitor.refreshPushStatus { _ in
self.pushMonitor.refreshPushStatus(publishChange: false) { _ in
DispatchQueue.main.async {
self.verifyPush()
}
Expand Down
19 changes: 15 additions & 4 deletions Sources/AppcuesKit/Push/PushMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal protocol PushMonitoring: AnyObject {
var pushBackgroundEnabled: Bool { get }
var pushPrimerEligible: Bool { get }

func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)?)
func refreshPushStatus(publishChange: Bool, completion: ((UNAuthorizationStatus) -> Void)?)

func didReceiveNotification(response: UNNotificationResponse, completionHandler: @escaping () -> Void) -> Bool
}
Expand All @@ -24,6 +24,7 @@ internal class PushMonitor: PushMonitoring {
private weak var appcues: Appcues?
private let config: Appcues.Config
private let storage: DataStoring
private let analyticsPublisher: AnalyticsPublishing

private(set) var pushAuthorizationStatus: UNAuthorizationStatus = .notDetermined

Expand All @@ -43,8 +44,9 @@ internal class PushMonitor: PushMonitoring {
self.appcues = container.owner
self.config = container.resolve(Appcues.Config.self)
self.storage = container.resolve(DataStoring.self)
self.analyticsPublisher = container.resolve(AnalyticsPublishing.self)

refreshPushStatus()
refreshPushStatus(publishChange: false)

NotificationCenter.default.addObserver(
self,
Expand All @@ -56,10 +58,10 @@ internal class PushMonitor: PushMonitoring {

@objc
private func applicationWillEnterForeground(notification: Notification) {
refreshPushStatus()
refreshPushStatus(publishChange: true)
}

func refreshPushStatus(completion: ((UNAuthorizationStatus) -> Void)? = nil) {
func refreshPushStatus(publishChange: Bool, 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 {
Expand All @@ -69,7 +71,16 @@ internal class PushMonitor: PushMonitoring {
#endif

UNUserNotificationCenter.current().getNotificationSettings { [weak self] settings in
let shouldPublish = publishChange && self?.pushAuthorizationStatus != settings.authorizationStatus
self?.pushAuthorizationStatus = settings.authorizationStatus

if shouldPublish {
self?.analyticsPublisher.publish(TrackingUpdate(
type: .event(name: Events.Device.deviceUpdated.rawValue, interactive: false),
isInternal: true
))
}

completion?(settings.authorizationStatus)
}
}
Expand Down

0 comments on commit 5f1e881

Please sign in to comment.