NotificationService.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if 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),
  37. let decryptedMessage = NCPushNotificationEncryption.shared().decryptPushNotification(message, withDevicePrivateKey: privateKey),
  38. let data = decryptedMessage.data(using: .utf8) else {
  39. continue
  40. }
  41. if var json = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject],
  42. let subject = json["subject"] as? String {
  43. bestAttemptContent.body = subject
  44. if let pref = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroups) {
  45. json["account"] = tableAccount.account as AnyObject
  46. pref.set(json, forKey: "NOTIFICATION_DATA")
  47. pref.synchronize()
  48. }
  49. }
  50. }
  51. }
  52. } catch let error as NSError {
  53. print("Failed : \(error.localizedDescription)")
  54. }
  55. contentHandler(bestAttemptContent)
  56. }
  57. }
  58. override func serviceExtensionTimeWillExpire() {
  59. // Called just before the extension will be terminated by the system.
  60. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  61. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  62. bestAttemptContent.title = ""
  63. bestAttemptContent.body = "Nextcloud notification"
  64. contentHandler(bestAttemptContent)
  65. }
  66. }
  67. }