这个问题已经得到了回答。以下是参考资料:
iOS Notification Trigger: fortnightly and/or quarterly
但到目前为止,我尝试的是:
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天后我就做不到了。不过,这是随机数据。有人能检查一下密码有什么问题吗?
更新:
根据您的建议(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)
}发布于 2017-09-06 10:10:26
使用简单的UNNotificationTrigger是不可能实现的,如果要每两周重复一次通知,就需要设置一个timeInterval相当于两周的timeInterval。但是,您不能指定时间间隔触发器的第一个触发日期,它会在安排时间间隔触发器时立即开始“滴答”。
另一方面,可以计划在特定日期触发UNCalendarNotificationTrigger,但问题是不能为通知触发器指定自定义重复间隔。
您需要的是首先为用户指定的日期设置一个不重复的UNCalendarNotificationTrigger,并且一旦发送通知,就设置一个每两周触发一次的UNTimeIntervalNotificationTrigger。这种方法唯一的问题是用户也会在指定的日期看到通知,而不仅仅是在那之后的两周一次。但是,您可以通过将通知设置为在指定日期后两周发送,从而避免此问题,这样用户就不会注意到通知之间的差异。
发布于 2017-09-07 09:28:46
@大卫的回答是完全正确的。我想在这里补充一件事。如果用户不点击通知,则无法安排其他通知。您可以使用后台获取。如果IOS触发后台提取,则使用计划日期检查当前日期并生成新通知。
https://stackoverflow.com/questions/46070648
复制相似问题