-
Notifications
You must be signed in to change notification settings - Fork 13
/
NotificationService.swift
53 lines (47 loc) · 2.2 KB
/
NotificationService.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
//
// NotificationService.swift
// NotificationServiceExtension
//
// Created by Ajay Subramanya on 6/14/23.
//
import KlaviyoSwiftExtension
import UIKit
import UserNotifications
// MARK: notification service extension implementation.
/// When push payload is marked as there being mutable-content this service
/// (more specifically the `didReceiveNotificationRequest` ) is called to perform
/// tasks such as downloading images and attaching it to the notification before it's displayed to the user.
///
/// There is a limited time before which `didReceiveNotificationRequest` needs to wrap up it's operations
/// else the notification is displayed as received.
///
/// Any property from `UNMutableNotificationContent` can be mutated here before presenting the notification.
class NotificationService: UNNotificationServiceExtension {
var request: UNNotificationRequest!
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.request = request
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
KlaviyoExtensionSDK.handleNotificationServiceDidReceivedRequest(
request: self.request,
bestAttemptContent: bestAttemptContent,
contentHandler: contentHandler)
}
}
override func serviceExtensionTimeWillExpire() {
/// Called just before the extension will be terminated by the system.
/// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler,
let bestAttemptContent = bestAttemptContent {
KlaviyoExtensionSDK.handleNotificationServiceExtensionTimeWillExpireRequest(
request: request,
bestAttemptContent: bestAttemptContent,
contentHandler: contentHandler)
}
}
}