CloudKit will not reset my icon will be 0

I tried several things and can not imagine reset the number of icons from the notifications received from cloudKit. Has anyone else encountered this problem. Here is what I tried:

1) Set the icon counter locally to 0

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

Bad luck...

2) Call the server to clear the icon counter

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

Bad luck...

3) Pull for all changes to notifications 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

+7
ios8 push-notification apple-push-notifications cloudkit cksubscription
source share
2 answers

You need to do CKModifyBadgeOperation after processing your notifications.

Here is my Swift function, which I call after marking all notifications as read. I am adding an operation to defaultContainer instead of just starting it. Interestingly, that matters.

 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) } 
+13
source share

Errors and alert operations fail if you do not install a container on it. Since you are not using termination blocks, you are not seeing an error (you are using the default NSOperation termination block, which is incorrect and has no error parameter). Look at the headers for these operations to see the syntax of the completion block.

And the best way is to add the operation to the container using the addOperation method, this essentially sets the container in the operation before starting it. It also runs in an internal queue, which has the added benefit of preventing multiple operations from running simultaneously, which can lead to conflicts.

0
source share

All Articles