摆脱应用程序图标上的UILocalNotification

时间:2016-01-29 02:05:31

标签: ios objective-c uilocalnotification

我有一个待办事项列表应用,可以发送特定杂务的通知。一切都很好,但带有通知数量的红色圆圈永远不会消失在图标上。我应该添加什么样的代码来摆脱那些?谢谢!

这里有一些代码(一切正常)只是为了给你一个要点

发送通知......

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

}

在app delegate ...

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

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

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

1 个答案:

答案 0 :(得分:2)

放置

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

applicationDidFinishLaunching和/或applicationDidBecomeActive内。