Repeating a Local Notification after every 14 days(two weeks)?

后端 未结 3 1959
囚心锁ツ
囚心锁ツ 2020-12-17 01:43

In my app i\'ve to set repetitive UILocalNotification. I\'m able to set repeatInterval as daily, weekly etc by doing

UILocalNotification *localN         


        
相关标签:
3条回答
  • 2020-12-17 02:12

    You can cancel the notification and set a new one fire date at 14 days from now when handling the notification. i.e. when your app is running in the foreground, do it in:

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

    See this SO topic: Set repeatInterval in local notification

    I've tried this way, and it works.

    0 讨论(0)
  • 2020-12-17 02:18

    In iOS 10, you can use UNTimeIntervalNotificationTrigger to repeat notification after every 14 days.

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

    Hope this will help anyone looking for this topic.

    0 讨论(0)
  • 2020-12-17 02:30

    You can only set the repeat interval to a calendar unit. In order to get the right time interval you may have to set several notifications though.

    If you for instance wanted a notification every 20 minutes you would have to create 3 notifications 20 minutes apart with a repeat interval of NSHourCalendarUnit.

    The problem in your case is that the next thing up from the week unit is a month but a month is not exactly 4 weeks.

    To actually set a notification for every 14 days you will have to create 26 notifications with a repeat interval of NSYearCalendarUnit.

    0 讨论(0)
提交回复
热议问题