UILocalNotification应该在每个工作日重复一次,但是周末也会触发

我有一个UILocalNotification,应该从星期一到星期五每天一次,但不是在周末。 我认为将通知的repeatInterval属性设置为NSWeekdayCalendarUnit将完成此操作。 可悲的是,我的通知仍然在周末开火。 谁能build议为什么? 这是我的代码:

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.alertAction = @"View"; localNotification.alertBody = NSLocalizedString(@"ALERT_MESSAGE", nil); localNotification.soundName = UILocalNotificationDefaultSoundName; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM-dd-yyyy HH:mm"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]]; // Notification fire times are set by creating a notification whose fire date // is an arbitrary weekday at the correct time, and having it repeat every weekday NSDate *fireDate = [dateFormatter dateFromString:@"01-04-2012 11:00"]; localNotification.fireDate = fireDate; localNotification.repeatInterval = NSWeekdayCalendarUnit; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; break; [localNotification release]; 

iOS平日只是一周内的一天。 它不具有周末的“工作周”内涵。

该文件说明了一点更清楚,build议您使用1-7作为单位:

NSWeekdayCalendarUnit

指定星期几单位。

相应的值是一个kCFCalendarUnitSecond。 等于kCFCalendarUnitWeekday。 星期几单位是数字1到N(公历日历N = 7,1星期日)。

资料来源: http : //developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html

要正确设置从星期一到星期五的通知,以下是一些框架代码。 你必须执行5次,所以将它封装在fireDate参数的方法里面会很好。 我已经展示过星期一怎么做了。

 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; // Set this to an NSDate that is for the time you want, on Monday notification.fireDate = fireDate; // Repeat every week notification.repeatInterval = NSWeekCalendarUnit; 

您必须为本地通知单独设置一天,并将本地通知repeatInterval设置为NSWeekCalendar单元。 如

  [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:2 :tempDict] andRepeatInterval:NSWeekCalendarUnit]; [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:3 :tempDict]andRepeatInterval:NSWeekCalendarUnit]; [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:4 :tempDict] andRepeatInterval:NSWeekCalendarUnit]; [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:5 :tempDict] andRepeatInterval:NSWeekCalendarUnit]; [self notificationWithItem:tempDict Date:[self SetDateForAlarmWithWeekday:6 :tempDict] andRepeatInterval:NSWeekCalendarUnit]; 

在这里,我已经使setDateForAlarmWithWeekday返回date设置在本地通知的fireDate属性和notificationWithItem设置本地通知。

 -(void) notificationWithItem:(NSDictionary*)tmpdict Date:(NSDate *)date andRepeatInterval:(NSCalendarUnit)CalUnit { UILocalNotification *localNotification =[[UILocalNotification alloc]init]; if (localNotification==nil) { return; } localNotification.fireDate=date; localNotification.timeZone=[NSTimeZone defaultTimeZone]; localNotification.repeatCalendar=[NSCalendar currentCalendar]; localNotification.alertBody=[NSString stringWithFormat:@"%@",[tmpdict objectForKey:@"Reminder"]]; NSDictionary *snoozeDic=[tmpdict objectForKey:@"Snooze"]; if ([[snoozeDic valueForKey:@"Switch"]intValue]==1) { localNotification.alertAction=@"Snooze"; }else { localNotification.hasAction=NO; } localNotification.repeatInterval=CalUnit; localNotification.soundName=[NSString stringWithFormat:@"%@.caf",[tmpdict objectForKey:@"Tone"]]; localNotification.userInfo=[NSDictionary dictionaryWithObject:tmpdict forKey:@"AlarmInfo"]; localNotification.applicationIconBadgeNumber=1; [[UIApplication sharedApplication]scheduleLocalNotification:localNotification]; } -(NSDate*)SetDateForAlarmWithWeekday:(int)WeekDay:(NSDictionary*)dics { NSLog(@"set date for alarm called"); NSCalendar *calendar=[NSCalendar currentCalendar]; [calendar setTimeZone:[NSTimeZone defaultTimeZone]]; unsigned currentFlag=NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSWeekdayCalendarUnit; NSDateComponents *comp=[calendar components:currentFlag fromDate:[NSDate date]]; NSArray *array=[[dics objectForKey:@"Time"] componentsSeparatedByString:@" "]; NSInteger hour=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0] intValue]; NSInteger min=[[[[array objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1] intValue]; if ([[array objectAtIndex:1] isEqualToString:@"PM"]) { hour=hour+12; } else { if (hour==12) { hour=0; } } comp.hour=hour; comp.minute=min; comp.second=0; NSLog(@"set date for alarm (%i:%i:%i)",comp.hour,comp.minute,comp.second); NSLog(@"weekday :%i ",WeekDay); NSLog(@"comp weekday %i",comp.weekday); int diff=(WeekDay-comp.weekday); NSLog(@"difference :%i",diff); int multiplier; if (WeekDay==0) { multiplier=0; }else { multiplier=diff>0?diff:(diff==0?diff:diff+7); } NSLog(@"multiplier :%i",multiplier); return [[calendar dateFromComponents:comp]dateByAddingTimeInterval:multiplier*secondsInOneDay]; } 

我希望这会帮助你。

我也搜查过它。 下面的代码适合我。 星期日到星期六的星期值1到7以及要发起的动作的通知主体,并指定date,然后通知将在该特定date发生。 对于周末来说,在周六和周日,这个函数的值分别为7和1。

希望这对你有所帮助。

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

类似,但在Swift中。

请记住,WeekDay从1 = Sunday开始并转到7.请参阅Apple文档: https : //developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/

 func scheduleLocalNotification(date:NSDate, weekDay:Int){ guard let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian), let groupName = dateFormatter?.stringFromDate(date) else { return } calendar.timeZone = NSTimeZone.defaultTimeZone() let dateComponents = NSDateComponents() dateComponents.calendar = calendar dateComponents.hour = calendar.component(.Hour, fromDate: date) dateComponents.minute = calendar.component(.Minute, fromDate: date) dateComponents.weekOfYear = calendar.component(.WeekOfYear, fromDate: date) dateComponents.weekday = weekDay let localNotificaion = UILocalNotification() localNotificaion.fireDate = calendar.dateFromComponents(dateComponents) localNotificaion.alertBody = "Hey!" localNotificaion.timeZone = NSTimeZone.defaultTimeZone() localNotificaion.userInfo = ["Time": groupName] localNotificaion.repeatInterval = .WeekOfYear UIApplication.sharedApplication().scheduleLocalNotification(localNotificaion) }