【发布时间】:2020-05-04 09:35:49
【问题描述】:
如何使我的应用能够以可自定义的间隔每天触发本地通知,但仅限于分配的时间,例如:从 10:00 到 20:00 pm?
现在我只实现了自定义间隔的重复通知:
func beginNotifications(_ config: UserConfig) {
let interval = config.notificationInterval
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
let content = UNMutableNotificationContent()
content.title = "Content title"
content.subtitle = "Content body"
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
center.add(request)
}
到目前为止,我只提出了解决方案 - 使用 Timer 制作两个单独的方法,每天启动和停止通知功能,并为应用启用后台模式。
【问题讨论】:
-
没有简单的解决方案,但我认为最好的办法是使用UNCalendarNotificationTrigger 而不是 UNTimeIntervalNotificationTrigger。每次您希望通知触发时,您都需要创建一个请求。
标签: ios swift uilocalnotification localnotification