每14天(两周)后重复本地通知?

在我的应用程序中,我必须设置重复的UILocalNotification 。 我可以通过做每日,每周等设置repeatInterval

 UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.repeatInterval = NSDayCalendarUnit; // or any other calendarUnit 

好吧,没关系,但是我必须每14天设置一次repeatInterval。 我从这个链接中知道我们只能使用NSCalendarUnit重复间隔之一。 所以你可以有一分钟或一小时或一天,但不是五分钟或三小时或十四天的重复间隔。 任何有关iOS 5或更高版本(该文章是为iOS 4.0编写的)限制的想法?

您只能将重复间隔设置为日历单位。 为了获得正确的时间间隔,你可能不得不设置几个通知。

例如,如果您想要每隔20分钟发出一次通知,则必须使用NSHourCalendarUnit的重复间隔,在20分钟内创build3个通知。

在你的情况下的问题是,从本周单位的下一个事情是一个月,但一个月不完全是4个星期。

要实际设置每14天的通知,您将不得不创build26重复间隔NSYearCalendarUnit的通知。

您可以在处理通知之后的14天内取消通知并设置一个新的启动date。 即当您的应用程序在前台运行时,请执行以下操作:

 (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

看到这个话题: 在本地通知中设置repeatInterval

我已经试过这种方式,它的工作原理。

在iOS 10中,您可以使用UNTimeIntervalNotificationTrigger每14天后重复一次通知。

 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:(14*24*3600) repeats: YES]; NSLog(@"td %@", trigger.nextTriggerDate); UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger]; [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Something went wrong: %@",error); } else { NSLog(@"Created! --> %@",request); } }]; 

希望这将有助于任何人寻找这个话题。