Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios10

swift2

I am trying to schedule series of notifications in the for loop. After scheduling I am reading all pending notifications (just for testing purpose).

Scheduling (called from for loop):

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)
            })
        }
}

Reading:

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" in the latter method is UNNotificationRequest array, but the count of this array returns always 1, and the scheduled event - is only my last event of the list that I'm feeding to the schedule method.

During scheduling I am also printing all events, that are scheduled and the interval (to be sure it's in the future), and I do get proper log of all events I want to schedule, but somehow it looks like each next event overwrites the previous one. What am I doing wrong?

like image 507
Async- Avatar asked Aug 11 '16 12:08

Async-


1 Answers

The problem was not actually the UNUserNotificationCenter, but the identifier of the request, which in my code stayed the same throughout the iterations in the for loop. When I added unique id to it, it worked.

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

As always, problem was much easier than it seemed.

like image 186
Async- Avatar answered Nov 01 '22 18:11

Async-