创建在特定时间触发的本地通知

编程入门 行业动态 更新时间:2024-10-27 00:26:08
本文介绍了创建在特定时间触发的本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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)

更多推荐

创建在特定时间触发的本地通知

本文发布于:2023-11-27 06:26:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636962.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   时间   在特定

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!