From b592bd8c68ef14f51cbba5bba091774599b89271 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Thu, 17 Aug 2023 13:42:47 -0600 Subject: [PATCH 1/4] fix: add unsubscribed status, remove clearing device token on change events logic, add badge clearing on app open --- Example/BasicExample/BasicExample/AppDelegate.swift | 2 +- .../BasicExample/BasicExample/SceneDelegate.swift | 1 + Sources/TwilioEngage/TwilioEngage.swift | 13 +++++++------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Example/BasicExample/BasicExample/AppDelegate.swift b/Example/BasicExample/BasicExample/AppDelegate.swift index f6fd77f..ed9991d 100644 --- a/Example/BasicExample/BasicExample/AppDelegate.swift +++ b/Example/BasicExample/BasicExample/AppDelegate.swift @@ -85,7 +85,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD func applicationWillEnterForeground(_ application: UIApplication) { - UserDefaults(suiteName: "group.com.segment.twiliopush")?.set(1, forKey: "count"); UIApplication.shared.applicationIconBadgeNumber = 0 + UserDefaults(suiteName: "group.com.segment.twiliopush")?.set(1, forKey: "Count"); UIApplication.shared.applicationIconBadgeNumber = 0 } // MARK: UISceneSession Lifecycle diff --git a/Example/BasicExample/BasicExample/SceneDelegate.swift b/Example/BasicExample/BasicExample/SceneDelegate.swift index b80cbcf..dbf0be5 100644 --- a/Example/BasicExample/BasicExample/SceneDelegate.swift +++ b/Example/BasicExample/BasicExample/SceneDelegate.swift @@ -39,6 +39,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func sceneWillEnterForeground(_ scene: UIScene) { // Called as the scene transitions from the background to the foreground. // Use this method to undo the changes made on entering the background. + UserDefaults(suiteName: "group.com.segment.twiliopush")?.set(1, forKey: "Count"); UIApplication.shared.applicationIconBadgeNumber = 0 } func sceneDidEnterBackground(_ scene: UIScene) { diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 4c9f7bd..70bfec0 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -79,7 +79,12 @@ public class TwilioEngage: EventPlugin { case .notDetermined: newStatus = .didNotSubscribe case .denied: - newStatus = .didNotSubscribe + // accounts for user disabling notifications in settings + if currentStatus == .subscribed { + newStatus = .unsubscribed + } else { + newStatus = .didNotSubscribe + } default: // These cases are all some version of subscribed. //case .authorized: @@ -97,6 +102,7 @@ public class TwilioEngage: EventPlugin { UIApplication.shared.registerForRemoteNotifications() } } + print("Push Status Changed, old=\(currentStatus), new=\(newStatus)") self.analytics?.track(name: Events.changed.rawValue) } @@ -111,11 +117,6 @@ public class TwilioEngage: EventPlugin { // this will succeed if the event name can be used to generate a push event case. guard Events(rawValue: event.event) != nil else { return event as? T } - // we don't need the device token for status changed events - if Events(rawValue: event.event) == Events.changed { - deviceToken = nil - } - // `messaging_subscription` data type is an array of objects context[keyPath: KeyPath(Self.contextKey)] = [[ "key": deviceToken, From bf5fb9c23fb524850f98ca7d91dae30fb8a83a38 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Wed, 6 Sep 2023 11:06:21 -0600 Subject: [PATCH 2/4] fix: add dedup_id fix push_open --- Sources/TwilioEngage/TwilioEngage.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 70bfec0..cfa6f07 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -113,10 +113,23 @@ public class TwilioEngage: EventPlugin { // we only wanna look at track events guard var event = event as? TrackEvent else { return event } guard var context = event.context else { return event as? T } +// guard var properties = event.properties else {return event } // this will succeed if the event name can be used to generate a push event case. guard Events(rawValue: event.event) != nil else { return event as? T } + + // we only need to add a deDup_id to `push recieved` and `push opened` events + if event.event == Events.tapped.rawValue || event.event == Events.received.rawValue { + if var properties = event.properties?.dictionaryValue { + let formattedEventName = event.event.lowercased().replacingOccurrences(of: " ", with: "_") + let deDup_id = "\(formattedEventName)\(event.messageId!)" + properties[keyPath: "dedup_id"] = deDup_id + + event.properties = try? JSON(properties) + } + } + // `messaging_subscription` data type is an array of objects context[keyPath: KeyPath(Self.contextKey)] = [[ "key": deviceToken, @@ -142,7 +155,6 @@ public class TwilioEngage: EventPlugin { extension TwilioEngage: RemoteNotifications { public func receivedRemoteNotification(userInfo: [AnyHashable: Any]) { - // notification was received while the app was open. if let notification = userInfo as? [String: Any] { trackNotification(notification, fromLaunch: false) } From 65b35baef285fa8323d4120b06001fab94c51973 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Wed, 6 Sep 2023 12:29:09 -0600 Subject: [PATCH 3/4] fix: change message_id to message_id from push data --- Sources/TwilioEngage/TwilioEngage.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 20188ba..2e6a040 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -122,7 +122,9 @@ public class TwilioEngage: EventPlugin { if event.event == Events.tapped.rawValue || event.event == Events.received.rawValue { if var properties = event.properties?.dictionaryValue { let formattedEventName = event.event.lowercased().replacingOccurrences(of: " ", with: "_") - let deDup_id = "\(formattedEventName)\(event.messageId!)" +// if properties.con + let messageId = properties["message_id"] ?? UUID().toString() + let deDup_id = "\(formattedEventName)\(messageId)" properties[keyPath: "dedup_id"] = deDup_id event.properties = try? JSON(properties) From 8aa6dcf4708c70e208f9eb4b26fc5c487ead59f0 Mon Sep 17 00:00:00 2001 From: Alan Charles Date: Wed, 6 Sep 2023 13:19:57 -0600 Subject: [PATCH 4/4] fix: change message_id to twi messageId --- Sources/TwilioEngage/TwilioEngage.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/TwilioEngage/TwilioEngage.swift b/Sources/TwilioEngage/TwilioEngage.swift index 2e6a040..ea23289 100644 --- a/Sources/TwilioEngage/TwilioEngage.swift +++ b/Sources/TwilioEngage/TwilioEngage.swift @@ -122,7 +122,6 @@ public class TwilioEngage: EventPlugin { if event.event == Events.tapped.rawValue || event.event == Events.received.rawValue { if var properties = event.properties?.dictionaryValue { let formattedEventName = event.event.lowercased().replacingOccurrences(of: " ", with: "_") -// if properties.con let messageId = properties["message_id"] ?? UUID().toString() let deDup_id = "\(formattedEventName)\(messageId)" properties[keyPath: "dedup_id"] = deDup_id