-
Notifications
You must be signed in to change notification settings - Fork 16
/
MessagingPush.swift
85 lines (74 loc) · 3.21 KB
/
MessagingPush.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import CioInternalCommon
import CioTracking
import Foundation
/**
Swift code goes into this module that are common to *all* of the Messaging Push modules (APN, FCM, etc).
So, performing an HTTP request to the API with a device token goes here.
*/
public class MessagingPush: ModuleTopLevelObject<MessagingPushInstance>, MessagingPushInstance {
@Atomic public private(set) static var shared = MessagingPush()
private var globalDataStore: GlobalDataStore
// testing constructor
internal init(implementation: MessagingPushInstance?, globalDataStore: GlobalDataStore, sdkInitializedUtil: SdkInitializedUtil) {
self.globalDataStore = globalDataStore
super.init(implementation: implementation, sdkInitializedUtil: sdkInitializedUtil)
}
// singleton constructor
override private init() {
self.globalDataStore = CioGlobalDataStore.getInstance()
super.init()
}
// for testing
internal static func resetSharedInstance() {
Self.shared = MessagingPush()
}
// At this time, we do not require `MessagingPush.initialize()` to be called to make the SDK work. There is
// currently no module initialization to perform.
public static func initialize() {
MessagingPush.shared.initializeModuleIfSdkInitialized()
}
override public func inititlizeModule(diGraph: DIGraph) {
let logger = diGraph.logger
logger.debug("Setting up MessagingPush module...")
logger.info("MessagingPush module setup with SDK")
}
override public func getImplementationInstance(diGraph: DIGraph) -> MessagingPushInstance {
MessagingPushImplementation(diGraph: diGraph)
}
/**
Register a new device token with Customer.io, associated with the current active customer. If there
is no active customer, this will fail to register the device
*/
public func registerDeviceToken(_ deviceToken: String) {
// Compare the new deviceToken with the one stored in globalDataStore.
// If they are different, proceed with registering the device token.
// This check helps to avoid duplicate requests, as registerDeviceToken is already called on SDK initialization.
if deviceToken != globalDataStore.pushDeviceToken {
// Call the registerDeviceToken method on the implementation.
// This method is responsible for registering the device token and updating the globalDataStore as well.
if let implementation = implementation {
implementation.registerDeviceToken(deviceToken)
} else {
// Update the globalDataStore with the new device token.
// The implementation may be nil due to lifecycle issues in wrappers SDKs.
globalDataStore.pushDeviceToken = deviceToken
}
}
}
/**
Delete the currently registered device token
*/
public func deleteDeviceToken() {
implementation?.deleteDeviceToken()
}
/**
Track a push metric
*/
public func trackMetric(
deliveryID: String,
event: Metric,
deviceToken: String
) {
implementation?.trackMetric(deliveryID: deliveryID, event: event, deviceToken: deviceToken)
}
}