Skip to content

Commit 7c548de

Browse files
authored
CT Push Migration (#522)
* Call CleverTap push methods * Instantiate wrapper once
1 parent 4cfb964 commit 7c548de

File tree

8 files changed

+114
-38
lines changed

8 files changed

+114
-38
lines changed

LeanplumSDK/LeanplumSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@
196196
6A07FDA32811911000995BE3 /* ActionManager+FileDownload.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A07FDA12811911000995BE3 /* ActionManager+FileDownload.swift */; };
197197
6A07FDAA28352DCE00995BE3 /* ContentMerger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A07FDA928352DCE00995BE3 /* ContentMerger.swift */; };
198198
6A07FDAB28352DCE00995BE3 /* ContentMerger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A07FDA928352DCE00995BE3 /* ContentMerger.swift */; };
199+
6A15EB3D28F6A5DB00878D6C /* LPCTNotificationsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A15EB3C28F6A5DB00878D6C /* LPCTNotificationsManager.swift */; };
200+
6A15EB3E28F6A5DB00878D6C /* LPCTNotificationsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A15EB3C28F6A5DB00878D6C /* LPCTNotificationsManager.swift */; };
199201
6A2109E128BE88D900DBF4A9 /* CTWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2109DF28BE88D900DBF4A9 /* CTWrapper.swift */; };
200202
6A2109E228BE88D900DBF4A9 /* CTWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2109DF28BE88D900DBF4A9 /* CTWrapper.swift */; };
201203
6A2109E328BE88D900DBF4A9 /* MigrationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A2109E028BE88D900DBF4A9 /* MigrationManager.swift */; };
@@ -809,6 +811,7 @@
809811
6A07FD9E280F27C700995BE3 /* NSRegularExpression+Matches.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSRegularExpression+Matches.swift"; sourceTree = "<group>"; };
810812
6A07FDA12811911000995BE3 /* ActionManager+FileDownload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ActionManager+FileDownload.swift"; sourceTree = "<group>"; };
811813
6A07FDA928352DCE00995BE3 /* ContentMerger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentMerger.swift; sourceTree = "<group>"; };
814+
6A15EB3C28F6A5DB00878D6C /* LPCTNotificationsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LPCTNotificationsManager.swift; sourceTree = "<group>"; };
812815
6A2109DF28BE88D900DBF4A9 /* CTWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CTWrapper.swift; sourceTree = "<group>"; };
813816
6A2109E028BE88D900DBF4A9 /* MigrationManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MigrationManager.swift; sourceTree = "<group>"; };
814817
6A265E2527187EBB0074354F /* NotificationsProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NotificationsProxy.swift; sourceTree = "<group>"; };
@@ -1354,6 +1357,7 @@
13541357
6A29EABA28EA12D30024880E /* MigrationManager+Constants.swift */,
13551358
6A29EAC328EB56AD0024880E /* MigrationManager+ResponseHandler.swift */,
13561359
6A37A89828EF738200F4339F /* MigrationManager+API.swift */,
1360+
6A15EB3C28F6A5DB00878D6C /* LPCTNotificationsManager.swift */,
13571361
);
13581362
path = Migration;
13591363
sourceTree = "<group>";
@@ -1967,6 +1971,7 @@
19671971
39C081A32781D40200C1DBD6 /* ActionManager+Definition.swift in Sources */,
19681972
075AAE9826847EC4007CA1BD /* Leanplum_SocketIO.m in Sources */,
19691973
075AAED126847EC4007CA1BD /* LPEnumConstants.m in Sources */,
1974+
6A15EB3D28F6A5DB00878D6C /* LPCTNotificationsManager.swift in Sources */,
19701975
075AAEDA26847EC4007CA1BD /* LPExceptionHandler.m in Sources */,
19711976
075AAE8526847EC4007CA1BD /* LPJSON.m in Sources */,
19721977
);
@@ -2095,6 +2100,7 @@
20952100
39C081AB278D995300C1DBD6 /* ActionManager+Scheduler.swift in Sources */,
20962101
6A714B9826F8B317004A34A9 /* Leanplum_SocketIO.m in Sources */,
20972102
6A714B9926F8B317004A34A9 /* LPEnumConstants.m in Sources */,
2103+
6A15EB3E28F6A5DB00878D6C /* LPCTNotificationsManager.swift in Sources */,
20982104
6A714B9A26F8B317004A34A9 /* LPExceptionHandler.m in Sources */,
20992105
6A714B9B26F8B317004A34A9 /* LPJSON.m in Sources */,
21002106
);

