[英]How do I setup local notification repeated each 7, 14, 28 days from specific day
I need to push local notification each 7, 14, 28 days. 我需要每7、14、28天推送一次本地通知。 I had successfully setup to push weekly (7 days) I am not fully understand how DateComponents works.
我已经成功设置为每周(7天)推送,但我还不完全了解DateComponents的工作方式。 IE: I need to repeat notification each minute, I do
Calendar.current.dateComponents([.second], from: fireDate)
How does it work with repeated 14 days, 28 days. IE:我需要每分钟重复一次通知,我做
Calendar.current.dateComponents([.second], from: fireDate)
它如何在重复14天,28天后起作用。
// datetime is timestamp which I can choose: ie: May 02, 2029
let fireDate = Date(timeIntervalSince1970: Double(truncating: datetime) / 1000)
let fireDateComps = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fireDate)
let content = UNMutableNotificationContent()
content.title = title
content.body = note
content.sound = UNNotificationSound.default
content.categoryIdentifier = "timer.category"
var triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fireDate)
var repeats = false
switch (recurring) {
case "weekly":
triggerDate = Calendar.current.dateComponents([ .weekday , .hour , .minute , .second], from: fireDate)
repeats = true
print("reminder repeat weekly")
case "every_two_weeks":
print("triggerDate 1 \(fireDate)")
let aaa = Calendar.current.date(byAdding: .day, value: 14, to: fireDate)
print("triggerDate 2 \(aaa)")
triggerDate = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: aaa!)
print("triggerDate 3 \(triggerDate)")
repeats = true
print("reminder repeat every_two_weeks")
case "every_four_weeks":
triggerDate = Calendar.current.dateComponents([ .month , .hour , .minute , .second], from: fireDate)
repeats = true
print("reminder repeat every_four_weeks")
default:
triggerDate = Calendar.current.dateComponents([ .year, .month, .day , .hour , .minute , .second], from: fireDate)
repeats = false
print("No recurring")
}
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: repeats)
print("trigger \(trigger), firstRepeatingDate \(triggerDate)")
let request = UNNotificationRequest(identifier: id,
content: content, trigger: trigger)
self.center.add(request) {(error) in
if let error = error {
Log.nonFatal("Save notification failed \(String(describing: error))")
reject("reminder", "Save reminder notification failed", error)
}
print("Saved recurring notification")
resolve(true)
}
It's been a while since I have used these apis, but based on the documentation here is a quick example for a couple of them (the one you use and the one I am proposing): 自从我使用这些api已经有一段时间了,但是根据文档,这里是几个示例的快速示例(您使用的是我建议的一个):
// UNCalendarNotificationTrigger (Trigger at specific date & time)
// This example fires a notification every day at 08:30
var date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
/* I guess date.day = 1 would fire at the 1st day of each month
whereas date.weekOfMonth = 2 would fire at the second week of each month etc
*/
What you may want instead is to fire periodically: 相反,您可能需要定期触发:
// UNTimeIntervalNotificationTrigger (Trigger after an amount of time has elapsed)
// This example fires a notification every 14 days (14d * 24h * 60m * 60s)
let days = 14
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(days * 24 * 60 * 60), repeats: true)
I had resolved the question. 我已经解决了这个问题。
func calendarAddNotification(id: String, title: String, datetime: NSNumber, note: String, recurring: String) {
let fireDate = Date(timeIntervalSince1970: Double(truncating: datetime) / 1000)
let content = UNMutableNotificationContent()
content.title = title
content.body = note
content.sound = UNNotificationSound.default
content.categoryIdentifier = "timer.category"
// setup normal notification
let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
let request = UNNotificationRequest(identifier: id,
content: content, trigger: trigger)
self.center.add(request) {(error) in
if let error = error {
Log.nonFatal("Save notification failed \(String(describing: error))")
} else {
print("Saved notification")
}
}
// non-recurring notification, return
if (!["weekly", "every_two_weeks", "every_four_weeks"].contains(recurring)) {
print("No recurring notification")
return
}
// recurring notification
var interval = 0
switch (recurring) {
case "weekly":
interval = 7
print("reminder repeat weekly")
case "every_two_weeks":
interval = 14
print("reminder repeat every_two_weeks")
case "every_four_weeks":
interval = 28
print("reminder repeat every_four_weeks")
default:
print("No recurring")
}
// add interval day to the fireDate
let recurringDateComponent = Calendar.current.date(byAdding: .day, value: interval, to: fireDate)
let recurringTrigger = UNTimeIntervalNotificationTrigger(timeInterval: recurringDateComponent!.timeIntervalSinceNow, repeats: true)
let recurringRequest = UNNotificationRequest(identifier: "recurring_" + id,
content: content, trigger: recurringTrigger)
self.center.add(recurringRequest) { (error) in
if let error = error {
Log.nonFatal("Save recurring notification failed \(String(describing: error))")
} else {
print("Saved recurring notification")
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.