iOS本地通知队列如何为UNTimeIntervalNotificationTrigger工作?

问题描述 投票:0回答:1

如果我创建了一个简单的应用程序,我希望每分钟发送一个通知,连续4分钟;在最初的5秒延迟之后。

当我调用下面的scheduleManyNotes()时,我打印出待发送的通知,但只看到1个。是什么原因导致这些通知被归为1?

func scheduleManyNotes() {

    for x in 0...4 {
        scheduleNote("note \(x)", (x * 60) + 5)
    }

    notificationCenter.getPendingNotificationRequests(completionHandler:{reqs in
        for request in reqs {
            print(request)
        }
    })
}

func scheduleNote(_ msg: String, _ delaySec: Int) {
    let content = UNMutableNotificationContent()
    content.sound = UNNotificationSound.default
    content.body = msg
    content.badge = NSNumber(integerLiteral: delaySec)
    content.categoryIdentifier = msg
    let trigger = delaySec == 0 ? nil : UNTimeIntervalNotificationTrigger(timeInterval: Double(delaySec), repeats: false)
    let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)

    NSLog("Scheduling Request \(msg)")

    notificationCenter.add(request) { (error) in
        if let error = error {
            NSLog("Error \(error.localizedDescription)")
        }
    }
}
ios swift iphone uilocalnotification ios13
1个回答
0
投票

问题是,我对所有的TimeInterval通知都使用了相同的标识符。我将标识符改为每个请求都是唯一的,这样我就有了5个唯一的请求。

// Original

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


// Modfified

    let uid = UUID.init().uuidString
    print("hopefully unique uuid:\(uid)")
    let request = UNNotificationRequest(identifier: uid, content: content, trigger: trigger)
© www.soinside.com 2019 - 2024. All rights reserved.