【问题标题】:Have UNNotification trigger across timezones跨时区触发 UNNotification
【发布时间】:2017-09-24 19:59:27
【问题描述】:

我无法跨时区正确触发 iOS 10 本地通知 (UNNotification)。

在 +10 小时时区,我希望它在 17:00:00 触发,并安排它。

稍后当我查看安排了哪些通知时,我会得到它的下一个触发日期,仍然是时区 +10 小时,它会说 17:00:00。

但是当我在设备上将时区更改为 +8 小时,并查看安排了哪些通知时,我得到了它的下一个触发日期,它仍然显示 17:00:00 .我原以为它会提前两个小时在 15:00:00 发射。

我怎样才能让它像这样工作?我尝试更改用于创建提供给 UNCalendarNotificationTrigger 的日期组件的日历的时区(尝试过的事情是:TimeZone.init(identifier: "Somewhere")TimeZone.autoupdatingCurrent,可能还有其他)。但它不会改变,AFAIK 我总是得到完全相同的开火日期,即使我改变时区。

【问题讨论】:

  • 分享一些代码?
  • 代码在这里没有帮助,因为 UNMutableNotificationContentUNCalendarNotificationTrigger 不接受时区。它接收日期组件,例如hourminute,然后在本地时间触发它。这是以前 iOS 版本的新行为,在 UILocalNotification 上有一个 timeZone 字段。
  • 触发器接收到的日期组件可以包含时区组件Calendar.current.dateComponents([.day,.month,.year,.hour,.minute,.timeZone], from: fireDate),但 UserNotifications 框架似乎忽略了时区。
  • 老实说我放弃了。也许没有解决方案......感觉就像是在浪费时间寻找它。不,这行不通...我还有更多其他工作要做:-P 不是说这是不可能的。或者至少这应该是 Radar 上的功能请求。
  • Jonny,您在哪里可以找到解决方案?我现在遇到了完全相同的问题。

标签: ios timezone unnotificationrequest unnotificationtrigger


【解决方案1】:

UNTimeIntervalNotificationTrigger可以解决这个问题。

您可以计算您设置的日期与当前日期之间的差异,然后安排时间间隔触发。

func timeIntervalTrigger(from date: Date, repeats: Bool) -> UNTimeIntervalNotificationTrigger {
    let timeInterval = date.timeIntervalSinceNow
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: repeats)
    return trigger
}

【讨论】:

  • 这确实解决了问题!遗憾的是,UNCalendarNotificationTrigger 有很多错误,这是解决它的好方法。在我看来,这应该是公认的答案。
猜你喜欢
  • 2017-05-27
  • 2019-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2010-10-06
相关资源
最近更新 更多