Question

I am wanting to add a badge icon to a UIButton within my app when there is a notification sent and I'm not quite sure how to achieve this. It would need to match the badge on the springboard icon and both the springboard badge and the badge on the UIButton would need to go away when that UIButton is pressed in the app.

UPDATE:

I got a badge to appear but when I open the app the badge gets reset back to zero when the app gets opened so I end up not seeing the badge on the button. I know the badge works because I tested it by doing this:

[[UIApplication sharedApplication]setApplicationIconBadgeNumber:1];

Here is the code for the badge:

NSString *badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];

    JSCustomBadge *actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
    actionAlertBadge.frame = CGRectMake(188, 6, 30, 30);

    [self.view addSubview:actionAlertBadge];

    if ([badgeNumber isEqual:@"0"])
    {
        actionAlertBadge.hidden = YES;
    }

How can I have the badge not get reset when opening the app, but instead reset when pressing that button in the app?

I have this working but still have one problem. If the app isn't running in the background, the badge shows up on the springboard icon and on the UIButton. When the button is pressed, the new view opens and the badge is reset so when the user goes back to the screen with the UIButton, the badge is no longer there. The issue I'm having is when the app is running in the background, the badge doesn't show on the button, but shows on the springboard.

- (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];
}  

In viewDidLoad

badgeNumber = [NSString stringWithFormat:@"%d", [[UIApplication sharedApplication]applicationIconBadgeNumber]];

    actionAlertBadge = [JSCustomBadge customBadgeWithString:badgeNumber];
    actionAlertBadge.frame = CGRectMake(83, 6, 30, 30);

    [self.view addSubview:actionAlertBadge];

    if ([badgeNumber isEqualToString:@"0"])
    {
       actionAlertBadge.hidden = YES;
    }
Was it helpful?

Solution

For Badge on UIButton You can use some custom library available here,

And to set/ get your Application badge you can use Method,

[UIApplication sharedApplication]setApplicationIconBadgeNumber:1]

EDIT

The issue I'm having is when the app is running in the background, the badge doesn't show on the button, but shows on the springboard.

If your app is in Background Badge will show on App icon, if you are using push notification than badge value set from payload,

{"aps": {"alert":"content test","badge":1,"sound":"default"}}

or you are using localnotification badge value set in applicationIconBadgeNumber property of UILocalNotification ,i.e.

 notification.applicationIconBadgeNumber=1;

So if your app in background you have two delegate methods for different notifications,

So implement that delegate methods Local notification didReceiveLocalNotification: and didReceiveRemoteNotification: for Push notification.

So please refer Apple Document before implementing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top