UIApplication.sharedApplication.scheduledLocalNotifications始终为空

时间:2015-03-11 11:45:58

标签: ios swift uilocalnotification uiapplication

即使我之前添加了新的本地通知,该属性也是空的。我发现了很多帖子(只有one on stack overflow) - 但没有人解决这个问题。

我没用,我想要删除本地通知。这就是为什么我想迭代数组并将我的通知的哈希值与delete和当前的迭代器对象进行比较。

通知正确触发。

向阵列添加通知

UIApplication.sharedApplication().scheduleLocalNotification(newNotification)

阅读数组

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

感谢您的帮助。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

似乎我没有第一次被困在上面.. 但似乎答案比我想要的更接近。

Cmd + LPM

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

该属性只是 setter 的形式。并且可能从未打算获得预定通知