4

I set subscription notifications for cloudkit. Here is my code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"TRUEPREDICATE"];
    CKSubscription *subscription = [[CKSubscription alloc]
                                    initWithRecordType:recordType
                                    predicate:predicate
                                    options:CKSubscriptionOptionsFiresOnRecordCreation];
    CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
    notificationInfo.alertLocalizationKey =@"New record in cloudKit";
    notificationInfo.shouldBadge = YES;
    notificationInfo.soundName = UILocalNotificationDefaultSoundName;
    notificationInfo.shouldSendContentAvailable = YES;
    subscription.notificationInfo = notificationInfo;
    CKContainer *container = [CKContainer defaultContainer];
    CKDatabase *publicDatabase = [container publicCloudDatabase];
    [publicDatabase saveSubscription:subscription
                   completionHandler:^(CKSubscription *subscription, NSError *error) {
                       if (!error)
                       {
                           NSLog(@"no error");
                       }
                       else
                       {
                           NSLog(@"error%@", error);
                       }

                       }];

and works just fine. The problem is the badges, they seem like cloudKit doesn't reset the badge number and the keep increasing even when I set the badge count to zero.

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}

When the app received a new notification goes from 0 to 5 (and every new notification increase by 1, the next time would be 6)

Any of you knows how keep track of the right count of badges from cloudkit (in Objective-C )

user2924482
  • 8,380
  • 23
  • 89
  • 173
  • 1
    possible duplicate of [CloudKit won't reset my badge count to 0](http://stackoverflow.com/questions/25120070/cloudkit-wont-reset-my-badge-count-to-0) – Edwin Vermeer Dec 02 '14 at 09:17

2 Answers2

9

This is a duplicate of CloudKit won't reset my badge count to 0

The answer there was: 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)
}
Community
  • 1
  • 1
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
1

This will help.

CKModifyBadgeOperation *badgeResetOperation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:0];
[badgeResetOperation setModifyBadgeCompletionBlock:^(NSError * operationError) {
    if (!operationError) {
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
}];
[[CKContainer defaultContainer] addOperation:badgeResetOperation];
Vincent Gigandet
  • 918
  • 10
  • 21