1

I'm trying to set the badge in my tab bar to "1" on the 4th tab when a notification is received by my app. Strangely enough, my console says that the method is firing (see NSLog "Fire!"), however the badge doesn't appear on my tab bar item once the notification is received? Am I missing something?

AppDelegate.m

-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo 

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        NSLog(@"App notification received!");
        // do stuff when app is active

    }else{
        // do stuff when app is in background

        static int i=1;
        [UIApplication sharedApplication].applicationIconBadgeNumber = i++;

        NSLog(@"App notification received background!");
    }
}

ViewController.m

- (void) myNotificationReceived:(NSNotification *) notification {


    [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"1"];


    NSLog(@"Fire!");
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationReceived:) name:@"pushNotification" object:nil];

}
Brittany
  • 1,359
  • 4
  • 24
  • 63
  • Few suggestions: What happens when you call `[[self.tabBarController.tabBar.items objectAtIndex:4] setBadgeValue:@"1"]` from `viewDidLoad`? Is this line called from main thread? Do you have a More... tab perhaps? – meaning-matters Jul 28 '17 at 17:56
  • _"contains >= 5 items"_ `objectAtIdex:` throws an exception for out-of-bounds indexes. – jscs Jul 28 '17 at 17:58
  • try this [[self.tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:@"1"]; does the 1 appear on hardcoded index? – Mohamad Bachir Sidani Jul 28 '17 at 18:00
  • 1
    Note that you're trying to set the 5th tab, not the 4th. – meaning-matters Jul 28 '17 at 18:02
  • @BashirSidani Tried this, still no "1" appearing. Though, my NSLog "Fire!" still shows in the console, leading me to believe that the method is executed. – Brittany Jul 28 '17 at 18:33
  • @meaning-matters tried the code with objectAtIndex 3 as well, still no badge appearing. – Brittany Jul 28 '17 at 18:34
  • @meaning-matters Oddly enough, when I put that same line in the controller's viewDidLoad, no badge appears either? – Brittany Jul 28 '17 at 18:37
  • Stop the debugger in`viewDidLoad` and print (using the `po` command) `self.tabBarController.tabBar.items`. What do you get? – meaning-matters Jul 28 '17 at 18:48
  • you are doing this in the App delegate. meaning that you are trying to add the badge before the UItabbarcontroller is even loaded. You need to do this after the UITabbarcontroller is initiated in the viewdidload – Mohamad Bachir Sidani Jul 28 '17 at 18:50
  • @BashirSidani But AppDelegate only attempts to fire the nsnotification method when a notification has been received? Which is after the tab bar loads and the app is opened? Also, see above comment - if I set the badge number in viewDidLoad it doesn't appear either? – Brittany Jul 28 '17 at 18:54
  • makes sense, try putting your line of code directly after your initiation of the UItabbarcontroller (and after its items are added) can you see it updating? – Mohamad Bachir Sidani Jul 28 '17 at 18:56
  • @Brittany Did you try with `UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;` Then you can set the badge like `[[tabController.viewControllers objectAtIndex:3] tabBarItem].badgeValue = @"1";` ? – Piyush Jul 29 '17 at 06:02
  • call `postNotificationName ` by using timer.... Call it after 1 sec. ..... So that the UITab gets time to get initialize... Just in case... – Kusal Shrestha Jul 31 '17 at 09:13
  • @PiyushPatel Added self.window.rootViewController line to my notification method and still no dice. – Brittany Aug 01 '17 at 17:07
  • @PiyushPatel Added self.window.rootViewController line to my AppDelegate and NOW it works - thank you!! If you want to post it as an answer I'll accept it :) – Brittany Aug 01 '17 at 17:13

2 Answers2

0

[[self.tabBarController.tabBar.items objectAtIndex:0] setBadgeValue:nil]; set your count or number at place of nil to show on tab bar

try this it may help you

0

No NSNotification method needed - the following fixed the issue in AppDelegate.m:

-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

        UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
        [[tabController.viewControllers objectAtIndex:4] tabBarItem].badgeValue = @"1";


        NSLog(@"App notification received!");
        // do stuff when app is active

    }else{


        static int i=1;
        [UIApplication sharedApplication].applicationIconBadgeNumber = i++;

        //  NSLog(@"App notification received!");

        NSLog(@"App notification received background!");
    }
}
Brittany
  • 1,359
  • 4
  • 24
  • 63