【发布时间】:2018-02-14 16:11:53
【问题描述】:
这个问题已经在 SO 上得到回答。这是参考:
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)
}
【问题讨论】:
-
repeate的值是多少?random data是什么意思?此外,您实际上需要什么?您需要在每个月的第二个星期日触发通知还是从设置后每两周触发一次?这两个要求根本不一样。 -
repeat 是一个布尔变量,从中决定重复功能。我希望从 sDate 开始每 2 周重复一次。
标签: swift3 calendar uilocalnotification unnotificationrequest unnotificationtrigger