【发布时间】:2021-04-20 18:52:27
【问题描述】:
我们的应用程序每天早上 8 点发送通知,向用户打招呼并提醒他完成任务。这是我们正在使用的脚本:
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: ["dailyReminder"])
let content = UNMutableNotificationContent()
content.title = "Good morning"
content.body = notificationMessages[randomIndex]
content.sound = .default
var dateComponents = DateComponents()
dateComponents.hour = 8
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let request = UNNotificationRequest(identifier: "dailyReminder", content: content, trigger: trigger)
center.add(request)
这是在用户完成当天的任务后调用的,以便他们在第二天得到提示。但是,有些人会在早上 8 点之前完成任务。在这种情况下,我们希望在第二天早上 8 点发送通知。
我如何计算脚本是在 8 点之前还是之后运行并相应地添加 24 小时?
【问题讨论】: