创建在特定时间触发的本地通知 [英] Creating a local notification to fire at a specific time

查看:65
本文介绍了创建在特定时间触发的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序存储一些应该在特定时间启动的本地通知.

I need my application to store some local notifications that should be launched on a specific time.

我的申请很简单.用户可以使用 UIDatePicker 预订时间.通知将在预订时间前 2 分钟发出.

My application is simple. The user can book times using a UIDatePicker. The notification is to be fired 2 minutes before that booked time.

现在我正在尝试手动插入应该触发通知的时间.我正在使用此代码:

For now i am trying to manually insert a time that the notification should be fired at. I am using this code:

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)

发生的情况是在预订时间后立即触发通知.想要的动作是这样的:

What happens is that the notification is fired instantly after booking the time. Desired action is this:

当前时间:14:17

客户预定时间:15:00

Client books for this time: 15:00

本地通知设置为触发:14:58

Local notification is set to be fired: 14:58

如果您需要任何其他信息,请告诉我.谢谢

If you need any additional information just tell me. Thank you

推荐答案

最初检查你的日期格式是错误的 dateFormatter.dateFormat = "YYY-MM-dd", it is in dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

initially check your date format it is in wrong dateFormatter.dateFormat = "YYY-MM-dd", it is in 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)

这篇关于创建在特定时间触发的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