如何使用CKFetchNotificationChangesOperation? [英] How to use CKFetchNotificationChangesOperation?
问题描述
如何使用 CKFetchNotificationChangesOperation
处理和将所有丢失的通知从已订阅的 CKSubscription
定向到-(void)application:(nonnull NSApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
在我的Mac应用程序中?我对该方法的代码如下,
How do I use CKFetchNotificationChangesOperation
to handle and direct all missed notifications from a subscribed CKSubscription
to the - (void)application:(nonnull NSApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
in my Mac application? The code I have for that method is as follows,
- (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}];
}
我的应用是菜单,我希望它检查是否有任何遗漏的通知,
My app is a menulet and I want it to check for any missed notifications and handle them properly when the menulet is clicked on.
更新:这是我一直在尝试的代码,但是数组始终为空,没有错误。我正在通过运行应用程序,关闭应用程序,删除记录然后再次运行应用程序进行测试。
UPDATE: This is the code I have been trying but the array is always empty and there is no error. I am testing by running the app, closing the app, deleting a record, and then running the app again.
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];
推荐答案
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];
这篇关于如何使用CKFetchNotificationChangesOperation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!