且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每14天(每两周一次)触发一次UILocalNotification

更新时间:2023-12-02 19:24:34

使用简单的 UNNotificationTrigger s不可能实现您想要的目标。为了使通知每两周重复一次,您需要设置一个 UNTimeIntervalNotificationTrigger ,其 timeInterval 相当于两周。但是,您不能指定时间间隔触发器的第一个触发日期,它会在您计划后立即开始滴答。

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.

另一方面, UNCalendarNotificationTrigger 可以安排在某个日期触发,但是这样做的问题是您不能为通知触发器指定自定义重复间隔。

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.

首先需要为用户指定的日期设置一个非重复的 UNCalendarNotificationTrigger ,并在通知发送后,设置一个 UNTimeIntervalNotificationTrigger 每两周触发一次。这种方法的唯一问题是,用户也将在指定的日期看到通知,而不仅仅是在此之后的两周。但是,您可以通过将通知设置为在指定日期后两周发送,来避免此问题,这样用户就不会注意到通知之间的差异。

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.