繁体   English   中英

本地通知立即触发,而不是在计划时触发

[英]Local notifications firing immediately instead of when they are scheduled

我一直试图在工作日安排本地通知,但它们似乎是立即开始而不是在我安排它们时。 这是我在didFinishLaunchingWithOption中的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月份的“1”年开始,而不是在2016年。

你需要使用notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

尝试设置年/月,因为它是从1970年1月1日创建的,所以日期是过去的=>立即触发

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM