4

我为 cloudkit 设置订阅通知。这是我的代码:

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);
                       }

                       }];

并且工作得很好。问题是徽章,它们似乎 cloudKit 没有重置徽章编号,即使我将徽章计数设置为零,它也会不断增加。

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

当应用收到新通知时,从 0 变为 5(并且每个新通知增加 1,下一次将是 6)

你们中的任何人都知道如何从 cloudkit 跟踪正确的徽章数量(在 Objective-C 中)

4

2 回答 2

9

这是CloudKit 的副本不会将我的徽章计数重置为 0

答案是:您需要在处理完通知后执行 CKModifyBadgeOperation。

这是我在将所有通知标记为已读后调用的 Swift 函数。我将操作添加到 defaultContainer 而不是仅仅启动它——我想知道这有什么不同。

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)
}
于 2014-12-02T09:16:27.643 回答
1

这会有所帮助。

CKModifyBadgeOperation *badgeResetOperation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:0];
[badgeResetOperation setModifyBadgeCompletionBlock:^(NSError * operationError) {
    if (!operationError) {
        [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    }
}];
[[CKContainer defaultContainer] addOperation:badgeResetOperation];
于 2016-07-28T10:06:15.060 回答