Browse Source

ApplicationHandle push notification

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 1 year ago
parent
commit
9823886df4

+ 5 - 0
ExternalResources/NCApplicationHandle.swift

@@ -30,6 +30,11 @@ class NCApplicationHandle: NSObject {
 
     let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
 
+    // class: AppDelegate
+    // func applicationWillEnterForeground(_ application: UIApplication)
+    func applicationWillEnterForeground(_ application: UIApplication) {
+    }
+    
     // class: AppDelegate
     // func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void)
     func applicationOpenUserActivity(_ userActivity: NSUserActivity) -> Bool {

+ 26 - 0
iOSClient/AppDelegate.swift

@@ -60,6 +60,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     private var privacyProtectionWindow: UIWindow?
 
+    struct PushNotificationData {
+        var account: String
+        var json: [String: AnyObject]
+    }
+    var pushNotificationData: PushNotificationData = PushNotificationData(account: "", json: [:])
+
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
 
         NCSettingsBundleHelper.checkAndExecuteSettings(delay: 0)
@@ -206,6 +212,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     // L' applicazione entrerà in primo piano (dopo il background)
     func applicationWillEnterForeground(_ application: UIApplication) {
+        NCApplicationHandle().applicationWillEnterForeground(application)
         guard !account.isEmpty, let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return }
 
         NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Application will enter in foreground")
@@ -404,6 +411,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     // MARK: - Push Notifications
 
     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
+        do {
+            let userInfo = notification.request.content.userInfo
+            if let message = userInfo["subject"] as? String {
+                let tableAccounts = NCManageDatabase.shared.getAllAccount()
+                for tableAccount in tableAccounts {
+                    guard let privateKey = CCUtility.getPushNotificationPrivateKey(tableAccount.account),
+                          let decryptedMessage = NCPushNotificationEncryption.shared().decryptPushNotification(message, withDevicePrivateKey: privateKey),
+                          let data = decryptedMessage.data(using: .utf8) else {
+                        continue
+                    }
+                    if let json = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject] {
+                        pushNotificationData = PushNotificationData(account: tableAccount.account, json: json)
+                        print(json)
+                    }
+                }
+            }
+        } catch let error as NSError {
+            print("Failed : \(error.localizedDescription)")
+        }
         completionHandler([.list, .banner, .sound])
     }
 

+ 1 - 0
iOSClient/Nextcloud-Bridging-Header.h

@@ -8,3 +8,4 @@
 #import "UIImage+animatedGIF.h"
 #import "CCUtility.h"
 #import "NCPushNotification.h"
+#import "NCPushNotificationEncryption.h"