UIApplication.sharedApplication.scheduledLocalNotifications 始终为空

UIApplication.sharedApplication.scheduledLocalNotifications is always empty

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

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

通知正确触发。

添加通知到数组

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

感谢您的帮助。

看来检查 UIApplication 的 scheduledLocalNotifications 数组是相当不可靠的。

大多数人似乎都建议只保留您自己的列表,并查询它。

首先,该数组将仅包含安排在当前日期之后的通知,因此不会添加过去已注册或已触发的任何通知。

对我来说,这不是问题,我注册的通知肯定是将来的,但仍然没有出现在列表中。我能找到的最佳答案是:

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)

看来我不是第一个卡在上面的.. 但似乎答案比我想的要近得多。

Cmd + LPM

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

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