ios - 如何为 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 中的那个。
我错过了什么?谢谢你的帮助!
解决方案
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以获得正确的路径
推荐阅读
- ios - AVAudioSession.sharedInstance().outputVolume 不始终返回正确的音量
- google-cloud-platform - 如何使 Ansible Dynamic Inventory 与 Google Cloud Platform (Google Compute Engine)、GCP 一起使用
- python - 设置远程python解释器时删除默认参数
- magento-1.9 - 我可以在创建时明确设置 Magento 1.9 订单增量 ID
- gradle - org.gradle.api.UncheckedIOException:无法为文件创建 MD5 哈希
- node.js - 为什么 node.js (Express) 不为这条路线提供服务?我该如何解决?
- javascript - 从 2 个或更多 spritesheets 创建动画 - Phaser
- sql - 如何使用条件搜索历史表并在同一个表中更新?
- html - VBA IE与大表单交互
- sql - laravel 使用控制器向数据库添加和删除列