NotificationService.swift 3.2 KB

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