| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import UserNotifications
- import OneSignal
- class NotificationService: UNNotificationServiceExtension {
-
- var contentHandler: ((UNNotificationContent) -> Void)?
- var receivedRequest: UNNotificationRequest!
- var bestAttemptContent: UNMutableNotificationContent?
-
- override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
- self.receivedRequest = request
- self.contentHandler = contentHandler
- self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
-
- if let bestAttemptContent = bestAttemptContent {
- //If your SDK version is < 3.5.0 uncomment and use this code:
- /*
- OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: self.bestAttemptContent)
- contentHandler(bestAttemptContent)
- */
-
- /* DEBUGGING: Uncomment the 2 lines below to check this extension is excuting
- Note, this extension only runs when mutable-content is set
- Setting an attachment or action buttons automatically adds this */
- //OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
- //bestAttemptContent.body = "[Modified] " + bestAttemptContent.body
-
- OneSignal.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.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 {
- OneSignal.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
- contentHandler(bestAttemptContent)
- }
- }
-
- }
|