我尝试在收到推送通知时为 UITabBarItem
设置 badgeValue。我正在使用此代码。这里的 UITabBarController
不是 rootViewController。我在 resign active 方法中尝试了同样的事情,但它也不起作用。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UITabBarController *tabBarController = (UITabBarController *)[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"tabBarController"] ;
[[tabBarController.tabBar.items objectAtIndex:2] setBadgeValue:@"1"];
}
最佳答案
我认为您可以在收到 remoteNotification 时使用 NSNotificationCenter 发布通知
在你的UITabBarController
初始化方法中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationReceived:) name:@"pushNotification" object:nil];
和
在 myNotificationReceived:
[[self.tabBar.items objectAtIndex:2] setBadgeValue:@"1"];
当您收到远程通知时
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
这样就可以获得完整的RemoteNotification信息
关于ios - 收到通知时在标签栏项目上设置角标(Badge),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26795868/