Resolved Showing Badge on UIButton

Discussion in 'iOS Programming' started by RagingGoat, Feb 18, 2014.

  1. RagingGoat, Feb 18, 2014
    Last edited: Feb 18, 2014

    RagingGoat macrumors 6502

    Joined:
    Jun 21, 2010
    #1
    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];
    }
    
     
  2. RagingGoat thread starter macrumors 6502

    Joined:
    Jun 21, 2010
    #2
    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];
    }
    
     

Share This Page