【问题标题】:iOS 13 local notification not sendingiOS 13本地通知未发送
【发布时间】:2019-10-15 20:24:33
【问题描述】:

我每天早上 8 点设置了一个重复发生的本地通知。这在每个 iOS 中都运行良好,除了当我更新到 iOS 13+ 时,它不再发送我的本地通知。那部分没有代码更改。我见过有人在 iOS 13 上遇到推送通知问题,但这是本地的,所以我不知道为什么它突然停止在 iOS 13 上工作。我尝试使用 xcode 10 和 11。

如果有人需要发布代码,我可以得到,但我很好奇是否有其他人在本地通知和 iOS 13 上遇到过这个问题。

【问题讨论】:

  • 用于推送通知。我正在使用设备令牌或任何推送通知。我只是在本地通知方面遇到问题。
  • 我对 React Native 推送通知模块有同样的问题。计划通知已停止工作。还没有找到任何解决方案。相同的代码在 iOS 12 及更早版本上运行良好。
  • 有人弄清楚了吗?

标签: uilocalnotification ios13


【解决方案1】:

我想我明白了!

至少对我来说,使用UNCalendarNotificationTrigger 设置触发器不起作用。但是,如果我改用UNTimeIntervalNotificationTrigger,效果很好!

这是我的代码:

let now = Date(timeIntervalSinceNow: 0)

// If event has already occured, silently fail.
if (timelineEvent.event.time < now) {
    return
}

// Otherwise, let's calculate the time until the next event
let interval = timelineEvent.event.time.timeIntervalSince(Date(timeIntervalSinceNow: 0))
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)

如果您严格针对 iOS 13 及更高版本,可以稍微简化日期比较:

let now = Date(timeIntervalSinceNow: 0)

// If event has already occured, silently fail.
if (timelineEvent.event.time < now) {
    return
}

// Otherwise, let's calculate the time until the next event
let interval = now.distance(to: timelineEvent.event.time)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)

奇怪的是,Date().distance(to: Date) 似乎仅适用于 iOS 13+,但文档显示它自 iOS 7.0 起可用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多