NotificationService.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UserNotifications
  24. class NotificationService: UNNotificationServiceExtension {
  25. var contentHandler: ((UNNotificationContent) -> Void)?
  26. var bestAttemptContent: UNMutableNotificationContent?
  27. override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
  28. self.contentHandler = contentHandler
  29. bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
  30. if let bestAttemptContent = bestAttemptContent {
  31. bestAttemptContent.title = "Nextcloud notification 🔔"
  32. bestAttemptContent.body = ""
  33. let message = bestAttemptContent.userInfo["subject"] as! String
  34. guard let decryptedMessage = NCPushNotification.sharedInstance().decryptPushNotification(message, withDevicePrivateKey: CCUtility.getPushNotificationPrivateKey()) else {
  35. contentHandler(bestAttemptContent)
  36. return
  37. }
  38. bestAttemptContent.body = decryptedMessage
  39. contentHandler(bestAttemptContent)
  40. }
  41. }
  42. override func serviceExtensionTimeWillExpire() {
  43. // Called just before the extension will be terminated by the system.
  44. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  45. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  46. contentHandler(bestAttemptContent)
  47. }
  48. }
  49. }