Kateri.Ze
Kateri.Ze

Reputation: 21

How to change badge count?

I’m using CKSubscription to send push notifications, I send them well and the device receives everything correctly, but my badge count is always +1 to what it was before? How can I set it to 0 after the app was run?

I have added this code to func application didFinishLaunchingWithOptions

UIApplication.shared.applicationIconBadgeNumber = 0

But when you run the app, the badge disappears, receiving the next push notification the number rises again.

Upvotes: 1

Views: 802

Answers (2)

Kateri.Ze
Kateri.Ze

Reputation: 21

Solved it with CKModifyBadgeOperation putting it in applicationDidBecomeActive

let badgeOp = CKModifyBadgeOperation(badgeValue: 0)
badgeOp.modifyBadgeCompletionBlock = { (error) -> Void in 
if error != nil { print (“error with the badge”)
} 
else {
UIApplication.shared.applicationIconBadgeNumber = 0 }
}
CKContainer.default().add(badgeOp)
}

all works!!!! So happy!!!!

Upvotes: 1

Terje
Terje

Reputation: 1000

It sounds like you're just not zeroing it out at the right place?

didFinishLaunchingWithOptions is not called when your app resumes from background, only when it is actually launched. The most common use of the badge I can think of is when you are receiving notifications and you want to zero them when the user has seen the notifications. So that's where you have to set your applicationIconBadgeNumber = 0

If for instance your app is just a single view with a list of messages and just seeing that list is enough to tell you the user has read the message you could use applicationWillEnterForeground instead. That is called every time you come back from being in the background.

You still also have to zero it out when a notification comes in while the app is in the foreground though.

Upvotes: 0

Related Questions