Skip to content

Commit

Permalink
feat: automatic push events (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
hownowstephen authored Sep 25, 2021
1 parent b720467 commit cf81b23
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
30 changes: 30 additions & 0 deletions Sources/MessagingPush/MessagingPush+NotificationCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,20 @@ public extension MessagingPush {
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) -> Bool {
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier, UNNotificationDefaultActionIdentifier:
if customerIO.sdkConfig.autoTrackPushEvents {
trackMetric(notificationContent: response.notification.request.content, event: .delivered) { _ in
// XXX: pending background queue so that this can get retried instead of discarding the result
}
}
default: break
}

guard let pushContent = PushContent.parse(notificationContent: response.notification.request.content,
jsonAdapter: DITracking.shared.jsonAdapter)
else {
// push does not contain a CIO rich payload, so end early
return false
}

Expand All @@ -32,6 +43,12 @@ public extension MessagingPush {
if let deepLinkurl = pushContent.deepLink {
UIApplication.shared.open(url: deepLinkurl)

if customerIO.sdkConfig.autoTrackPushEvents {
trackMetric(notificationContent: response.notification.request.content, event: .opened) { _ in
// XXX: pending background queue so that this can get retried instead of discarding the result
}
}

completionHandler()

return true
Expand All @@ -42,6 +59,19 @@ public extension MessagingPush {
return false
}

func trackMetric(
notificationContent: UNNotificationContent,
event: Metric,
onComplete: @escaping (Result<Void, CustomerIOError>) -> Void
) {
guard let deliveryID: String = notificationContent.userInfo["CIO-Delivery-ID"] as? String,
let deviceToken: String = notificationContent.userInfo["CIO-Delivery-Token"] as? String else {
return onComplete(Result.success(()))
}

trackMetric(deliveryID: deliveryID, event: event, deviceToken: deviceToken, onComplete: onComplete)
}

private func cleanup(pushContent: PushContent) {
pushContent.cioAttachments.forEach { attachment in
let localFilePath = attachment.url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ public extension MessagingPush {
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) -> Bool {
if customerIO.sdkConfig.autoTrackPushEvents {
trackMetric(notificationContent: request.content, event: .delivered) { _ in
// XXX: pending background queue so that this can get retried instead of discarding the result
}
}

guard let pushContent = PushContent.parse(notificationContent: request.content,
jsonAdapter: DITracking.shared.jsonAdapter)
else {
// push is not sent by CIO. Therefore, end early.
// push does not contain a CIO rich payload, so end early
return false
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/Tracking/Store/SdkConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public struct SdkConfig {
*/
var trackingApiUrl: String = ""

/**
Automatic tracking of push events will automatically generate `opened` and `delivered` metrics
for push notifications sent by Customer.io
*/
public var autoTrackPushEvents: Bool = true

internal var httpBaseUrls: HttpBaseUrls {
HttpBaseUrls(trackingApi: trackingApiUrl)
}
Expand Down

0 comments on commit cf81b23

Please sign in to comment.