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

Swift3 每14天(每两周)触发一次UILocalNotification,swift3,calendar,uilocalnotification,unnotificationrequest,unnotificationtrigger,Swift3,Calendar,Uilocalnotification,Unnotificationrequest,Unnotificationtrigger,问题已经就此得到了回答。以下是参考资料: 但到目前为止,我所尝试的是: 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

问题已经就此得到了回答。以下是参考资料:

但到目前为止,我所尝试的是:

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天后我无法实现它。尽管如此,它来自随机数据。有人能检查一下代码有什么问题吗

更新:

根据您的建议(),这里是最后的代码,它起作用了

//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
,时间间隔等于两周。但是,您不能指定时间间隔触发器的第一个触发日期,它会在您计划触发日期后立即开始“滴答”

另一方面,
UNCalendarNotificationTrigger
可以计划在某个日期触发,但问题是无法为通知触发器指定自定义重复间隔


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

@David answer非常正确。我想在这里补充一点。如果用户不点击通知,则无法安排其他通知。您可以使用后台获取。如果IOS触发后台提取,则将当前日期与您的计划日期进行检查,并生成新通知

repeate
的值是多少?你所说的随机数据是什么意思?此外,你真正需要什么?您是否需要在每个月的第二个星期日触发通知,或者从设置开始每两周触发一次通知?这两个要求根本不一样。repeat是一个bool变量,它决定了repeat功能。我希望它从sDate开始每两周重复一次,sDate是未来的任何日期。你能检查更新的问题吗,还是我应该提供演示?你能检查一下,让它按预期工作吗?不用担心,很高兴我能帮上忙。对于每两周一次,您可以安排两次重复的通知weekly@pnavk你误解了每两周一次,OP明确要求每两周一次,而不是一周两次。你能在背景获取部分添加一个示例吗?据我所知,当后台提取发生时,系统自行决定,您无法安排它,因此我不知道如何在这里使用它。