【问题标题】:Running local notifications with hourly interval every day for specific amount of time在特定的时间段内每天以每小时间隔运行本地通知
【发布时间】: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


【解决方案1】:

如果将本地推送通知安排 N 次(您想要多少次),我想到了第一个解决方案。当然,您只需要使 identifier 独一无二。


到目前为止,我只提出了解决方案 - 制作两种不同的方法 使用定时器,每天启动和停止通知功能, 并为应用启用后台模式。

imo 不建议这样做,如果你推动这个,你可能需要与苹果审查团队打交道。

【讨论】:

  • 谢谢@Glenn。 “...本地推送通知 N 次...” - 是的,我也是这么想的。
  • 也许考虑选择一个答案或发布您自己的答案? :) 但是,当然,不客气。编码愉快。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 2023-01-09
  • 2019-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
相关资源
最近更新 更多