首页 > 解决方案 > 如何为 CloudKit 通知检索所需的密钥?

问题描述

我已经将 CloudKit 设置为在名为“Notifications”的表中存储和发送更改通知,效果很好。但是,我正在根据我简单命名为“key”的字段的内容处理订阅(图像订阅不同的线程),效果也很好。

现在我想根据属性“desiredKeys”在有效负载中的“密钥”字段中包含内容,但它不包含在 UNNotificationCenter willPresent 方法的常规有效负载中(仅存在常规警报、徽章和声音键,不是“关键”领域。

我已经设置了我的 CKNotification.Info 如下:

let querySubscription = CKQuerySubscription(recordType: "Notification", predicate: NSPredicate(format: "key == %@", key), options: .firesOnRecordCreation)
        let info = CKSubscription.NotificationInfo()
        info.titleLocalizationKey = "%1$@"
        info.titleLocalizationArgs = ["title"]
        info.subtitleLocalizationKey = "%1$@"
        info.subtitleLocalizationArgs = ["subtitle"]
        info.alertLocalizationKey = "%1$@"
        info.alertLocalizationArgs = ["body"]
        info.desiredKeys = ["key"]
        info.shouldBadge = true
        info.shouldSendContentAvailable = true
        info.soundName = "default"
        querySubscription.notificationInfo = info

标题、副标题和正文正在工作,但不需要的键不存在。

不过我有点困惑,因为我读过我应该从 AppDelegate 的 didReceiveRemoteNotification 委托方法中收到的字典中创建一个 CKQueryNotification,但该方法从未被调用,只有 UNNotificationCenter 中的那个。

我错过了什么?谢谢你的帮助!

标签: iosswiftnotificationspushcloudkit

解决方案


let userInfo = notification.request.content.userInfo as! [String: Any]
    var notificationKey = ""
    if let ck = userInfo["ck"] as? [String: Any] {
        if let qry = ck["qry"] as? [String: Any] {
            if let af = qry["rid"] as? String {
                    notificationKey = af
            }
        }
    }
    print("notification key = \(notificationKey)")

我的工作就像你描述的那样,只是“ck”路径有点不同,所以我需要先打印userInfo以获得正确的路径


推荐阅读