Trigger UILocalNotification for every 14 days (fortnightly) Swift

寵の児 提交于 2019-12-07 20:13:19

What you want to achieve is not possible using simple UNNotificationTriggers. For a notification to be repeated biweekly, you would need to set up a UNTimeIntervalNotificationTrigger with a timeInterval equivalent to two weeks. However, you cannot specify the first fire date of a time interval trigger, it start "ticking" as soon as you schedule it.

On the other hand, UNCalendarNotificationTrigger can be scheduled to fire at a certain date, but the problem with this is that you cannot specify a custom repeat interval for the notification trigger.

What you would need is to first, set up a non-repeating UNCalendarNotificationTrigger for the date specified by the user and once that notification is delivered, set up a UNTimeIntervalNotificationTrigger that fires every two weeks. The only issue with this approach is that the user would see a notification at the specified date as well, not only every two weeks after that. However, you can circumvent this issue by setting the notification to be delivered two weeks after the specified date, then the user won't notice the difference between the notifications.

@David answer is pretty much right. I want to add one thing here. If user will not tap on notification then you can not schedule other notifications. You can use background fetch for that. If IOS trigger background fetch then check current date with your schedule dates and generate new notifications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!