每周在特定的date和时间发出通知

我想在每个星期天的晚上8点触发一个UILocalNotification ,不过,我每天晚上8点都有一个启动date。

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

谢谢。

您错过了NSDateComponents init函数中的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; //... 

我也搜查过它。 下面的代码适合我。 星期日到星期六的星期值1到7以及要发起的动作的通知主体,并指定date,然后通知将在该特定date发生。 设定date时会自动设定。

希望这对你有所帮助。

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