LeanplumSDK/LeanplumSDK/Classes/Internal/Leanplum.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ +(void)load
119119
#endif
120120
}
121121

122-
+ (NotificationsManager*)notificationsManager
122+
+ (LPCTNotificationsManager*)notificationsManager
123123
{
124-
static NotificationsManager *managerInstance = nil;
124+
static LPCTNotificationsManager *managerInstance = nil;
125125
static dispatch_once_t onceLPInternalStateToken;
126126
dispatch_once(&onceLPInternalStateToken, ^{
127-
managerInstance = [NotificationsManager new];
127+
managerInstance = [LPCTNotificationsManager new];
128128
});
129129
return managerInstance;
130130
}
@@ -514,6 +514,7 @@ + (void)startWithUserId:(NSString *)userId userAttributes:(NSDictionary *)attrib
514514

515515
+ (void)triggerStartIssued
516516
{
517+
LPLog(LPDebug, @"Triggering blocks on start issued");
517518
[LPInternalState sharedState].issuedStart = YES;
518519
NSMutableArray* startIssuedBlocks = [LPInternalState sharedState].startIssuedBlocks;
519520

@@ -933,7 +934,7 @@ + (void)startWithUserId:(NSString *)userId
933934
[[LPRequestSender sharedInstance] send:request];
934935
[Leanplum triggerStartIssued];
935936
} else {
936-
[[LPInternalState sharedState] setCalledStart:YES];
937+
[[LPInternalState sharedState] setHasStarted:YES];
937938
[[LPInternalState sharedState] setStartSuccessful:YES];
938939
[Leanplum triggerStartIssued];
939940
[Leanplum triggerStartResponse:YES];
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// LPCTNotificationsManager.swift
3+
// LeanplumSDK
4+
//
5+
// Created by Nikola Zagorchev on 12.10.22.
6+
//
7+
8+
import Foundation
9+
// Use @_implementationOnly to *not* expose CleverTapSDK to the Leanplum-Swift header
10+
@_implementationOnly import CleverTapSDK
11+
12+
@objc public class LPCTNotificationsManager: NotificationsManager {
13+
struct Constants {
14+
static let OpenDeepLinksInForeground = true
15+
}
16+
17+
enum NotificationEvent: String, CustomStringConvertible {
18+
case opened = "Open"
19+
case received = "Receive"
20+
21+
var description: String {
22+
rawValue
23+
}
24+
}
25+
26+
override func notificationOpened(userInfo: [AnyHashable : Any], action: String = LP_VALUE_DEFAULT_PUSH_ACTION) {
27+
if Utilities.messageIdFromUserInfo(userInfo) != nil {
28+
// Handle Leanplum notifications
29+
super.notificationOpened(userInfo: userInfo, action: action)
30+
return
31+
}
32+
handlerCleverTapNotification(userInfo: userInfo, event: .opened)
33+
}
34+
35+
override func notificationReceived(userInfo: [AnyHashable : Any], isForeground: Bool) {
36+
if Utilities.messageIdFromUserInfo(userInfo) != nil {
37+
// Handle Leanplum notifications
38+
super.notificationReceived(userInfo: userInfo, isForeground: isForeground)
39+
return
40+
}
41+
handlerCleverTapNotification(userInfo: userInfo, event: .received)
42+
}
43+
44+
public override func didRegisterForRemoteNotificationsWithDeviceToken(_ deviceToken: Data) {
45+
super.didRegisterForRemoteNotificationsWithDeviceToken(deviceToken)
46+
47+
Log.debug("[Wrapper] Will call CleverTap.setPushToken for didRegisterForRemoteNotifications, when Leanplum has issued start.")
48+
// Leanplum.onStartIssued guarantees that Wrapper is initialized and CT instance is available, if migration has started.
49+
Leanplum.onStartIssued {
50+
if MigrationManager.shared.useCleverTap {
51+
MigrationManager.shared.setPushToken(deviceToken)
52+
}
53+
}
54+
}
55+
56+
func handlerCleverTapNotification(userInfo: [AnyHashable : Any], event: NotificationEvent) {
57+
Log.debug("[Wrapper] Will call CleverTap.handlePushNotification for Push \(event), when Leanplum has issued start.")
58+
// Leanplum.onStartIssued guarantees that Wrapper is initialized and CT instance is available, if migration has started.
59+
Leanplum.onStartIssued {
60+
if MigrationManager.shared.useCleverTap {
61+
Log.debug("""
62+
[Wrapper] Calling CleverTap.handlePushNotification:openDeepLinksInForeground: \
63+
\(Constants.OpenDeepLinksInForeground) for Push \(event)
64+
""")
65+
CleverTap.handlePushNotification(userInfo, openDeepLinksInForeground: Constants.OpenDeepLinksInForeground)
66+
}
67+
}
68+
}
69+
}

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/MigrationManager+API.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
@objc public extension MigrationManager {
99

1010
func launch() {
11-
wrapper?.launch()
11+
guard let wrapper = wrapper else {
12+
Log.debug("[Wrapper] Calling launch before wrapper is initialized.")
13+
return
14+
}
15+
wrapper.launch()
1216
}
1317

1418
var state: MigrationState {
@@ -51,6 +55,10 @@
5155
wrapper?.setUserId(userId)
5256
}
5357

58+
func setPushToken(_ token: Data) {
59+
wrapper?.setPushToken(token)
60+
}
61+
5462
func setTrafficSourceInfo(_ info: [AnyHashable: Any]) {
5563
wrapper?.setTrafficSourceInfo(info)
5664
}

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/MigrationManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Foundation
6060
}
6161

6262
func initWrapper() {
63-
if migrationState.useCleverTap {
63+
if migrationState.useCleverTap, wrapper == nil {
6464
guard let id = accountId, let token = accountToken, let accountRegion = regionCode else {
6565
Log.error("[Wrapper] Missing CleverTap Credentials. Cannot initialize CleverTap.")
6666
return

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/Wrapper/CTWrapper.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,16 @@ class CTWrapper: Wrapper {
4949
}
5050

5151
func launch() {
52-
guard Thread.isMainThread else {
53-
DispatchQueue.main.async { [self] in
54-
launch()
55-
}
56-
return
57-
}
58-
5952
let config = CleverTapInstanceConfig.init(accountId: accountId, accountToken: accountToken, accountRegion: accountRegion)
6053
config.useCustomCleverTapId = true
6154
config.logLevel = CleverTapLogLevel(LPLogManager.logLevel())
6255
cleverTapInstance = CleverTap.instance(with: config, andCleverTapID: identityManager.cleverTapID)
6356
cleverTapInstance?.setLibrary("Leanplum")
64-
57+
// Track App Launched
6558
cleverTapInstance?.notifyApplicationLaunched(withOptions: [:])
66-
6759
Log.debug("[Wrapper] CleverTap instance created with accountId: \(accountId)")
60+
// Set the current push token registered in Leanplum
61+
setCurrentPushToken()
6862

6963
if !identityManager.isAnonymous {
7064
Log.debug("""
@@ -77,18 +71,18 @@ class CTWrapper: Wrapper {
7771
triggerInstanceCallback()
7872
}
7973

74+
// MARK: Callback
75+
func setInstanceCallback(_ callback: ((Any) -> Void)?) {
76+
instanceCallback = callback
77+
triggerInstanceCallback()
78+
}
79+
8080
private func triggerInstanceCallback() {
8181
guard let callback = instanceCallback, let instance = cleverTapInstance else {
8282
return
8383
}
84-
8584
callback(instance)
8685
}
87-
88-
func setInstanceCallback(_ callback: ((Any) -> Void)?) {
89-
instanceCallback = callback
90-
triggerInstanceCallback()
91-
}
9286

9387
// MARK: Tracking
9488
func track(_ eventName: String?, value: Double, info: String?, params: [String: Any]) {
@@ -203,6 +197,18 @@ class CTWrapper: Wrapper {
203197
cleverTapInstance?.onUserLogin(profile, withCleverTapID: cleverTapID)
204198
}
205199

200+
func setPushToken(_ token: Data) {
201+
Log.debug("[Wrapper] Calling CleverTap.setPushToken")
202+
cleverTapInstance?.setPushToken(token)
203+
}
204+
205+
func setCurrentPushToken() {
206+
if let token = Leanplum.user.pushToken {
207+
Log.debug("[Wrapper] Setting current push token using setPushTokenAs")
208+
cleverTapInstance?.setPushTokenAs(token)
209+
}
210+
}
211+
206212
// MARK: Traffic Source
207213
func setTrafficSourceInfo(_ info: [AnyHashable: Any]) {
208214
let source = info["publisherName"] as? String

LeanplumSDK/LeanplumSDK/ClassesSwift/Migration/Wrapper/Wrapper.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ protocol Wrapper {
3131

3232
func setUserId(_ userId: String)
3333

34+
func setPushToken(_ token: Data)
35+
3436
func setTrafficSourceInfo(_ info: [AnyHashable: Any])
3537

3638
func setLogLevel(_ level: LeanplumLogLevel)

LeanplumSDK/LeanplumSDK/ClassesSwift/Notifications/NSObject+Notifications.swift

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ extension NSObject {
7474
}
7575
}
7676

77-
// Do not handle non-Leanplum notifications
78-
guard Utilities.messageIdFromUserInfo(userInfo) != nil else {
79-
return
80-
}
81-
8277
let state = UIApplication.shared.applicationState
8378
// Call notification received or perform action
8479
if #available(iOS 10, *) {
@@ -154,11 +149,6 @@ extension NSObject {
154149
}
155150
}
156151

157-
// Do not handle non-Leanplum notifications
158-
guard Utilities.messageIdFromUserInfo(userInfo) != nil else {
159-
return
160-
}
161-
162152
// Handle UNNotificationDefaultActionIdentifier and Custom Actions
163153
if response.actionIdentifier != UNNotificationDismissActionIdentifier {
164154
let notifWasOpenedFromStart = notificationsProxy.isEqualToHandledNotification(userInfo: userInfo) && notificationsProxy.notificationOpenedFromStart
@@ -190,11 +180,6 @@ extension NSObject {
190180
}
191181
}
192182

193-
// Do not handle non-Leanplum notifications
194-
guard Utilities.messageIdFromUserInfo(notification.request.content.userInfo) != nil else {
195-
return
196-
}
197-
198183
// Notification is received while app is running on foreground
199184
Leanplum.notificationsManager().notificationReceived(userInfo: notification.request.content.userInfo, isForeground: true)
200185
}
@@ -211,8 +196,7 @@ extension NSObject {
211196
}
212197
}
213198

214-
// Do not handle non-Leanplum notifications
215-
guard let userInfo = notification.userInfo, Utilities.messageIdFromUserInfo(userInfo) != nil else {
199+
guard let userInfo = notification.userInfo else {
216200
return
217201
}
218202

0 commit comments

Comments
 (0)