如何使用CloudKit的通知传递数据?

时间:2016-06-23 20:55:14

标签: swift notifications cloudkit subscription

我试图通过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"] 足以传递给我这些数据?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

您已设置CKNotificationInfo权限,只需在didRecieveRemoteNotificationHandler中访问它{/ p>

if let queryNotification = notification as? CKQueryNotification{
    let id = query.recordFields?["idEvento"] as? String
}

或者idEvento恰好是什么类型。请记住,所需键的唯一受支持的属性类型是字符串,数字,位置,日期和引用。