NotificationService.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //
  2. // NotificationService.swift
  3. // Notification Service Extension
  4. //
  5. // Created by Ivan Sein on 30.01.20.
  6. // Author Ivan Sein <ivan@nextcloud.com>
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. import UserNotifications
  22. class NotificationService: UNNotificationServiceExtension {
  23. var contentHandler: ((UNNotificationContent) -> Void)?
  24. var bestAttemptContent: UNMutableNotificationContent?
  25. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  26. self.contentHandler = contentHandler
  27. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  28. if let bestAttemptContent = bestAttemptContent {
  29. // Modify the notification content here...
  30. bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
  31. contentHandler(bestAttemptContent)
  32. }
  33. }
  34. override func serviceExtensionTimeWillExpire() {
  35. // Called just before the extension will be terminated by the system.
  36. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  37. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  38. contentHandler(bestAttemptContent)
  39. }
  40. }
  41. }