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

Asy*_*nc- 5 swift2 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)
            })
        }
}
Run Code Online (Sandbox Code Playgroud)

读:

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)
            }
        })
    }
Run Code Online (Sandbox Code Playgroud)

后一种方法中的"通知"是UNNotificationRequest数组,但是这个数组的计数总是返回1,而预定的事件 - 只是我为调度方法提供的列表的最后一个事件.

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

Asy*_*nc- 6

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

let identifier = NSString.localizedUserNotificationStringForKey("sampleRequest\(event.title)", arguments: nil)
Run Code Online (Sandbox Code Playgroud)

一如往常,问题比看起来容易得多.