scheduleLocalNotification 在 Swift 2.0 中不起作用

scheduleLocalNotification is not working in Swift 2.0

我是 iOS 的新手 technology.I 正在尝试使用 UILocalNotification 使用 swift 2.0 构建闹钟应用程序。当我单击按钮时,我从 UIPickerView 获取时间和日期。我也想显示通知和播放声音。 但是声音和通知都不起作用。请帮助我!

@IBOutlet weak var myPickerView: UIDatePicker!

override func viewDidLoad() {
    super.viewDidLoad()
    myPickerView.date = NSDate()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func setAlarmClicked(sender: UIButton) {
    let dateFormater : NSDateFormatter = NSDateFormatter()
    dateFormater.timeZone = NSTimeZone.defaultTimeZone()
    dateFormater.timeStyle = .ShortStyle
    dateFormater.dateStyle = .ShortStyle

    let dateTimeString : NSString = dateFormater.stringFromDate(myPickerView.date)
    print("date is \(dateTimeString)")

    self.sheduleLocalNotificationWithDate(myPickerView.date)
}

@IBAction func cancelAlarmClicked(sender: UIButton) {

}

func sheduleLocalNotificationWithDate(fireDate : NSDate){
    let localNotification : UILocalNotification = UILocalNotification()
    localNotification.fireDate = fireDate
    localNotification.alertBody = "Alert !!!!"
    localNotification.soundName = "minion.mp3"

    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

您必须注册本地通知。将此添加到您的视图控制器:

override func viewDidAppear() {

    super.viewDidAppear()
    let settings = UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)

}

当您的视图第一次出现时,会出现一个警告,点击 "OK",您的通知应该会起作用。请记住,用户始终可以禁用通知。