Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudKit won't reset my badge count to 0

I've tried a number of things and can't seem to reset the badge count from notifications comings from cloudKit. Has anyone else ran into this problem. Here is what I've tried:

1) Set the badge count locally to 0

  application.applicationIconBadgeNumber = 0; (temporarily removes the badge count).

No luck...

2) Call the server to clear the badge count

 CKModifyBadgeOperation *oper = [[CKModifyBadgeOperation alloc] initWithBadgeValue:0];
  [oper start];

No luck...

3) Pull in all notification changes and mark them all read

NSMutableArray *array = [NSMutableArray array];
CKFetchNotificationChangesOperation *operation = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:nil];
operation.notificationChangedBlock = ^(CKNotification *notification) {
    [array addObject:notification.notificationID];
};
operation.completionBlock = ^{
        CKMarkNotificationsReadOperation *op = [[CKMarkNotificationsReadOperation alloc] initWithNotificationIDsToMarkRead:array];
        [op start];
};
[operation start];

And again no luck...

Any suggestions would be greatly appreciated! Thanks, Chris

like image 291
Chris Gokey Avatar asked Aug 04 '14 13:08

Chris Gokey


1 Answers

You need to do a CKModifyBadgeOperation after processing your notifications.

Here is my Swift function which I call after marking all the notifications as read. I add the operation to the defaultContainer instead of just starting it - I wonder does that make any difference.

func resetBadgeCounter() {
    let badgeResetOperation = CKModifyBadgeOperation(badgeValue: 0)
    badgeResetOperation.modifyBadgeCompletionBlock = { (error) -> Void in
        if error != nil {
            println("Error resetting badge: \(error)")
        }
        else {
            UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        }
    }
    CKContainer.defaultContainer().addOperation(badgeResetOperation)
}
like image 52
SarahR Avatar answered Sep 20 '22 13:09

SarahR