简体   繁体   English

如何重设徽章应用程序图标编号?

[英]How to reset the Badge app icon number?

I integrated the Push Notification to the CloudKit so that every change in the iCloud will pop up a notification on my iPhone and the badge app icon number will add one correspondingly. 我将推送通知集成到CloudKit,以便iCloud中的每一次更改都会在我的iPhone上弹出一个通知,并且徽章应用程序图标编号将相应地添加一个。 However, when I used the code: 但是,当我使用代码时:

application.applicationIconBadgeNumber = 0

to reset that number in the applicationDidBecomeActive(_ application: UIApplication) , I noticed that the badge app icon number truly disappeared but if another new notification came again, the number won't start from one again as supposed but just add one to the original total number before the reset. 要在applicationDidBecomeActive(_ application: UIApplication)重置该数字,我注意到徽章应用程序图标的数字确实消失了,但是如果再次收到另一个新通知,该数字将不会像预期的那样从另一个开始,而只是在原始数字上添加一个重置前的总数。 Therefore the number is getting bigger and bigger. 因此,这个数字越来越大。 I wonder how to solve this problem? 我想知道如何解决这个问题?

The problem is your apns payload , it contains badge count more than 1 , you need to reset the payload as well. 问题是您的apns有效负载 ,其中包含的徽章计数大于1 ,您还需要重置有效负载。

When you set application.applicationIconBadgeNumber = 0 it just resets the badge count locally, not in the server. 设置application.applicationIconBadgeNumber = 0它只是在本地(而不是在服务器中)重置徽章计数。

Solution would be reset the badge count for the user in the server too. 解决方案也将为服务器中的用户重置徽章计数。

Update: Apns Payload 更新:Apns有效负载

{
    "aps" : {
        "alert" : {
            "title" : "Push",
            "body" : "Hello User"
        },
        "badge" : 5
    }

}

The app shows the badge count same as in the apns payload above, you need to reset the badge value in the payload above from server. 该应用程序显示的徽章计数与上面的apns有效负载中的徽章数量相同,您需要从服务器重置以上有效负载中的徽章值。

Hope it helps. 希望能帮助到你。

Cheers. 干杯。

I find that I should not only set the application side like: 我发现我不仅应该将应用程序侧设置为:

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

but I should also set the iCloud side in CKContainer. 但我也应该在CKContainer中设置iCloud端。 Therefore, the complete code is like below: 因此,完整的代码如下所示:

let operation = CKModifyBadgeOperation(badgeValue: 0)
operation.modifyBadgeCompletionBlock = {(error) in
    if let error = error{
        print("\(error)")
        return
    }
    application.applicationIconBadgeNumber = 0
}
CKContainer.default().add(operation)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM