NotificationService.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. bestAttemptContent.title = ""
  30. bestAttemptContent.body = "Nextcloud notification"
  31. do {
  32. let message = bestAttemptContent.userInfo["subject"] as! String
  33. let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
  34. for tableAccount in tableAccounts {
  35. guard let privateKey = CCUtility.getPushNotificationPrivateKey(tableAccount.account) else {
  36. continue
  37. }
  38. guard let decryptedMessage = NCPushNotificationEncryption.sharedInstance().decryptPushNotification(message, withDevicePrivateKey: privateKey) else {
  39. continue
  40. }
  41. guard let data = decryptedMessage.data(using: .utf8) else {
  42. continue
  43. }
  44. let json = try JSONSerialization.jsonObject(with: data) as! [String:AnyObject]
  45. if let subject = json["subject"] as? String {
  46. bestAttemptContent.body = subject
  47. }
  48. }
  49. } catch let error as NSError {
  50. print("Failed : \(error.localizedDescription)")
  51. }
  52. contentHandler(bestAttemptContent)
  53. }
  54. }
  55. override func serviceExtensionTimeWillExpire() {
  56. // Called just before the extension will be terminated by the system.
  57. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  58. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  59. bestAttemptContent.title = ""
  60. bestAttemptContent.body = "Nextcloud notification"
  61. contentHandler(bestAttemptContent)
  62. }
  63. }
  64. }