我正在编写一个使用 CloudKit 的小应用程序。由于某种原因,当有与查询匹配的新记录时,应用程序不会收到任何通知。有没有人能够让这个功能工作?
我在应用程序中创建新记录,也在 CloudKit 仪表板中创建新记录。记录非常简单,只有一个整数字段。
创建记录:
CKRecord *record = [[CKRecord alloc] initWithRecordType:kSISCloudKitRecordTypeTest];
record[@"value"] = @1;
[self.publicDatabase saveRecord:record completionHandler:^(CKRecord *record, NSError *error)
{
// this call succeeds, no error.
}];
注册通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application registerForRemoteNotifications];
}
创建订阅:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"value = 1"];
CKSubscription *subscription = [[CKSubscription alloc]
initWithRecordType:kSISCloudKitRecordTypeTest
predicate:predicate
options:CKSubscriptionOptionsFiresOnRecordCreation];
CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertLocalizationKey = @"LOCAL_NOTIFICATION_KEY";
notificationInfo.soundName = @"Party.aiff";
notificationInfo.shouldBadge = YES;
subscription.notificationInfo = notificationInfo;
[self.publicDatabase saveSubscription:subscription
completionHandler:^(CKSubscription *subscription, NSError *error)
{
// this succeeds as well, at least the 1st time I run it.
// on subsequent calls it returns an error "duplicate subscription", which is OK by me.
}
运行上述代码并在仪表板中创建新记录后,我希望调用此应用程序委托方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
CKNotification *cloudKitNotification = [CKNotification notificationFromRemoteNotificationDictionary:userInfo];
NSLog(@"cloudKitNotification: %@", cloudKitNotification);
}
但是,它永远不会被调用。