58

In this application that I'm trying to make, I use push notifications. This part works just fine. When I send a notification I also add a badge to the app icon. The problem is when I lunch the application it should disappear again, but it does not.

-(IBAction)Push{

    NSMutableDictionary *data = [NSMutableDictionary dictionary];

    [data setObject:@"Numfeud: Troels made a move!" forKey:@"alert"];

    [data setObject:[NSNumber numberWithInt:1] forKey:@"badge"];

    [data setObject:@"bar" forKey:@"foo"];

    [PFPush sendPushDataToChannelInBackground:@"GameChannel2" withData:data];
}

In the application didFinishLaunchingWithOptions I try to set badge to 0 in this way:

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

How can I clear the application icon badge?

Brian
  • 14,610
  • 7
  • 35
  • 43
thar
  • 1,094
  • 2
  • 13
  • 25

5 Answers5

215

If your app becomes active again and is still in the background you should reset the badge count in -applicationDidBecomeActive: as well:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}

If your app is still running in the background -application:didFinishLaunchingWithOptions: won't be called.

Mark
  • 2,714
  • 1
  • 14
  • 17
  • Thanks made! Then there is something there make sense :) Works just find, I'm only reset it in the BecomeActive method. Can't se why I should have it twice? – thar Mar 04 '12 at 18:05
  • 7
    Resetting once in the ``-applicationDidBecomeActive:`` will do just fine. Because this method will also be called when ``-application:didFinishLaunchingWithOptions:`` is called. Please accept my answer :) – Mark Mar 04 '12 at 19:45
  • Isn't it better to do it on applicationWillResignActive, right before the user is going to see it? – Phuah Yee Keat Mar 04 '19 at 16:52
8

Likely, -application:didFinishLaunchingWithOptions: is not being called, because your app is still running in the background. In order to remove the badge count when the app is launched from the background you'll have to reset the badge number in -applicationWillEnterForeground:, too.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • Thanks Fabian! Your answer was just what i needed :) thannnkkss – thar Mar 04 '12 at 18:06
  • Are -applicationDidBecomeActive and -applicationWillEnterForeground: doing the same less or more? When placing the reset in -applicationDidBecomeActive. It work i both situations. – thar Mar 04 '12 at 18:23
  • -applicaionDidBecomeActive: will be invoked when returning from a phone call, too. -applicationWillEnterForeground: is the appropriate method in your situation. – Fabian Kreiser Mar 05 '12 at 15:18
  • applicationWillEnterForeground will not be called after application:didFinishLaunchingWithOptions:, though. – aslı May 19 '14 at 11:37
  • Yes, that's why it has to be reset twice. Using `-applicationDidBecomeActive:` and only resetting it once is okay, too. – Fabian Kreiser May 19 '14 at 19:09
5

In Swift and In AppDelegate

func applicationDidBecomeActive(_ application: UIApplication) {
    application.applicationIconBadgeNumber = 0
}
Sourabh Kumbhar
  • 1,034
  • 14
  • 12
2

You can use this codes also.

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    application.applicationIconBadgeNumber = 0;
}

or In a specific ViewController

- (void)awakeFromNib {
   [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
handiansom
  • 783
  • 11
  • 27
1

Maybe call it in applicationWillResignActive (in AppDelegate.m):

-(void)applicationWillResignActive:(UIApplication *)application{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}

This will help you to clear badge if push come when app is being open. User seeing notification and you clear it, when he press Home Button (once or twice). Also it will be clear if app being closed (clear after user open it).

Here you can see when this method called.

S. Matsepura
  • 1,673
  • 1
  • 17
  • 24