Я создаю свои два CKSubscriptions
в методе didFinishLaunchingWithOptions
моего делегата приложения как таковом.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
_myContainer = [CKContainer containerWithIdentifier:@"iCloud.com.isaranjha.Copyfeed"];
_privateDatabase = [_myContainer privateCloudDatabase];
[_privateDatabase fetchSubscriptionWithID:@"subscription" completionHandler:^(CKSubscription *subscription, NSError *error){
if (subscription) {
} else {
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
CKSubscription *subscription = [[CKSubscription alloc] initWithRecordType:@"Strings" predicate:predicate subscriptionID:@"subscription" options:CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordDeletion | CKSubscriptionOptionsFiresOnRecordUpdate];
CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
notificationInfo.alertBody = @"";
notificationInfo.shouldSendContentAvailable = YES;
subscription.notificationInfo = notificationInfo;
[_privateDatabase saveSubscription:subscription completionHandler:^(CKSubscription *subscription, NSError *error) {
}];
}
}];
[_privateDatabase fetchSubscriptionWithID:@"subscription1" completionHandler:^(CKSubscription *subscription, NSError *error){
if (subscription) {
} else {
NSPredicate *predicate1 = [NSPredicate predicateWithValue:YES];
CKSubscription *subscription1 = [[CKSubscription alloc] initWithRecordType:@"Images" predicate:predicate1 subscriptionID:@"subscription1" options:CKSubscriptionOptionsFiresOnRecordCreation | CKSubscriptionOptionsFiresOnRecordDeletion | CKSubscriptionOptionsFiresOnRecordUpdate];
CKNotificationInfo *notificationInfo1 = [CKNotificationInfo new];
notificationInfo1.shouldSendContentAvailable = YES;
notificationInfo1.alertBody = @"";
subscription1.notificationInfo = notificationInfo1;
[_privateDatabase saveSubscription:subscription1 completionHandler:^(CKSubscription *subscription, NSError *error) {
}];
}
}];
ViewController *view = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = navController;
return YES;
}
Они создаются успешно, так как когда я выхожу из системы NSError
, он возвращает ноль, и каждый раз, когда я открываю приложение после этого, оно может правильно их получить. Однако, когда запись создается или удаляется на одном устройстве, скажем, на iPhone, уведомление не срабатывает (или не принимается должным образом) на другом устройстве, скажем, на Mac. Итак, вот как я слушаю уведомления на своем Mac.
- (void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"CKSubscription received.");
CKQueryNotification *cloudKitNotification = [CKQueryNotification notificationFromRemoteNotificationDictionary:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CloudKitUpdated" object:nil userInfo:@{@"ckNotification" : cloudKitNotification}];
}
Этот NSLog
, к сожалению, никогда не срабатывает.