简体   繁体   中英

set badge on tabbar item when notification received

I tried to set badgeValue for UITabBarItem when push notification is received.I am using this code. Here the UITabBarController is not a rootViewController. I tried the same thing in resign active method but there also its not working.

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

  UITabBarController *tabBarController = (UITabBarController *)[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"tabBarController"] ;
  [[tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:@"1"];

}

I think that you can use NSNotificationCenter to post notification when you received a remoteNotification

In your UITabBarController initialize method

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

And In myNotificationReceived:

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

When you receive a remote notification

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

In this way, you can get whole RemoteNotification information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM