ios - 本地通知立即触发,而不是按计划触发

标签 ios swift uilocalnotification

我一直在尝试按工作日安排本地通知,但它们似乎会立即触发,而不是在我安排它们时触发。这是我在 didFinishLaunchingWithOptions 中的 AppDelegate 中的代码:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
        var notification = UILocalNotification()
        var components = NSDateComponents()
        var calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
        components.weekday = 5
        components.hour = 9
        components.minute = 24
        notification.alertBody = "Notification test"
        notification.fireDate = calendar?.dateFromComponents(components)
        UIApplication.sharedApplication().scheduleLocalNotification(notification)

关于为什么它可能不起作用的任何线索?

最佳答案

正如其他人提到的,确保指定 components 对象的 yearmonth 属性。

components.year = 2016
components.month = 6

您的代码的部分当前触发日期如下:

0001-01-01

这意味着通知将在第“1”年的一月份触发,而不是在 2016 年。

关于ios - 本地通知立即触发,而不是按计划触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586599/

相关文章:

ios - 停止重复本地通知 iOS

swift - 无法从 didReceiveLocalNotification 重新加载 UITable

iphone - 如何在警报 View 中隐藏关闭按钮和关闭操作以仅显示 "Open app"按钮

ios - storekit 框架无法在 ios 模拟器 5.1 中工作

ios - UIImage 和 CGI​​mage 之间的转换是一项繁重的操作

ios - 在 UITableView 上执行具有多个状态的 segue

iOS Swift MapKit 自定义注解

ios - 当作为 Objective-C block 调用时,Swift 闭包崩溃

ios - CocoaPods - 无法找到 `GoogleMaps` 的规范

ios - 具有透明背景的模态 UISplitViewController