gpt4 book ai didi

ios - 如果时间过去,UILocalnotification 会直接触发

转载 作者:行者123 更新时间:2023-11-29 03:49:44 24 4
gpt4 key购买 nike

我已经在我的项目中添加了几个 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; 不是一个选项,因为我不想要这样。

最佳答案

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

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

关于ios - 如果时间过去,UILocalnotification 会直接触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17198827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com