Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 特定时间的本地通知_Ios_Swift_Uilocalnotification - Fatal编程技术网

Ios 特定时间的本地通知

Ios 特定时间的本地通知,ios,swift,uilocalnotification,Ios,Swift,Uilocalnotification,我有一个应用程序,我需要在特定的日期和时间通知。我使用以下代码设置日期和时间 let app = UIApplication.sharedApplication() let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Sound], categories: nil) app.registerUserNotificationSettings(notificationSettings) let cal

我有一个应用程序,我需要在特定的日期和时间通知。我使用以下代码设置日期和时间

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秒,但不能安排特定的时间。

如下所示设置您的日期

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /*find out and place date format from http://userguide.icu-project.org/formatparse/datetime*/
let customDate = dateFormatter.dateFromString(/*your_date_string*/)

// your code
 alarm.fireDate = customDate
您的\日期\字符串将是您希望安排通知的日期和时间


希望这有帮助

你的代码是正确的,除了一件事,时间。NSDate在24小时系统上工作。因此,如果希望通知在下午5点发出,则需要将小时值设置为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
为空。您可能希望将其设置为某个值,以便它显示出来


希望这有帮助。:)

示例代码中的日期是过去的(今天上午5点)。所以通知使用军事时间?