NotificationService.swift 3.4 KB

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