特定时间的本地通知 [英] Local Notifications at a specific time
问题描述
我有一个应用程序,我需要在特定日期和时间收到通知.我正在使用以下代码来设置日期和时间.
I have an app and I need to have the notifications at a specific date and time. I am using the following code to set the date and time.
let app = UIApplication.sharedApplication()
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil)
app.registerUserNotificationSettings(notificationSettings)
let calendar = NSCalendar.currentCalendar()
let date = NSDateComponents()
date.hour = 5
date.minute = 0
date.month = 5
date.day = 21
date.year = 2016
date.timeZone = NSTimeZone.systemTimeZone()
let alarm = UILocalNotification()
alarm.fireDate = calendar.dateFromComponents(date)
alarm.timeZone = NSTimeZone.defaultTimeZone()
alarm.alertTitle = ""
alarm.alertBody = ""
alarm.soundName = "Sound.wav"
app.scheduleLocalNotification(alarm)
我已允许通知,并且我可以在用户离开应用后 10 秒安排通知,但不是在特定时间.
I have allowed notifications and I can schedule ten seconds after the user leaves the app but not for a specific time.
推荐答案
你的代码是正确的,除了一件事,小时.NSDate 适用于 24 小时制.因此,如果您希望在下午 5 点收到通知,则需要将小时值设置为 17
You code is correct except for one thing, the hour. NSDate works on 24-hour system. So if you want the notification to come on 5 PM you need to set the hour value to 17
date.hour = 5 //this means 05:00 AM
date.minute = 0
date.hour = 17 //this mean 05:00 PM
date.minute = 0
附言- 您的 alertBody
和 alertTitle
为空白.您可能希望将其设置为某些内容以便显示.
P.S. - Your alertBody
and alertTitle
are blank. You might want to set it to something so that it shows up.
希望这会有所帮助.:)
Hope this helps. :)
这篇关于特定时间的本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!