Browse Source

Decrypt push notifications using Notification Service Extension.

Signed-off-by: Ivan Sein <ivan@nextcloud.com>
Ivan Sein 5 years ago
parent
commit
dccdce6095

+ 25 - 2
Notification Service Extension/NotificationService.swift

@@ -31,8 +31,29 @@ class NotificationService: UNNotificationServiceExtension {
         bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
         
         if let bestAttemptContent = bestAttemptContent {
-            // Modify the notification content here...
-            bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
+            bestAttemptContent.title = ""
+            bestAttemptContent.body = "Nextcloud notification"
+            do {
+                let message = bestAttemptContent.userInfo["subject"] as! String
+                let tableAccounts = NCManageDatabase.sharedInstance.getAllAccount()
+                for tableAccount in tableAccounts {
+                    guard let privateKey = CCUtility.getPushNotificationPrivateKey(tableAccount.account) else {
+                        continue
+                    }
+                    guard let decryptedMessage = NCPushNotificationEncryption.sharedInstance().decryptPushNotification(message, withDevicePrivateKey: privateKey) else {
+                        continue
+                    }
+                    guard let data = decryptedMessage.data(using: .utf8) else {
+                        continue
+                    }
+                    let json = try JSONSerialization.jsonObject(with: data) as! [String:AnyObject]
+                    if let subject = json["subject"] as? String {
+                        bestAttemptContent.body = subject
+                    }
+                }
+            } catch let error as NSError {
+                print("Failed : \(error.localizedDescription)")
+            }
             
             contentHandler(bestAttemptContent)
         }
@@ -42,6 +63,8 @@ class NotificationService: UNNotificationServiceExtension {
         // Called just before the extension will be terminated by the system.
         // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
         if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
+            bestAttemptContent.title = ""
+            bestAttemptContent.body = "Nextcloud notification"
             contentHandler(bestAttemptContent)
         }
     }

+ 10 - 0
Notification Service Extension/Notification_Service_Extension-Bridging-Header.h

@@ -19,4 +19,14 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //
 
+#import "CCUtility.h"
+#import "NCPushNotificationEncryption.h"
 
+#import "OCActivity.h"
+#import "OCUserProfile.h"
+#import "OCCapabilities.h"
+#import "OCExternalSites.h"
+#import "OCSharedDto.h"
+
+#import "HCFeatures.h"
+#import "NCComments.h"