How to use CKFetchNotificationChangesOperation?

爱⌒轻易说出口 提交于 2019-12-23 23:33:35

问题


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];

回答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];


来源:https://stackoverflow.com/questions/30993115/how-to-use-ckfetchnotificationchangesoperation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!