How to use CKFetchNotificationChangesOperation?

582 views Asked by At

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

There are 1 answers

1
AudioBubble On BEST ANSWER
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];