微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

设备重新启动后,计划通知会发生什么情况?

如何解决设备重新启动后,计划通知会发生什么情况?

我试图找到这个问题的确切答案:“设备重启后我的预定通知会发生什么?”。

我已使用 UNUserNotificationCenter 安排所有通知,它们将根据安排的时间每天重复触发。

这是我编写的代码片段,它在设备开启时工作。

func scheduleNotification() {
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = "This is the title"
    content.body = "The is the body"
    content.categoryIdentifier = "identifier"
    content.userInfo = ["info":"B"]
    content.sound = UNNotificationSound.default
    
    var dateComponents = DateComponents()
    dateComponents.hour = 0
    dateComponents.minute = 29
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,repeats: true)
    let request = UNNotificationRequest(identifier: UUID().uuidString,content: content,trigger: trigger)
    center.removeAllPendingNotificationRequests()
    center.add(request)
}

解决方法

简而言之,它应该保留,虽然我找不到官方的苹果文档,但它对我有用。计时器应该在重新启动后继续存在。只要在相关时间到达时设备处于开启状态并且应用程序仍处于安装状态,通知就会被触发。当然发送通知需要权限。

这个问题之前有人问过,没有答案 - UNUserNotificationCenter notifications after device reboot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。