41

What is the code to remove the badge on my app's icon? When I receive push, I need to remove it when a button is clicked!

obliviux
  • 413
  • 1
  • 4
  • 4

4 Answers4

97

objC :

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

swift :

UIApplication.sharedApplication().applicationIconBadgeNumber = 0;
Alberto Scampini
  • 798
  • 9
  • 27
Felixyz
  • 19,053
  • 14
  • 65
  • 60
  • 2
    If your using an API like Parse.com (I was), you will likely also have to update the remote database to "0". Otherwise setting it locally will hides the badge but upon the next push the badge will not show 0 but instead show the old value +1. Like this guys saw http://stackoverflow.com/questions/13757285/clear-push-notification-badge-after-increment – Wesley Smith Apr 14 '14 at 09:51
10

You can remove badge from push notifications by adding the following lines to your code

(void)applicationDidBecomeActive:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
julienc
  • 19,087
  • 17
  • 82
  • 82
Ravindra Naik
  • 351
  • 3
  • 7
7

As for iOS5, just setting badge number won't remove those push notification in the notification center. You have to do this...

[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

I already tested this. It looks like cancelAllLocalNotifications method also works with push notifications in notification center as well.

Hlung
  • 13,850
  • 6
  • 71
  • 90
  • Beware with: cancelAllLocalNotifications. The question was to remove badges from PUSH notifications but this one will remove all local notifications (is applicaiton had scheduled any). – Lukasz Jan 09 '13 at 09:03
  • Yes, I'm aware of that. But according to http://stackoverflow.com/questions/9925854/remove-single-remote-notification-from-notification-center , you cannot selectively remove one particular notification. You can only remove all. You should comment in Felixyz's answer too because using `applicationIconBadgeNumber=0` will also removes ALL push+local notifications, right? – Hlung Jan 10 '13 at 08:21
1

Swift 3

UIApplication.shared.applicationIconBadgeNumber = 0

Can be added to following methods:

optional public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool

and

optional public func applicationDidBecomeActive(_ application: UIApplication)
Maverick
  • 3,209
  • 1
  • 34
  • 40