-
Notifications
You must be signed in to change notification settings - Fork 16
/
MessagingPushFCM.swift
121 lines (103 loc) · 3.91 KB
/
MessagingPushFCM.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import CioInternalCommon
import CioMessagingPush
import CioTracking
import Foundation
#if canImport(UserNotifications)
import UserNotifications
#endif
// Some functions are copied from MessagingPush because
// 1. This allows the generated mock to contain these functions
// 2. Customers do not need to `import CioMessaginPush`. Only 1 import: `CioMessaginPushFCM`.
public protocol MessagingPushFCMInstance: AutoMockable {
func registerDeviceToken(fcmToken: String?)
// sourcery:Name=didReceiveRegistrationToken
func messaging(
_ messaging: Any,
didReceiveRegistrationToken fcmToken: String?
)
// sourcery:Name=didFailToRegisterForRemoteNotifications
func application(
_ application: Any,
didFailToRegisterForRemoteNotificationsWithError error: Error
)
func deleteDeviceToken()
func trackMetric(
deliveryID: String,
event: Metric,
deviceToken: String
)
#if canImport(UserNotifications)
@discardableResult
// sourcery:Name=didReceiveNotificationRequest
// sourcery:IfCanImport=UserNotifications
func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) -> Bool
// sourcery:IfCanImport=UserNotifications
func serviceExtensionTimeWillExpire()
#endif
}
public class MessagingPushFCM: MessagingPushFCMInstance {
internal static let shared = MessagingPushFCM()
internal var messagingPush: MessagingPushInstance {
MessagingPush.shared
}
public func registerDeviceToken(fcmToken: String?) {
guard let deviceToken = fcmToken else {
return
}
messagingPush.registerDeviceToken(deviceToken)
}
public func messaging(_ messaging: Any, didReceiveRegistrationToken fcmToken: String?) {
guard let deviceToken = fcmToken else {
return
}
registerDeviceToken(fcmToken: deviceToken)
}
public func application(_ application: Any, didFailToRegisterForRemoteNotificationsWithError error: Error) {
messagingPush.deleteDeviceToken()
}
public func deleteDeviceToken() {
messagingPush.deleteDeviceToken()
}
public func trackMetric(deliveryID: String, event: Metric, deviceToken: String) {
messagingPush.trackMetric(deliveryID: deliveryID, event: event, deviceToken: deviceToken)
}
#if canImport(UserNotifications)
/**
- returns:
Bool indicating if this push notification is one handled by Customer.io SDK or not.
If function returns `false`, `contentHandler` will *not* be called by the SDK.
*/
@discardableResult
public func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) -> Bool {
messagingPush.didReceive(request, withContentHandler: contentHandler)
}
/**
iOS OS telling the notification service to hurry up and stop modifying the push notifications.
Stop all network requests and modifying and show the push for what it looks like now.
*/
public func serviceExtensionTimeWillExpire() {
messagingPush.serviceExtensionTimeWillExpire()
}
@available(iOSApplicationExtension, unavailable)
public func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) -> CustomerIOParsedPushPayload? {
messagingPush.userNotificationCenter(center, didReceive: response)
}
@available(iOSApplicationExtension, unavailable)
public func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) -> Bool {
messagingPush.userNotificationCenter(center, didReceive: response, withCompletionHandler: completionHandler)
}
#endif
}