为什么只设置一个本地通知?

时间:2015-06-03 07:02:55

标签: ios uilocalnotification

我正在使用for循环添加本地通知,但当我检查当前使用[[UIApplication sharedApplication] scheduledLocalNotifications]安排了多少通知时,它会显示当前只安排了一个通知。

这是我的代码:

for (int i = ((int)day - 1) * 6; i < ((int)day - 1) * 6 + 64; i++) {
    NSLog(@"Notification #%d", i);

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSDictionary *currentPrayTime = [self.prayTimes objectAtIndex:i];

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy.MM.dd HH:mm"];

    NSDate *dateToSetNotification = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@ %@", [currentPrayTime objectForKey:@"date"], [currentPrayTime objectForKey:@"time"]]];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setMinute:-10];
    NSDate *dateBeforePray = [gregorian dateByAddingComponents:offsetComponents toDate:dateToSetNotification options:0];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = dateBeforePray;
    localNotif.timeZone = [NSTimeZone localTimeZone];

    localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%d минут до %@", nil), [[NSUserDefaults standardUserDefaults] integerForKey:@"minutesBefore"], [currentPrayTime objectForKey:@"title"]];

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    localNotif.userInfo = currentPrayTime;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

感谢您的任何进展!

修改

显示的唯一通知是循环中的最后一个本地通知。

1 个答案:

答案 0 :(得分:0)

请重新检查UILocalNotification fireDate,它不应该是过去日期意味着小于当前日期&amp;时间。

localNotif.fireDate = dateBeforePray;
NSLog(@"%@",localNotif.fireDate);

希望这会对你有所帮助。