Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RagingGoat

macrumors 6502
Original poster
Jun 21, 2010
307
15
I am using Urban Airship for push notifications and showing a badge on a UIButton on the home screen of my app. This works fine if the app is in a closed state and not in the background. The problem I'm having is that if the app isn't in a closed state and a push notification comes through, the home screen of the app doesn't refresh to show the badge on the button.

Here is the code I'm currently using to show the badge.

In viewDidLoad
Code:
badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];
    
    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);
    
    if ([badgeNumber isEqualToString:@"0"])
    {
        actionAlertBadge.hidden = YES;
    }
    
    [self.view addSubview:actionAlertBadge];

Button
Code:
- (IBAction)gotoActionAlerts
{
    ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc]initWithStyle:UITableViewStylePlain];
    WebViewController *wvc = [[WebViewController alloc]init];
    [actionAlerts setWebViewController:wvc];
    actionAlerts.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [[UAPush shared] resetBadge];
    actionAlertBadge.hidden = YES;
    [self.navigationController pushViewController:actionAlerts animated:YES];
}
 
Last edited:

RagingGoat

macrumors 6502
Original poster
Jun 21, 2010
307
15
I resolved this by adding this to viewDidLoad
Code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];

and implementing this
Code:
- (void)applicationWillEnterForeground:(NSNotification *)notification
{
    badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];
    
    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);
    
    if ([badgeNumber isEqualToString:@"0"])
    {
        actionAlertBadge.hidden = YES;
    }
    
    [self.view addSubview:actionAlertBadge];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.