Cloudkit: CKNotificationInfo badge value never decrease

877 views Asked by At

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 )

2

There are 2 answers

4
Edwin Vermeer On

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)
}
0
Vincent Gigandet On

This will help.

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