【发布时间】:2021-12-29 00:14:59
【问题描述】:
我正在尝试安排本地通知在每天的特定时间触发,但从明天开始。
例如"从明天开始,每天下午 2 点触发通知"
这就是我设置日程功能的方式。
func scheduleNotifications(date: Date, identfier: String, after: Bool) {
let content = UNMutableNotificationContent()
content.title = "App"
content.body = "Test."
content.sound = .default
content.userInfo = ["Hour": Int(hourFormatter.string(from: date)) ?? 0]
let afterDay = Calendar.current.date(byAdding: .day, value: after ? 1 : 0, to: Date())
var components = Calendar.current.dateComponents([.hour, .minute], from: afterDay!)
components.hour = Int(hourFormatter.string(from: date)) ?? 0
components.minute = Int(minuteFormatter.string(from: date)) ?? 0
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
let request = UNNotificationRequest(identifier: identfier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
【问题讨论】:
-
请不要删除您的original question 以便下次创建新的,而是对其进行编辑以解决导致否决/关闭投票的问题。通常在此之后,downvotes 将被退回。更多信息可以找到here
标签: swift notifications usernotifications