NotificationService.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // NotificationService.swift
  3. // ntfyNSE2
  4. //
  5. // Created by Philipp Heckel on 5/12/22.
  6. //
  7. import UserNotifications
  8. class NotificationService: UNNotificationServiceExtension {
  9. var contentHandler: ((UNNotificationContent) -> Void)?
  10. var bestAttemptContent: UNMutableNotificationContent?
  11. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  12. self.contentHandler = contentHandler
  13. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  14. if let bestAttemptContent = bestAttemptContent {
  15. // Modify the notification content here...
  16. bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
  17. print("hi")
  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. }