在swift 3之前或之前2天设置本地通知

时间:2017-08-10 05:46:08

标签: swift3 calendar localnotification

我想在我的应用中添加本地通知。我在textfields填写信息,并在为考试选择的特定日期之前设置datetime and reminder。任何人都可以实施这样的演示,请告诉我该怎么做。

1 个答案:

答案 0 :(得分:1)

答案基于我所理解的内容,请根据您的要求更改时间和提醒字符串。

 func scheduleNotification(InputUser:String) {

let now: NSDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: NSDate())

let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let date = cal.dateBySettingHour(now.hour, minute: now.minute + 1, second: 0, ofDate: NSDate(), options: NSCalendarOptions())
let reminder = UILocalNotification()
reminder.fireDate = date
reminder.alertBody = InputUser
reminder.alertAction = "Cool"
reminder.soundName = "sound.aif"
reminder.repeatInterval = NSCalendarUnit.Minute




UIApplication.sharedApplication().scheduleLocalNotification(reminder)

print("Firing at \(now.hour):\(now.minute+1)")

}