ios - 记录中特定字段的 cloudkit 通知

标签 ios apple-push-notifications nspredicate cloudkit cknotification

我有 cloudkit 通知功能。当有人更改记录时,订阅者会收到通知。我的订阅定义如下:

         NSPredicate *searchConditions = [NSPredicate predicateWithFormat:@"%K = %@", CLOUDKIT_PUBLIC_ID_GUID, theCloudGUID];
         int subscriptionOptions = CKSubscriptionOptionsFiresOnRecordUpdate | CKSubscriptionOptionsFiresOnRecordDeletion;
         CKSubscription *publicSubscription = [[CKSubscription alloc] initWithRecordType:CLOUDKIT_RECORDNAME_PUBLIC_ID
                                                                               predicate:searchConditions
                                                                                 options:subscriptionOptions];

         CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
         notificationInfo.alertLocalizationKey = CLOUDKIT_NOTIFICATION_PUBLIC;
         notificationInfo.shouldBadge = NO;
         notificationInfo.alertBody = CLOUDKIT_NOTIFICATIONBODY_PUBLIC;
         publicSubscription.notificationInfo = notificationInfo;

         [publicDatabase saveSubscription:publicSubscription completionHandler:^(CKSubscription * _Nullable subscription, NSError * _Nullable error)
          {
              //error handling
          }

问题是,这条记录中有多个字段。我只想在一个特定字段发生变化时提醒订阅者。

创建订阅时,有没有办法设置搜索谓词来检测特定字段的变化?我通读了各种 Predicate 文档,但没有看到具体提到这一点。

或者,在收到通知时,有没有办法查看哪些字段发生了变化?在 didReceiveRemoteNotification 我试过:

 CKQueryNotification *queryNotification = [CKQueryNotification notificationFromRemoteNotificationDictionary:userInfo]; 

但是 queryNotification.recordFields 为空。

作为最坏的情况,我考虑过将特定字段分解成它自己的记录,但是我有维护更多记录的开销,这些记录由一个公共(public) GUID 绑定(bind)在一起。我希望保持这个更紧凑。

最佳答案

你的问题有点老了,所以也许你已经想通了,但使用 desiredKeys 属性可能会有所帮助:https://developer.apple.com/documentation/cloudkit/cknotificationinfo/1514931-desiredkeys

如果这些通知是静默推送,您可以查看有效负载以查看某些键是否更改并让您的应用做出相应的 react 。

如果这些是推送(对用户可见)通知,我认为您不能根据 key 更改本身进行推送。如果您要测试如何值更改(是否等于this),您可以在CKQuerySubscription 上设置NSPredicate或不等于 that 等),但我不确定它是否会因任何更改而被触发。

关于ios - 记录中特定字段的 cloudkit 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452641/

相关文章:

ios - 运行 expo build :ios, 时获取无效凭据,即使我可以手动登录

ios - UISwipeGestureRecognizer 状态总是结束

iphone - "Push notification"- 反馈,卸载应用

ios - 具有多个条件的 UITableView filteredArrayUsingPredicate?

ios - NSPredicate predicateWithFormat : still counting previously deleted data

ios - 是否可以使用高阶函数合并数组中的重复对象?

iOS swift : SearchController with a StoryBoard UISearchBar

ios - 即使应用程序被终止,如何继续进行 iOS 位置跟踪?

ios - Apple 推送通知 (APN) - 何时使用沙箱和生产?

swift - 搜索 Controller - 将 NSPredicate 与模型一起使用时出现问题