Reputation: 4300
I have setup a subscription on the public database for a CloudKit application. I want to retrieve the recordName from the notificationInfo that is returned. I have not been able to find any documentation to explain how to retrieve the recordName with framework methods, so I have attempted to parse the info manually it does not work, but this seems seriously brute force and I'm not sure I can count on the notificationInfo structure always being the same. Any guidance would be appreciated. Here is the code snippet.
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
If I print the 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";
};
}]
Upvotes: 1
Views: 346
Reputation: 114783
If the notification is a result of a subscription, then CKNotification(fromRemoteNotificationDictionary:)
will actually return an instance of CKQueryNotification
. You can then access the CKQueryNotification
's recordID
property to obtain the id of the record that changed.
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)")
}
}
Upvotes: 4