无法从CloudKit notificationInfo和USERINFO结构得到CKRecord.ID

问题描述 投票:0回答:1

我已经安装了CloudKit应用公共数据库的订阅。我想要从返回的notificationInfo的recordName。我一直没能找到任何文件来说明如何检索与框架方法recordName,所以我试图解析信息手动这是行不通的,但是这似乎严重暴力,我不知道我能指望该notificationInfo结构始终是相同的。任何指导将不胜感激。这里是代码片段。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    var changedCKRecord : String

    let dict = userInfo as! [String : NSObject]
    let notification = CKNotification(fromRemoteNotificationDictionary: dict)
    let db = CloudKitDatabase.shared

    if notification.subscriptionID == db.ckStyleSubscriptionID {
        db.handleNotification()
        completionHandler(.newData)
        print(".newData for CKStyle returned")

        for (key,value) in dict {
            if key == "ck" {
                let newDict : [String : NSObject] = value as! [String : NSObject]
                for (key2,value2) in newDict {
                    if key2 == "qry" {
                        let thirdDict : [String : NSObject] = value2 as! [String : NSObject]
                        for (key3,value3) in thirdDict {
                            if key3 == "rid" {
                                if let end = value3 as? String {
                                    changedCKRecord = end
                                    print(changedCKRecord as Any)
                                }
                            }//if key3
                        }//for key3
                    }//if key2
                }//for key 2 in
            }//if key is ck
        }//for in

    } else {
        completionHandler(.noData)
        print(notification.subscriptionID as Any)
        print(db.ckStyleSubscriptionID)
        print(".noData for CKStyle returned")
    }//if notification else

}//didReceiveRemoteNotification

如果我打印notificationInfo:

[AnyHashable("aps"): {"content-available" = 1;},
AnyHashable("ck"): {
ce = 2;
cid = "iCloud. the real bundle identifier  ";
ckuserid = "_c4478d4d3a212da39cca27eb17dffe03";
nid = "58372f81-d10b-4e3b-98ae-40a0d1e048a2";
qry =     {
dbs = 2;
fo = 2;
rid = "97DB306F-3277-07A1-04F5-40228A5EF036";
sid = "ckstyle-changes";
zid = "_defaultZone";
zoid = "_defaultOwner";
};
}]
ios swift push-notification cloudkit
1个回答
2
投票

如果通知是订阅的结果,那么CKNotification(fromRemoteNotificationDictionary:)实际上将返回CKQueryNotification的一个实例。然后,您可以访问CKQueryNotificationrecordID属性来获得改变该记录的ID。

let dict = userInfo as! [String : NSObject]
let notification = CKNotification(fromRemoteNotificationDictionary: dict)
if let queryNotification = notification as? CKQueryNotification,
   let recordName = queryNotification.recordID?.recordName {
       print("Record name is \(recordName)")
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.