【问题标题】:How to pass data using CloudKit's notification?如何使用 CloudKit 的通知传递数据?
【发布时间】:2016-06-23 20:55:13
【问题描述】:

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

【问题讨论】:

    标签: swift notifications cloudkit subscription


    【解决方案1】:

    你已经设置好了CKNotificationInfo,你只需要像这样在你的didRecieveRemoteNotificationHandler中访问它:

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

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

    【讨论】:

    • 非常感谢您的评论!不知道那个!!但是只有一个问题,我现在如何处理 id 变量?我认为我想要的所有 recordFields 都会出现在 userInfo 中......我像普通的 segue 一样将它传递给目标 VC?再次感谢您的关注
    • 是的,您现在可以根据需要传递 id 变量,我认为您可以从 userInfo 字典中获取字段,但通常将其转换为 CKNoticication 对象更容易。如果 idEvento 不是字符串,您可以将其转换为答案中提到的任何其他类型
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2020-06-20
    • 2017-10-12
    • 2015-11-30
    • 1970-01-01
    • 2022-10-22
    相关资源
    最近更新 更多