首先,我想说我一直在寻找我的问题的答案,但到目前为止我发现的唯一东西是旧版本 Swift 的答案或没有专门回答我的问题的答案。
背景信息:
我正在尝试开发一个可以在设定的时间间隔内提醒您的应用程序。现在这可行,因为您只设置了 1 个提醒。但是,如果我将间隔设置为 20 秒,则启动应用程序,设置 2 个通知并关闭应用程序,仅在 20 秒内显示第二个通知。第一个通知被第二个覆盖。
问题:如何确保用户请求的所有通知都实际发送,并且没有通知覆盖前一个通知?
通知代码:
let tijd = 20 //20 is just for the test, normally there is more code to it
// Notification
let content = UNMutableNotificationContent()
content.title = "Notification title"//title
content.body = "Notification body" //body
content.badge = 1
content.sound = UNNotificationSound.default()
// Timer
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(tijd), repeats: false)
let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)