Echizzle
Echizzle

Reputation: 3459

Getting rid of UILocalNotification on App Icon

I have a to do list app that sends notifications for specific chores. Everything works great but the red circle with the number of notifications never goes away on the icon. What kind of code should I add to get rid of those? Thanks!

Here is some code (it all works) just to give you the gist

Sending the notification...

- (void)sendNotification {

NSString *choreTitle = [NSString stringWithFormat:@"%@", self.chore.title];
NSString *choreDetail = [NSString stringWithFormat:@"%@", self.chore.detail];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
if (localNotification) {
    localNotification.fireDate = self.datePicker.date;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = 0;
    localNotification.soundName = @"bell_tree.mp3";
    localNotification.alertBody = [NSString stringWithFormat: @"A friendly reminder, %@, %@", choreTitle, choreDetail];
    localNotification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm"];
NSDate *date = self.datePicker.date;
NSString *formattedDateString = [dateFormatter stringFromDate:date];

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"An alert will be sent to you on %@",formattedDateString] message:nil preferredStyle:UIAlertControllerStyleAlert];

[alertController addAction:[UIAlertAction actionWithTitle:@"Okay!" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

[self dismissViewControllerAnimated:YES completion:nil];

}]];

[self presentViewController:alertController animated:YES completion:nil];

}

In the app delegate...

- (void)applicationDidBecomeActive:(UIApplication *)application {

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]];
}
}

Upvotes: 0

Views: 49

Answers (1)

Tobi Nary
Tobi Nary

Reputation: 4596

Place

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Inside your applicationDidFinishLaunching and/or applicationDidBecomeActive.

Upvotes: 2

Related Questions