NotificationService.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // NotificationService.swift
  3. // Notification Service Extension
  4. //
  5. // Created by Marino Faggiana on 27/07/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 = ""
  32. bestAttemptContent.body = "Nextcloud notification"
  33. guard let message = bestAttemptContent.userInfo["subject"] else {
  34. contentHandler(bestAttemptContent)
  35. return
  36. }
  37. for result in NCManageDatabase.sharedInstance.getAllAccount() {
  38. guard let privateKey = CCUtility.getPushNotificationPrivateKey(result.account) else {
  39. continue
  40. }
  41. guard let decryptedMessage = NCPushNotificationEncryption.sharedInstance()?.decryptPushNotification(message as? String, withDevicePrivateKey: privateKey) else {
  42. continue
  43. }
  44. guard let data = decryptedMessage.data(using: .utf8) else {
  45. contentHandler(bestAttemptContent)
  46. return
  47. }
  48. do {
  49. let json = try JSONSerialization.jsonObject(with: data) as! [String:AnyObject]
  50. if let app = json["app"] as? String {
  51. if app == "spreed" {
  52. bestAttemptContent.title = "Nextcloud Talk"
  53. } else {
  54. bestAttemptContent.title = app.capitalized
  55. }
  56. }
  57. if let subject = json["subject"] as? String {
  58. bestAttemptContent.body = subject
  59. }
  60. } catch let error as NSError {
  61. print("Failed : \(error.localizedDescription)")
  62. }
  63. }
  64. contentHandler(bestAttemptContent)
  65. }
  66. }
  67. override func serviceExtensionTimeWillExpire() {
  68. if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
  69. bestAttemptContent.title = ""
  70. bestAttemptContent.body = "Nextcloud"
  71. contentHandler(bestAttemptContent)
  72. }
  73. }
  74. }