我想触发每周一晚上 7:00 发出的每周通知。
我看到这样的代码:
let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
- 我应该将开始日期设置为什么?
- 此外,日期是否需要在现在?
我正在使用 iOS 10+ 和 swift 3。
最佳答案
传递要生成本地通知的日期
let strDate = (data as AnyObject).value(forKey: "not_date") as? String
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-MM-dd hh:mm a"
let notidate = dateformatter.date(from: strDate!)
需要先stattnotificationdate like = 2018-06-25 07:00 PM 之后需要加上时间间隔
var dateStart = Date()
dateStart = dateStart.addingTimeInterval(TimeInterval(604800))
并通过这些interwal生成通知
//MARK:- Schedule Notification with Daily bases.
func scheduleNotification(at date: Date, body: String, titles:String) {
let triggerDaily = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
if #available(iOS 10.0, *) {
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let content = UNMutableNotificationContent()
content.title = titles
content.body = body
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "HardikBar"
let request = UNNotificationRequest(identifier: "NotificationAt-\(date))", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
} else {
// Fallback on earlier versions
}
}
关于ios - 使用本地通知 api 在 swift 3 中触发每周通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51054772/