如何使用CloudKit的通知传递数据?
How to pass data using CloudKit's notification?
我正试图通过 CloudKit 的订阅在我的通知中传递一个字符串。但是,当我在我的 iPhone 中打印这个字符串时,它是零!这是我的代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
print(cloudKitNotification)
if cloudKitNotification.notificationType == CKNotificationType.Query {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("performReload", object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("performReloadItem", object: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("EventoViewController") as! EventoViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationViewController, animated: false)
})
}
}
这是我设置订阅时的样子:
let predicateItem = NSPredicate(format: "self contains '\(self.eventoID)'")
let subscriptionItem = CKSubscription(recordType: "Itens",
predicate: predicateItem,
options: [.FiresOnRecordCreation, .FiresOnRecordUpdate])
let notificationInfoItem = CKNotificationInfo()
notificationInfoItem.shouldSendContentAvailable = true
notificationInfoItem.desiredKeys = ["idEvento"]
notificationInfoItem.shouldBadge = false
subscriptionItem.notificationInfo = notificationInfo
let publicdbItem = CKContainer.defaultContainer().publicCloudDatabase
publicdbItem.saveSubscription(subscriptionItem) { (returnRecord, error) in
if error != nil {
print(error?.localizedDescription)
} else {
print("ITEM subscription ok!\n")
}
}
不应该只 notificationInfoItem.desiredKeys = ["idEvento"] 就足以将数据传递给我吗?非常感谢任何帮助!
您已经设置了 CKNotificationInfo
权限,您只需要在 didRecieveRemoteNotificationHandler
:
中像这样访问它
if let queryNotification = notification as? CKQueryNotification{
let id = query.recordFields?["idEvento"] as? String
}
或者 idEvento
恰好是什么类型。请记住,所需键唯一支持的属性类型是字符串、数字、位置、日期和引用。
我正试图通过 CloudKit 的订阅在我的通知中传递一个字符串。但是,当我在我的 iPhone 中打印这个字符串时,它是零!这是我的代码:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
let cloudKitNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
print(cloudKitNotification)
if cloudKitNotification.notificationType == CKNotificationType.Query {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("performReload", object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("performReloadItem", object: nil)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("EventoViewController") as! EventoViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationViewController, animated: false)
})
}
}
这是我设置订阅时的样子:
let predicateItem = NSPredicate(format: "self contains '\(self.eventoID)'")
let subscriptionItem = CKSubscription(recordType: "Itens",
predicate: predicateItem,
options: [.FiresOnRecordCreation, .FiresOnRecordUpdate])
let notificationInfoItem = CKNotificationInfo()
notificationInfoItem.shouldSendContentAvailable = true
notificationInfoItem.desiredKeys = ["idEvento"]
notificationInfoItem.shouldBadge = false
subscriptionItem.notificationInfo = notificationInfo
let publicdbItem = CKContainer.defaultContainer().publicCloudDatabase
publicdbItem.saveSubscription(subscriptionItem) { (returnRecord, error) in
if error != nil {
print(error?.localizedDescription)
} else {
print("ITEM subscription ok!\n")
}
}
不应该只 notificationInfoItem.desiredKeys = ["idEvento"] 就足以将数据传递给我吗?非常感谢任何帮助!
您已经设置了 CKNotificationInfo
权限,您只需要在 didRecieveRemoteNotificationHandler
:
if let queryNotification = notification as? CKQueryNotification{
let id = query.recordFields?["idEvento"] as? String
}
或者 idEvento
恰好是什么类型。请记住,所需键唯一支持的属性类型是字符串、数字、位置、日期和引用。