UNUserNotificationCenter - 在 for 循环中仅安排一个事件,swift 2.3,iOS10,

UNUserNotificationCenter - schedules only one event in the for loop, swift 2.3, iOS10,

我正在尝试在 for 循环中安排一系列通知。安排后我正在阅读所有待处理的通知(仅用于测试目的)。

调度(从 for 循环中调用):

func scheduleNotification (event : Meeting, todaysBadgeCounter: Int) {
 if #available(iOS 10.0, *) {

        let minutesBefore = CallIn.Settings.notifyNumberOfMinutesBeforeEvent
        let notifyBefore = UNNotificationAction(identifier: NotificationActions.NotifyBefore.rawValue, title: "Notification", options: [])
        let category = UNNotificationCategory(identifier: "CALLINNOTIFICATION", actions: [notifyBefore], intentIdentifiers: [], options: [])


        UNUserNotificationCenter.currentNotificationCenter().setNotificationCategories([category])
        let content = UNMutableNotificationContent()

        content.title = NSString.localizedUserNotificationStringForKey(event.title, arguments: nil)
        if(minutesBefore <= 1){
            content.body = NSString.localizedUserNotificationStringForKey("Your \(event.title) is about to start", arguments: nil)
        }else{
            content.body = NSString.localizedUserNotificationStringForKey("You have \(event.title) in \(Int(minutesBefore)) minutes", arguments: nil)

        }
        content.sound = UNNotificationSound.defaultSound()

        //interval in seconds from current point in time to notification
        let interval : NSTimeInterval = NSTimeInterval(secondsFromNowTo(event.startTime.dateByAddingTimeInterval(-minutesBefore * 60)))


        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
        let request = UNNotificationRequest.init(identifier: "sampleRequest", content: content, trigger: trigger)

        //only schedule in the future
        if(interval > 0){
            UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: { (error) in
                // handle the error if needed
                log.error(error?.localizedDescription)
                print("SCHEDULING >=iOS10:", event.title, ", interval:", interval)
            })
        }
}

正在阅读:

func printScheduledNotifications() {
    if #available(iOS 10.0, *) {
        print("printing scheduled notifications >=iOS10")
        UNUserNotificationCenter.currentNotificationCenter().getPendingNotificationRequestsWithCompletionHandler({ (notifications) in
            print("count", notifications.count)
            for notification in notifications{

                print(notification.description)
            }
        })
    }
后一种方法中的

"notifications" 是 UNNotificationRequest 数组,但此数组的计数 returns 始终为 1,并且计划的事件 - 只是我正在提供的列表中的最后一个事件到调度方法。

在安排期间,我还打印了所有已安排的事件和间隔(以确保它在未来),并且我确实获得了我想要安排的所有事件的正确日志,但不知何故它看起来像每个下一个事件会覆盖上一个。我究竟做错了什么?

问题实际上不是 UNUserNotificationCenter,而是请求的标识符,在我的代码中,它在 for 循环的整个迭代中保持不变。当我向它添加唯一 ID 时,它起作用了。

let identifier = NSString.localizedUserNotificationStringForKey("sampleRequest\(event.title)", arguments: nil)

一如既往,问题比看起来容易得多。