moonvader moonvader - 1 year ago 91
Swift Question

Reset count on icon badge

I send pushes using When i send pushes i set increment badge to "yes" so users can see badges with "1" on my app icon.

When users opens app's main ViewController, app clears badge count using this code

UIApplication.sharedApplication().applicationIconBadgeNumber = 0

This works fine and badge is clear. But when i send new push with badge increment enabled - i see that number on badge is 2. Is something wrong with my way to reset badge count?

Answer Source

That's because while you remove the local badge, the badge count in Parse Installation class for that device remains the same. You can do the following to remove that:

Assuming that you have already made sure that current user has a PFInstallation (i.e. he did not decline the push notification access request) , To reset the badge number on backend, you can use the following:

var currentInstallation = PFInstallation.currentInstallation()
      if currentInstallation.badge != 0 {
        currentInstallation.badge = 0

This makes sure to set the badge to 0 only if it is currently showing a non-zero counter.

Setting badge on currentInstallation will automatically set applicationIconBadgeNumber too. By doing this, Parse will know what number your app is currently displaying and they can increment the counter correctly whenever an Increment is sent to this device again. For more information you can read Official Parse Blog Announcement on this matter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download