How to use CKFetchNotificationChangesOperation?

2019-07-13 23:45发布

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条回答
小情绪 Triste *
2楼-- · 2019-07-14 00:18
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];
查看更多
登录 后发表回答