简体   繁体   中英

Reset count on icon badge

I send pushes using Parse.com. 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?

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 (ie 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
        currentInstallation.save
       }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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