每 14 天(每两周)触发一次 UILocalNotification Swift
Trigger UILocalNotification for every 14 days (fortnightly) Swift
问题已经在 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 天后我无法实现它。虽然,它来自随机数据。有人可以检查代码有什么问题吗?
Update:
根据您的建议 (),这是有效的最终代码!
//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
无法实现您想实现的目标。对于每两周重复一次的通知,您需要将 UNTimeIntervalNotificationTrigger
设置为 timeInterval
相当于两周。但是,您不能指定时间间隔触发器的第一个触发日期,它会在您安排后立即开始 "ticking"。
另一方面,UNCalendarNotificationTrigger
可以安排在特定日期触发,但问题是您无法为通知触发器指定自定义重复间隔。
首先,您需要为用户指定的日期设置一个非重复 UNCalendarNotificationTrigger
,然后在发送该通知后,设置一个每两次触发一次的 UNTimeIntervalNotificationTrigger
周。这种方法的唯一问题是用户也会在指定日期看到通知,而不仅仅是在那之后每两周一次。不过,您可以通过将通知设置为在指定日期两周后发送来规避此问题,这样用户就不会注意到通知之间的差异。
@David 的回答非常正确。我想在这里补充一件事。如果用户不会点击通知,则您无法安排其他通知。您可以为此使用后台获取。如果 IOS 触发后台获取,则检查当前日期与您的计划日期并生成新通知。
问题已经在 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 天后我无法实现它。虽然,它来自随机数据。有人可以检查代码有什么问题吗?
Update:
根据您的建议 (
//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
无法实现您想实现的目标。对于每两周重复一次的通知,您需要将 UNTimeIntervalNotificationTrigger
设置为 timeInterval
相当于两周。但是,您不能指定时间间隔触发器的第一个触发日期,它会在您安排后立即开始 "ticking"。
另一方面,UNCalendarNotificationTrigger
可以安排在特定日期触发,但问题是您无法为通知触发器指定自定义重复间隔。
首先,您需要为用户指定的日期设置一个非重复 UNCalendarNotificationTrigger
,然后在发送该通知后,设置一个每两次触发一次的 UNTimeIntervalNotificationTrigger
周。这种方法的唯一问题是用户也会在指定日期看到通知,而不仅仅是在那之后每两周一次。不过,您可以通过将通知设置为在指定日期两周后发送来规避此问题,这样用户就不会注意到通知之间的差异。
@David 的回答非常正确。我想在这里补充一件事。如果用户不会点击通知,则您无法安排其他通知。您可以为此使用后台获取。如果 IOS 触发后台获取,则检查当前日期与您的计划日期并生成新通知。