问题
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. 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. Therefore the number is getting bigger and bigger. I wonder how to solve this problem?
回答1:
The problem is your apns payload, it contains badge count more than 1, you need to reset the payload as well.
When you set application.applicationIconBadgeNumber = 0
it just resets the badge count locally, not in the server.
Solution would be reset the badge count for the user in the server too.
Update: Apns Payload
{
"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.
Hope it helps.
Cheers.
回答2:
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. 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)
来源:https://stackoverflow.com/questions/42720259/how-to-reset-the-badge-app-icon-number