简体   繁体   中英

Local notifications not working (swift)

I am looking to fire local notifications. I have tried to create this and I was successful, there were no errors but when I run the app in the simulator local notifications don't execute

code in app delegate

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Badge | .Sound, categories: nil))

  // play default sound
    return true
}

and the view controller

class TechByteSchedulingViewController:UIViewController {
@IBOutlet weak var datePicker: UIDatePicker!

@IBAction func DateChosen(sender: UIButton) {
    func sendNotification(sender: UIButton) {
        var localNotification = UILocalNotification()
        localNotification.fireDate = datePicker.date
        localNotification.repeatInterval = .CalendarUnitDay
        localNotification.alertBody = "check out your daily byte"
        localNotification.alertAction = "Open"
        localNotification.timeZone = NSTimeZone.defaultTimeZone()
        localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
        localNotification.soundName = UILocalNotificationDefaultSoundName
        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

        func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
            application.applicationIconBadgeNumber = 0

    }
        self.navigationController?.popToRootViewControllerAnimated(true)
            }

}
override func viewDidDisappear(animated: Bool) {


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

Base on what I see you implement some function inside of the ButtonAction function which is wrong... you should implement the sendNotfication function outside of ButtonAction then called it inside of ButtonAction

class TechByteSchedulingViewController:UIViewController  {

   @IBOutlet weak var datePicker: UIDatePicker!

  @IBAction func DateChosen(sender: UIButton) {
    self.sendNotification()
  }

    func sendNotification() {
    var localNotification = UILocalNotification()
    localNotification.fireDate = datePicker.date
    localNotification.repeatInterval = .CalendarUnitDay
    localNotification.alertBody = "check out your daily byte"
    localNotification.alertAction = "Open"
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    localNotification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
 }

    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        application.applicationIconBadgeNumber = 0
    self.navigationController?.popToRootViewControllerAnimated(true)
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM