0

我在我的项目中添加了几个 UILocalnotifications,确切地说是 24 个。这些通知都是这样的

NSTimeInterval timeAdjustment = [[NSDate date] timeIntervalSinceDate:[NSDate networkDate]];
        NSDate *date = [NSDate date];
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *dateComponents = [calendar components:
                                            (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit |
                                             NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit)
                                                       fromDate:date];

NSDateComponents *comps = [[NSDateComponents alloc] init];
            [comps setDay:[dateComponents day]];
            [comps setMonth:[dateComponents month]];
            [comps setYear:[dateComponents year]];
            [comps setHour:18];
            [comps setMinute:25];
            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDate *fireDate = [gregorian dateFromComponents:comps];
            UILocalNotification *alarm = [[UILocalNotification alloc] init];
            alarm.fireDate = [fireDate dateByAddingTimeInterval:timeAdjustment];
            alarm.soundName = @".aiff";
            alarm.alertBody = @"..";
            alarm.timeZone = [NSTimeZone defaultTimeZone];
            [[UIApplication sharedApplication] scheduleLocalNotification:alarm];

            NSDateComponents *comps2 = [[NSDateComponents alloc] init];
            [comps2 setDay:[dateComponents day]];
            [comps2 setMonth:[dateComponents month]];
            [comps2 setYear:[dateComponents year]];
            [comps2 setHour:18];
            [comps2 setMinute:30];
            NSCalendar *gregorian2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDate *fireDate2 = [gregorian2 dateFromComponents:comps2];
            UILocalNotification *alarm2 = [[UILocalNotification alloc] init];
            alarm2.fireDate = [fireDate2 dateByAddingTimeInterval:timeAdjustment];
            alarm2.soundName = @".aiff";
            alarm2.alertBody = @"..";
            alarm2.timeZone = [NSTimeZone defaultTimeZone];
            [[UIApplication sharedApplication] scheduleLocalNotification:alarm2];

所有这些通知都会在正确的时间触发,但问题是如果在通知时间之后通知将立即启动。例如,现在是 20.35,通知时间是 18.25 和 18.30,但它们现在仍然启动。我怎样才能防止这种情况?设置alarm.repeatInterval = NSDayCalendarUnit;不是一个选项,因为我不想要这个。

4

1 回答 1

1

如果当前时间超过了您为通知设置的时间,请不要安排通知:

if ([alarm.fireDate compare:[NSDate date]] == NSOrderedDescending) {
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
于 2013-06-19T19:03:49.950 回答