Timothy
Timothy

Reputation: 128

UILocalnotification fires directly if time passed

I've added a couple of UILocalnotifications to my project, 24 to be exact. These notifications all look like this

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

All of these notification do fire at the right time, but the problem is that the notification will launch immediately if the is after the notification time. So for example, it's 20.35 now, the notification times were 18.25 and 18.30 but both of them still launch now. How can I prevent this? Setting alarm.repeatInterval = NSDayCalendarUnit; is not an option since I don't want this.

Upvotes: 0

Views: 408

Answers (1)

Idles
Idles

Reputation: 1131

Don't schedule the notification if the current time is past the time you set for the notification:

if ([alarm.fireDate compare:[NSDate date]] == NSOrderedDescending) {
    [[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}

Upvotes: 1

Related Questions