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

时间:2013-08-02 10:14:50

标签: iphone ios objective-c uilocalnotification

在我的应用中,我要设置重复的UILocalNotification。我可以通过

将每天,每周等设置repeatInterval
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSDayCalendarUnit; // or any other calendarUnit

好的..没关系,但我要每14天设置一次repeatInterval。 我从this link开始知道我们只能使用其中一个NSCalendarUnit重复间隔。因此,您可以重复间隔为一分钟或一小时或一天但不是五分钟或三小时或14天。关于iOS 5或更高版本中此限制的任何想法(该文章是为iOS 4.0编写的)?

3 个答案:

答案 0 :(得分:6)

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

例如,如果你想要每20分钟发一次通知,则必须在20分钟内创建3个通知,重复间隔为NSHourCalendarUnit。

你的问题是,从周单位开始的下一件事是一个月,但一个月不是4周。

要实际设置每14天一次的通知,您必须创建26个通知,重复间隔为NSYearCalendarUnit。

答案 1 :(得分:1)

您可以在处理通知后的14天内取消通知并设置新的开火日期。即,当您的应用程序在前台运行时,请执行以下操作:

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

请参阅此SO主题: Set repeatInterval in local notification

我尝试过这种方式,但它确实有效。

答案 2 :(得分:0)

在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);
    }
}];

希望这可以帮助任何寻找此话题的人。

相关问题