每周在特定日期和时间触发通知
Posted
技术标签:
【中文标题】每周在特定日期和时间触发通知【英文标题】:Fire a notification at a specific day and time every week 【发布时间】:2013-11-21 01:34:57 【问题描述】:我想在每周日晚上 8 点触发UILocalNotification
,但是,我每天晚上 8 点都有开火日期。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
notification.fireDate = fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [NSString stringWithFormat: @"New updates!"] ;
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"New updates added for that week!"] forKey:@"new"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName=UILocalNotificationDefaultSoundName;
NSLog(@"notification: %@",notification);//it indicates that the notif will be triggered today at 8PM and not Sunday.
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
谢谢。
【问题讨论】:
试试这个 notification.repeatInterval = NSWeekCalendarUnit; @Chany:它跳到下周(下周二),但不是在周日。这是我对NSWeekCalendarUnit
的看法:next fire date = Tuesday, November 26, 2013 at 8:00:00 PM
【参考方案1】:
您错过了 NSDateComponents
初始化函数中的 NSWeekCalendarUnit
。将NSWeekCalendarUnit
添加到其中并将repeatInterval
设置为NSWeekCalendarUnit
,则输出为
next fire date = Sunday, November 24, 2013 at 8:00:00 PM
代码在这里:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setWeekday: 1] ; //for fixing Sunday
[componentsForFireDate setHour: 20] ; //for fixing 8PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
//...
notification.repeatInterval = NSWeekCalendarUnit;
//...
【讨论】:
非常感谢。没有你,我永远不会考虑。谢谢! 嗨@chancyWu,我需要在一周内再增加一天。对于前需要设置每周的星期一和星期二获取本地通知。我该怎么办?提前致谢【参考方案2】:我也搜索过。下面的代码对我有用。将工作日值 1 传递到 7 星期日到星期六,通知正文与您要触发的操作并指定您的日期,然后通知将在该特定日期出现。 设置日期时会自动设置时间。
希望对您有所帮助。
-(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
[componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
// [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
// [componentsForFireDate setMinute:0] ;
// [componentsForFireDate setSecond:0] ;
notification.repeatInterval = NSWeekCalendarUnit;
notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
【讨论】:
如果我给 [componentsForFireDate setDate: -1];减一?以上是关于每周在特定日期和时间触发通知的主要内容,如果未能解决你的问题,请参考以下文章