如何使用CKFetchNotificationChangesOperation
处理并将所有错过的通知从订阅的CKSubscription
发送到我的Mac应用中的- (void)application:(nonnull NSApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
?我对该方法的代码如下,
- (void)application:(nonnull NSApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
NSLog(@"CKSubscription received.");
CKQueryNotification *cloudKitNotification = [CKQueryNotification notificationFromRemoteNotificationDictionary:userInfo];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CloudKitUpdated" object:nil userInfo:@{@"ckNotification" : cloudKitNotification}];
}
我的应用程序是一个menulet,我希望它能够检查是否有任何错过的通知,并在单击菜单时正确处理它们。
更新:这是我一直在尝试的代码但是数组总是空的并且没有错误。我正在通过运行应用程序,关闭应用程序,删除记录,然后再次运行应用程序进行测试。
NSMutableArray *array = [NSMutableArray array];
CKFetchNotificationChangesOperation *operation = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:nil];
operation.notificationChangedBlock = ^(CKNotification *notification) {
[array addObject:notification.notificationID];
};
operation.completionBlock = ^{
};
operation.fetchNotificationChangesCompletionBlock = ^(CKServerChangeToken *token, NSError *error) {
NSLog(@"Missed notifications: %@", array);
};
[_myContainer addOperation:operation];
答案 0 :(得分:1)
NSMutableArray *array = [NSMutableArray array];
CKFetchNotificationChangesOperation *operation = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:nil];
operation.notificationChangedBlock = ^(CKNotification *notification) {
[array addObject:notification.notificationID];
};
operation.completionBlock = ^{
};
operation.fetchNotificationChangesCompletionBlock = ^(CKServerChangeToken *token, NSError *error) {
NSLog(@"Missed notifications: %@", array);
};
[_myContainer addOperation:operation];