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

问题描述

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

我已使用 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...