3

Even if I add a new local notification right before, the attribute is empty. I found a lot of post (and just one on stack overflow) - but nobody has solved this problem.

My useless is, that I want to delete a local notification. That's why I want to iterate over the array and compare the hash value of my notification to delete and the current iterator object.

The notification fires correctly.

Add notification to the array

UIApplication.sharedApplication().scheduleLocalNotification(newNotification)

Read the array

for notification in application.scheduledLocalNotifications {
    if notification.hashValue == hashValue {
        application.cancelLocalNotification(notification as! UILocalNotification)
        NSLog("Unsheduled local notification for \(notification.alertBody!)")
    }
}

Thanks for your help.

Community
  • 1
  • 1
Tobonaut
  • 2,245
  • 2
  • 26
  • 39

2 Answers2

1

It seems that checking the UIApplication's scheduledLocalNotifications array is fairly unreliable.

Most people seem to recommend just keeping your own list as well, and querying that.

Firstly, that array will only contain notifications that are scheduled after the current date, so any that have been registered that are in the past or any that have already fired, will not be added.

For me, that was not the problem, the notificatiosn I was registering were definitely in the future, but still didn't appear in the list. The best answer I could find is:

Having similar issues right now. My guess here is that iOS does not schedule the notifications immediately but only at the end of the current run loop. I am running into these problems when setting the scheduledLocalNotifications property several times in the same run loop and changes don't seem to be updated accordingly. I think I will just keep a copy of the local notifications array myself and only set scheduledLocalNotifications and never read it.

(source)

Community
  • 1
  • 1
yothsoggoth
  • 393
  • 3
  • 10
1

Seems I have not been first to be stucked on it.. But seems answer is much closer than I tought.

Cmd + LPM

public var scheduledLocalNotifications: [UILocalNotification]? // setter added in iOS 4.2

The property is just form of setter. And probably was never intended to get scheduled notifications

Silvertaurus
  • 384
  • 3
  • 5