创建在特定时间触发的本地通知
Creating a local notification to fire at a specific time
我需要我的应用程序存储一些应该在特定时间启动的 local notifications
。
我的申请很简单。用户可以使用 UIDatePicker
预订时间。通知将在预订时间前 2 分钟触发。
现在我正在尝试手动插入通知应触发的时间。我正在使用此代码:
let notification = UILocalNotification()
self.timePicked = " 22:38:00 +0000"
timeForLocalPush = defaults.objectForKey("timeForLocalPush") as! String
timeForLocalPush = timeForLocalPush + self.timePicked
print(timeForLocalPush) // this prints for this example today, 2016-08-30 22:38:00 +0000
dateFormatter.dateFormat = "YYY-MM-dd"
notification.fireDate = dateFormatter.dateFromString(timeForLocalPush)
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = self.nameField.text! + " Har bokat tid nu!"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
发生的事情是在预订时间后立即触发通知。所需的操作是这样的:
当前时间:14:17
本次客户书籍:15:00
本地通知已设置为触发:14:58
如果您需要任何其他信息,请告诉我。谢谢
初步检查你的日期格式是错误的dateFormatter.dateFormat = "YYY-MM-dd"
,是dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
例如
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message "
localNotification1.timeZone = NSCalendar.currentCalendar().timeZone
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
localNotification1.fireDate = formatter.dateFromString("2016-08-30 22:38:00")
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
请正确设置日期格式:
// this prints for this example today, 2016-08-30 22:38:00 +0000
//Your date format decalre: dateFormatter.dateFormat = "YYY-MM-dd"
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
我需要我的应用程序存储一些应该在特定时间启动的 local notifications
。
我的申请很简单。用户可以使用 UIDatePicker
预订时间。通知将在预订时间前 2 分钟触发。
现在我正在尝试手动插入通知应触发的时间。我正在使用此代码:
let notification = UILocalNotification()
self.timePicked = " 22:38:00 +0000"
timeForLocalPush = defaults.objectForKey("timeForLocalPush") as! String
timeForLocalPush = timeForLocalPush + self.timePicked
print(timeForLocalPush) // this prints for this example today, 2016-08-30 22:38:00 +0000
dateFormatter.dateFormat = "YYY-MM-dd"
notification.fireDate = dateFormatter.dateFromString(timeForLocalPush)
notification.timeZone = NSCalendar.currentCalendar().timeZone
notification.alertBody = self.nameField.text! + " Har bokat tid nu!"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
发生的事情是在预订时间后立即触发通知。所需的操作是这样的:
当前时间:14:17
本次客户书籍:15:00
本地通知已设置为触发:14:58
如果您需要任何其他信息,请告诉我。谢谢
初步检查你的日期格式是错误的dateFormatter.dateFormat = "YYY-MM-dd"
,是dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
例如
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message "
localNotification1.timeZone = NSCalendar.currentCalendar().timeZone
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
localNotification1.fireDate = formatter.dateFromString("2016-08-30 22:38:00")
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
请正确设置日期格式:
// this prints for this example today, 2016-08-30 22:38:00 +0000
//Your date format decalre: dateFormatter.dateFormat = "YYY-MM-dd"
dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"