1

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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
Kateri.Ze
  • 21
  • 4

2 Answers2

1

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!!!!

Kateri.Ze
  • 21
  • 4
0

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.

Terje
  • 980
  • 9
  • 15
  • Thank you, for your answer! Actually I have tried it, I even tried to put this piece of code in func application (... didReceiveRemoteNotification...) I thought that the problem might be that I don’t update badge count in CloudKit dashboard in my subscription type, thus it increases, but I have no idea how to do that... – Kateri.Ze Nov 18 '17 at 16:27