|
| 1 | +import UserNotifications |
| 2 | + |
| 3 | +import OneSignalExtension |
| 4 | + |
| 5 | +class NotificationService: UNNotificationServiceExtension { |
| 6 | + |
| 7 | + var contentHandler: ((UNNotificationContent) -> Void)? |
| 8 | + var receivedRequest: UNNotificationRequest! |
| 9 | + var bestAttemptContent: UNMutableNotificationContent? |
| 10 | + |
| 11 | + override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { |
| 12 | + self.receivedRequest = request |
| 13 | + self.contentHandler = contentHandler |
| 14 | + self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) |
| 15 | + |
| 16 | + if let bestAttemptContent = bestAttemptContent { |
| 17 | + /* DEBUGGING: Uncomment the 2 lines below to check this extension is executing |
| 18 | + Note, this extension only runs when mutable-content is set |
| 19 | + Setting an attachment or action buttons automatically adds this */ |
| 20 | + // print("Running NotificationServiceExtension") |
| 21 | + // bestAttemptContent.body = "[Modified] " + bestAttemptContent.body |
| 22 | + |
| 23 | + OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + override func serviceExtensionTimeWillExpire() { |
| 28 | + // Called just before the extension will be terminated by the system. |
| 29 | + // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. |
| 30 | + if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { |
| 31 | + OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent) |
| 32 | + contentHandler(bestAttemptContent) |
| 33 | + } |
| 34 | + } |
| 35 | +} |
0 commit comments