NotificationService.swift 1.2 KB

123456789101112131415161718192021222324252627
  1. import FirebaseMessaging
  2. import UserNotifications
  3. class NotificationService: UNNotificationServiceExtension {
  4. var contentHandler: ((UNNotificationContent) -> Void)?
  5. var bestAttemptContent: UNMutableNotificationContent?
  6. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  7. self.contentHandler = contentHandler
  8. bestAttemptContent = request.content
  9. .mutableCopy() as? UNMutableNotificationContent
  10. guard let bestAttemptContent = bestAttemptContent else { return }
  11. FIRMessagingExtensionHelper().populateNotificationContent(
  12. bestAttemptContent,
  13. withContentHandler: contentHandler)
  14. }
  15. override func serviceExtensionTimeWillExpire() {
  16. // Called just before the extension will be terminated by the system.
  17. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  18. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  19. contentHandler(bestAttemptContent)
  20. }
  21. }
  22. }