UILocalNotification repeatInterval 20天

我想创build本地通知与自定义间隔重复 (例如每20天)。 我知道我们有NSDayCalendarUnit,kCFCalendarUnitMonth …但我希望在20天设置重复间隔。 我不想每天都创build一个通知。

我真正需要的是连续21天重复通知,然后在7天后不再启动,然后是新的21天通知和7天没有…等。我应该安排所有这些日子,即使申请是无效。

要做到这一点,我决定创build一个21个通知,因为重复间隔= 28天的火灾date(这里是问题)

试试这个,你可以通过selectsetDaysetMonth ,…改变间隔:

 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay:3]; NSDate *date3Days = [calendar dateByAddingComponents:components toDate:[NSDate date] options:0]; UIApplication* app = [UIApplication sharedApplication]; NSArray* oldNotifications = [app scheduledLocalNotifications]; if ( oldNotifications ) { [app cancelAllLocalNotifications]; app.applicationIconBadgeNumber = 0; } UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init]; if (notifyAlarm) { notifyAlarm.fireDate = date3Days; notifyAlarm.timeZone = [NSTimeZone defaultTimeZone]; notifyAlarm.alertBody = NSLocalizedString( @"Push message", @""); notifyAlarm.soundName = @"sound.wav"; [app scheduleLocalNotification:notifyAlarm]; } 

如果你想设置一个特定的时间后UILocalNotifications应该出现,你可以创build一个上述解决scheme的方法,并在数组上循环显示你想显示通知的日子:

 NSArray *arrayNumbers = @[@5, @7, @14, @21, @30, @60, @90, @120]; NSDictionary *dictNotifications = [[NSUserDefaults standardUserDefaults] objectForKey:kUserDefAppStarts]; for ( NSNumber *bla in arrayNumbers ){ NSString *strKey = [NSString stringWithFormat:@"%@%@", kUserDefAppStarts, bla]; NSDictionary *dict = dictNotifications[strKey]; NSString *strMessageQuestion = dict[kKeyMessage]; [self createNotificationWithNumberOfDays:[bla integerValue] andMessage:strMessageQuestion userInfo:dict]; } 

这是你必须调用的方法

 + (void)createNotificationWithNumberOfDays:(int)days andMessage:(NSString *)message userInfo:(NSDictionary *)dict{ NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay:days]; NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0]; UIApplication *app = [UIApplication sharedApplication]; UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init]; if( notifyAlarm ){ [notifyAlarm setFireDate:dateAlert]; [notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]]; [notifyAlarm setSoundName:@"soundname"]; [notifyAlarm setAlertBody:message]; [notifyAlarm setUserInfo:dict]; [app scheduleLocalNotification:notifyAlarm]; } 

}

如果您想从当前时间起计划将来20天的UILocalNotification,那么您可以这样做:

 NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDateComponents *dateComp = [[NSDateComponents alloc] init]; int desiredAmountOfMonths = 4; for (int month = 0; month < desiredAmountOfMonths; month++) { dateComp.month = month; dateComp.day = 20; NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits]; UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.timeZone = [NSTimeZone defaultTimeZone]; } 

你将需要修改UILocalNotification什么消息,声音等,然后触发一旦完成自定义通知。