3

首先,我想说我一直在寻找我的问题的答案,但到目前为止我发现的唯一东西是旧版本 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)

此代码存储在 UITableView 单元格中。 我的故事板

4

1 回答 1

4

好吧,我想通了!
https://stackoverflow.com/a/41892828/7385440
这个答案导致了我遇到的同样问题。我必须为每个通知设置不同的标识符!所以我现在的代码是:

let request = UNNotificationRequest(identifier: bezigheid, content: content, trigger: trigger)

并且bezigheid在每个细胞中都是不同的。经过测试,现在我收到了 2 个不同的通知!

于 2017-04-13T11:58:23.930 回答