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

人气:603 发布:2022-10-16 标签: calendar swift3 uilocalnotification unnotificationrequest unnotificationtrigger

问题描述

问题已经在SO上得到了回答。这是参考:

The question has been already answered on SO. Here is the reference:

iOS通知触发器:每两周和/或每季度

但是到目前为止,我一直在尝试:

But what I have tried is so far:

var fortnightPart1 = DateComponents()
fortnightPart1.weekday = Calendar.current.component(.day, from: Sdate) //(Day..here Sunday)
fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
fortnightPart1.hour = Calendar.current.component(.hour, from: Sdate)
fortnightPart1.minute = Calendar.current.component(.minute, from: Sdate)
fortnightPart1.second = Calendar.current.component(.second, from: Sdate)
trigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: repeate)

14岁之后我无法实现它天。虽然,它来自随机数据。有人可以检查代码有什么问题吗?

I can't able to achieve it it after 14 days. Although, it comes on a random data. Can anybody check what's wrong with the code?

更新:

Update:

根据您的建议( https://stackoverflow.com/a/46072497/7408802 ),

//for 1st time notification
 let center = UNUserNotificationCenter.current()
 let content = UNMutableNotificationContent()
 content.title = title
 content.body = body
 content.categoryIdentifier = "\(id)"
 content.sound = UNNotificationSound.default()
 fortnightPart1.hour = Calendar.current.component(.hour, from: dateToFireON)
 fortnightPart1.minute = Calendar.current.component(.minute, from: dateToFireON)
 fortnightPart1.second = Calendar.current.component(.second, from: dateToFireON)
 trigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: false)
 content.userInfo = ["NotificationID": id,"repeate":repeate,"resedule":true]
 let request = UNNotificationRequest(identifier: "\(id)", content: content, trigger: trigger)
 center.add(request)

//when first notification triggered, we need to set another for 14 days repeat in UIApplicationDelegate
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = notification.alertTitle!
    content.body = notification.alertBody!
    content.categoryIdentifier = "\(reminderId)"
    content.userInfo = ["NotificationID": reminderId,"repeate":true,"resedule":false]
    content.sound = UNNotificationSound.default()
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1209600, repeats: true)
    let request = UNNotificationRequest(identifier:"\(reminderId)", content: content, trigger: trigger)
    center.add(request, withCompletionHandler: nil)
}

推荐答案

使用简单的 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.

570