Ios 本地通知将立即触发,而不是按计划触发
我一直试图在工作日之前安排本地通知,但它们似乎是立即启动的,而不是在我安排它们的时候。这是我在AppDelegate中的代码,位于Ios 本地通知将立即触发,而不是按计划触发,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,我一直试图在工作日之前安排本地通知,但它们似乎是立即启动的,而不是在我安排它们的时候。这是我在AppDelegate中的代码,位于didfishlaunchwithoptions: application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil)) var notification = UILocalNotification()
didfishlaunchwithoption
s:
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)
有没有关于它为什么不起作用的线索?请尝试设置年/月,因为它是从1970年1月1日创建的,所以日期是过去的=>立即触发的正如其他人提到的,请确保指定
组件对象的年和月属性
components.year = 2016
components.month = 6
您代码的当前火灾日期部分如下:
0001-01-01
这意味着通知将在1月份的“1”年发出,而不是在2016年。您需要使用notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5]代码>打印您的点火日期,您就会看到。好吧,打印组件会给我一个正确的结果(我定义的结果),但点火日期会给我0001-01-01 10:00:45+0000。dateFromComponents是否不像我想的那样工作?没错。它只是将给定的组件设置为空日期。您还想设置日期
而不是工作日
。哦,我明白了。我设置了工作日,因为我希望它在一周中的某一天启动,在本例中是星期四。这不是正确的方法吗?不,读这个