【问题标题】:How to set alerts in UNNotificationCenter some days before?几天前如何在 UNNotificationCenter 中设置警报?
【发布时间】:2020-04-10 00:17:57
【问题描述】:

如何在几天前触发本地通知?例如,我从 dateComponents 创建本地通知:

    var dateComponents = Calendar.current.dateComponents([.month, .day], from: birthDate)
    dateComponents.hour = 10
    dateComponents.minute = 30
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

我从以下日期获得天数:

var dateComponents = Calendar.current.dateComponents([.month, .day], from: birthDate).

但是我怎样才能将它们设置为在特定时间的某一天之前触发呢?

【问题讨论】:

  • 我推荐阅读this
  • 谢谢,但我还没有找到解决方案。此线程仅关于触发一周中的特定日期。
  • 您的问题不清楚。请具体说明哪些情况被理解为“几天前”?什么样的日子?
  • 我用这些信息更新了我的问题。

标签: ios swift usernotifications


【解决方案1】:
if let oneDayAgo = Calendar.current.date(byAdding: .day, value: -1, to: birthDate) {
    var dateComponents = Calendar.current.dateComponents([.month, .day], from: oneDayAgo)
    dateComponents.hour = 10
    dateComponents.minute = 30
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
}

【讨论】:

  • 感谢您的解决方案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 1970-01-01
  • 2022-09-26
相关资源
最近更新 更多