1

I'm developing a iOS app that manage Push Notifications. When a push comes up, the app badge is looking good, but where I'm having trouble is on the badges that appear inside the app. With inside the app i mean in the UITabBarItems that the app manages.

The app has a TabBarController where in two UITabBarItems a badge has to increment depending on wich Push Notification appeared. That is correctly handled in the server side, as I debuged and the push badge numbers comes ok. The thing is that the tab bar items are not updating ok.

Here's some part of my code:

This all happens inside de AppDelegate.m

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSString *user = [defaults stringForKey:@"id"];
   if (user){
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
       UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tabbar"];
       self.window.rootViewController= viewController;
   }

   return YES;
}

And here's where I handle the push and update the badges in the uitabbaritem:

-(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

   // Notify UAInbox to fetch any new messages if the notification
   // contains a rich application page id.
   if (application.applicationState != UIApplicationStateBackground) {
       UA_LINFO(@"Received remote notification: %@", userInfo);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshRequests" object:nil];
        NSString* type = [userInfo objectForKey:@"type"];
       if ([type isEqualToString:@"s"]){
           [[[[(UITabBarController*)self.window.rootViewController viewControllers]
           objectAtIndex: 2] tabBarItem] setBadgeValue:[[userInfo objectForKey:@"particularBadge"] stringValue]];
       }else{
           [[[[(UITabBarController*)self.window.rootViewController viewControllers]
           objectAtIndex: 0] tabBarItem] setBadgeValue:[[userInfo objectForKey:@"particularBadge"] stringValue]];
       }

   }else{
       // Notify UAPush that a push came in with the completion handler
       [[UAPush shared] handleNotification:userInfo
                       applicationState:application.applicationState
                 fetchCompletionHandler:completionHandler];

   }

   [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshRequests" object:nil];
   [self clearNotifications];
}

When I debug it, the badge in [[userInfo objectForKey:@"particularBadge"] stringValue] is 1 for example, but when the code finished passing over there the uitabbaritem doesn't update. Sometimes it works ok, but others it doesn't and I can't figure out why.

When the app launches it asks if the user is logged..If not the self.window.rootviewController is a LoginViewController. I don't know if this information should be important.

Any help would be appreciated!

FabKremer
  • 2,149
  • 2
  • 17
  • 29

0 Answers0