iOS-如果app在后台,是否可以更新本地通知的徽章数?

时间:2016-04-05 13:04:21

标签: ios objective-c iphone notifications uilocalnotification

我正在尝试在应用关闭时更新应用的徽章数量,任何人都可以告诉我们,如果应用关闭,我们可以更新吗?我使用以下代码:

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponent = [gregCalendar components:NSCalendarUnitYear  | NSCalendarUnitWeekOfYear fromDate:[NSDate date]];

    NSDateFormatter *outputFormatterHour = [[NSDateFormatter alloc] init];
    [outputFormatterHour setDateFormat:@"HH"];
    NSDateFormatter *outputFormatterMin = [[NSDateFormatter alloc] init];
    [outputFormatterMin setDateFormat:@"mm"];
    int hour = [[outputFormatterHour stringFromDate:_selectTimePicker.date] intValue];
    int min = [[outputFormatterMin stringFromDate:_selectTimePicker.date] intValue];

    [dateComponent setHour:hour];
    [dateComponent setMinute:min];

    NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

    UILocalNotification *localNotification = [[UILocalNotification alloc]init];

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    [localNotification setAlertTitle:@"Notification"];
    [localNotification setAlertBody:@"Body" ];
    [localNotification setAlertAction:@"Open App"];
    [localNotification setHasAction:YES];

    [localNotification setAlertLaunchImage:@"AppIcon"];

    localNotification.applicationIconBadgeNumber++;
    [localNotification setFireDate:fireDate];
    localNotification.repeatInterval = NSCalendarUnitDay;
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

0 个答案:

没有答案