toast toast - 17 days ago 7
Swift Question

How to remove Appicon Badge, ios10, swift

I can add a badge to the AppIcon, I just cannot remove it.

I have a notification helper class, with a refresh badge number function, as shown below:

import UserNotifications

class NotificationHelper {

...

//fetch all notifications, and update the tabBarController
func refreshTabBarBadge(tabBarController: UITabBarController) {
... some Core Data related stuff to set badge

let content = UNMutableNotificationContent()
if badge > 0 {
content.badge = badge as NSNumber
}
else {
content.badge = 0
content.badge = nil
}
print("set app icon badge to: \(badge)")
}


When I print the output, I see:
set app icon badge to: 1


This sets a badge on the AppIcon.

Then when I trigger an event that causes the
badge
variable to decrease I see:
set app icon badge to: 0


Yet, when I background the app and check the icon, the badge remains, set to 1.

Its as if
content.badge
is not working at all. In the Apple documentation it says if the badge number is 0, it will remove the badge, but this is not the case. As you can see, I also try setting it to
nil
afterwards.

How do I remove the badge from the AppIcon?

Answer

set badge number = 0. like UIApplication.shared.applicationIconBadgeNumber = 0