Last active
December 5, 2018 11:26
-
-
Save yunustek/7e8c0678d3806e60c34b29e895053426 to your computer and use it in GitHub Desktop.
NotificationService.Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UserNotifications | |
class NotificationService: UNNotificationServiceExtension { | |
var contentHandler: ((UNNotificationContent) -> Void)? | |
var bestAttemptContent: UNMutableNotificationContent? | |
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { | |
self.contentHandler = contentHandler | |
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) | |
if let bestAttemptContent = bestAttemptContent { | |
// Modify the notification content here... | |
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" | |
contentHandler(bestAttemptContent) | |
} | |
} | |
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 { | |
contentHandler(bestAttemptContent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment