5

我一直在网上搜索,看看您安排的通知是否在设备重启后被删除。我发现了不同的意见,所以我开始测试它。这是我注意到的:

  • 我已经安排了 10 分钟后的通知,重新启动了我的手机:没有任何反应
  • 但是,昨天我安排了很多通知,其中一些是针对当天的。即使从昨天开始我已经多次重启手机,这些通知还是会出现。

下面是我用来进行调度的一段代码:

let notificationContent = UNMutableNotificationContent()
notificationContent.body = "This is a notification"
notificationContent.categoryIdentifier = NotificationContentProvider.snoozeCategoryIdentifier
let unitFlags = [.hour, .minute, .second, .day, .month, .year] as [Calendar.Component]
let components = Calendar.current.dateComponents(Set(unitFlags), from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)
let request = UNNotificationRequest(identifier: "\(Int.random())", content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
    if let theError = error {
        print(theError.localizedDescription)
    }
})
4

0 回答 0