NotificationService.swift 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // NotificationService.swift
  3. // Notification Service Extension
  4. //
  5. // Created by Marino Faggiana on 27/07/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. import UserNotifications
  9. class NotificationService: UNNotificationServiceExtension {
  10. var contentHandler: ((UNNotificationContent) -> Void)?
  11. var bestAttemptContent: UNMutableNotificationContent?
  12. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  13. self.contentHandler = contentHandler
  14. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  15. if let bestAttemptContent = bestAttemptContent {
  16. // Modify the notification content here...
  17. bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
  18. contentHandler(bestAttemptContent)
  19. }
  20. }
  21. override func serviceExtensionTimeWillExpire() {
  22. // Called just before the extension will be terminated by the system.
  23. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  24. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  25. contentHandler(bestAttemptContent)
  26. }
  27. }
  28. }