-
Notifications
You must be signed in to change notification settings - Fork 16
/
MessagingPush+RichPush.swift
123 lines (106 loc) · 4.55 KB
/
MessagingPush+RichPush.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
122
123
import CioInternalCommon
import CioTracking
import Foundation
#if canImport(UserNotifications) && canImport(UIKit)
import UIKit
import UserNotifications
#endif
public extension MessagingPush {
#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
func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) -> Bool {
guard let implementation = implementation else {
contentHandler(request.content)
return false
}
return implementation.didReceive(request, withContentHandler: contentHandler)
}
/**
iOS 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.
*/
func serviceExtensionTimeWillExpire() {
implementation?.serviceExtensionTimeWillExpire()
}
#endif
}
extension MessagingPushImplementation {
#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
func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) -> Bool {
logger.info("did receive notification request. Checking if message was a push sent from Customer.io...")
logger.debug("notification request: \(request.content.userInfo)")
guard let deliveryID: String = request.content.userInfo["CIO-Delivery-ID"] as? String,
let deviceToken: String = request.content.userInfo["CIO-Delivery-Token"] as? String
else {
logger.info("the notification was not sent by Customer.io. Ignoring notification request.")
return false
}
logger.info("push was sent from Customer.io. Processing the request...")
if sdkConfig.autoTrackPushEvents {
logger.info("automatically tracking push metric: delivered")
logger.debug("parsed deliveryId \(deliveryID), deviceToken: \(deviceToken)")
trackMetric(deliveryID: deliveryID, event: .delivered, deviceToken: deviceToken)
}
if let richPushContent = CustomerIOParsedPushPayload.parse(
notificationContent: request.content,
jsonAdapter: jsonAdapter
) {
logger
.info("""
Parsing notification request to display rich content such as images, deep links, etc.
""")
logger.debug("push content: \(richPushContent)")
RichPushRequestHandler.shared.startRequest(
request,
content: richPushContent
) { notificationContent in
self.logger.debug("rich push was composed \(notificationContent).")
self.finishTasksThenReturn(contentHandler: contentHandler, notificationContent: notificationContent)
}
} else {
logger.info("the push was a simple push, not a rich push. Processing is complete.")
finishTasksThenReturn(contentHandler: contentHandler, notificationContent: request.content)
}
return true
}
private func finishTasksThenReturn(
contentHandler: @escaping (UNNotificationContent) -> Void,
notificationContent: UNNotificationContent
) {
logger
.debug(
"running all background queue tasks and waiting until complete to prevent OS from killing notification service extension before all HTTP requests have been performed"
)
backgroundQueue.run {
self.logger.debug("all background queue tasks done running.")
self.logger.info("Customer.io push processing is done!")
contentHandler(notificationContent)
}
}
/**
iOS 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.
*/
func serviceExtensionTimeWillExpire() {
logger.info("notification service time will expire. Stopping all notification requests early.")
RichPushRequestHandler.shared.stopAll()
}
#endif
}