Browse Source

fix missin scan image + suiteName userdefaults for PushNotification

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

+ 2 - 2
ExternalResources/NCApplicationHandle.swift

@@ -31,8 +31,8 @@ class NCApplicationHandle: NSObject {
     let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
 
     // class: AppDelegate
-    // func applicationWillEnterForeground(_ application: UIApplication)
-    func applicationWillEnterForeground(_ application: UIApplication) {
+    // func nextcloudPushNotificationAction(data: [String: AnyObject])
+    func nextcloudPushNotificationAction(data: [String: AnyObject]) {
     }
     
     // class: AppDelegate

+ 2 - 2
Nextcloud.xcodeproj/project.pbxproj

@@ -3970,7 +3970,7 @@
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 2;
+				CURRENT_PROJECT_VERSION = 3;
 				DEVELOPMENT_TEAM = NKUJUXUJ3B;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;
@@ -4033,7 +4033,7 @@
 				CLANG_WARN_UNREACHABLE_CODE = YES;
 				CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
 				COPY_PHASE_STRIP = NO;
-				CURRENT_PROJECT_VERSION = 2;
+				CURRENT_PROJECT_VERSION = 3;
 				DEVELOPMENT_TEAM = NKUJUXUJ3B;
 				ENABLE_STRICT_OBJC_MSGSEND = YES;
 				ENABLE_TESTABILITY = YES;

+ 17 - 15
Notification Service Extension/NotificationService.swift

@@ -35,21 +35,23 @@ class NotificationService: UNNotificationServiceExtension {
             bestAttemptContent.title = ""
             bestAttemptContent.body = "Nextcloud notification"
             do {
-                let message = bestAttemptContent.userInfo["subject"] as! String
-                let tableAccounts = NCManageDatabase.shared.getAllAccount()
-                for tableAccount in tableAccounts {
-                    guard let privateKey = CCUtility.getPushNotificationPrivateKey(tableAccount.account) else {
-                        continue
-                    }
-                    guard let decryptedMessage = NCPushNotificationEncryption.shared().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
+                if let message = bestAttemptContent.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 var json = try JSONSerialization.jsonObject(with: data) as? [String: AnyObject],
+                           let subject = json["subject"] as? String {
+                            bestAttemptContent.body = subject
+                            if let pref = UserDefaults.init(suiteName: NCBrandOptions.shared.capabilitiesGroups) {
+                                json["account"] = tableAccount.account as AnyObject
+                                pref.set(json, forKey: "NOTIFICATION_DATA")
+                                pref.synchronize()
+                            }
+                        }
                     }
                 }
             } catch let error as NSError {

+ 1 - 1
Widget/Files/FilesWidgetView.swift

@@ -127,7 +127,7 @@ struct FilesWidgetView: View {
                     })
 
                     Link(destination: entry.isPlaceholder ? linkNoAction : linkActionScanDocument, label: {
-                        Image("scan")
+                        Image(systemName: "doc.text.viewfinder")
                             .resizable()
                             .renderingMode(.template)
                             .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))

+ 1 - 1
Widget/Toolbar/ToolbarWidgetView.swift

@@ -61,7 +61,7 @@ struct ToolbarWidgetView: View {
                     })
 
                     Link(destination: entry.isPlaceholder ? linkNoAction : linkActionScanDocument, label: {
-                        Image("scan")
+                        Image(systemName: "doc.text.viewfinder")
                             .resizable()
                             .renderingMode(.template)
                             .foregroundColor(entry.isPlaceholder ? Color(.systemGray4) : Color(NCBrandColor.shared.brandText))

+ 11 - 26
iOSClient/AppDelegate.swift

@@ -59,12 +59,6 @@ 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)
@@ -205,12 +199,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Initialize Auto upload with \(items) uploads")
         }
 
+        // Push Notification
+        if let pref = UserDefaults.init(suiteName: NCBrandOptions.shared.capabilitiesGroups),
+           let data = pref.object(forKey: "NOTIFICATION_DATA") as? [String: AnyObject] {
+            nextcloudPushNotificationAction(data: data)
+            pref.set(nil, forKey: "NOTIFICATION_DATA")
+        }
+
         NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterApplicationDidBecomeActive)
     }
 
     // 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")
@@ -409,25 +409,6 @@ 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])
     }
 
@@ -449,6 +430,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         }
     }
 
+    func nextcloudPushNotificationAction(data: [String: AnyObject]) {
+        NCApplicationHandle().nextcloudPushNotificationAction(data: data)
+    }
+
     // MARK: - Login & checkErrorNetworking
 
     @objc func openLogin(viewController: UIViewController?, selector: Int, openLoginWeb: Bool) {