Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudkit: CKNotificationInfo badge value never decrease

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 )

like image 963
user2924482 Avatar asked Dec 26 '22 01:12

user2924482


2 Answers

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)
}
like image 124
Edwin Vermeer Avatar answered Feb 18 '23 06:02

Edwin Vermeer


This will help.

CKModifyBadgeOperation *badgeResetOperation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:0];
[badgeResetOperation setModifyBadgeCompletionBlock:^(NSError * operationError) {
    if (!operationError) {
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
}];
[[CKContainer defaultContainer] addOperation:badgeResetOperation];
like image 36
Vincent Gigandet Avatar answered Feb 18 '23 07:02

Vincent Gigandet