gpt4 book ai didi

ios - 使用本地通知 api 在 swift 3 中触发每周通知

转载 作者:行者123 更新时间:2023-11-28 10:40:05 26 4
gpt4 key购买 nike

我想触发每周一晚上 7:00 发出的每周通知。

我看到这样的代码:

let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
  1. 我应该将开始日期设置为什么?
  2. 此外,日期是否需要在现在?

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com