0

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.
}
iRiziya
  • 3,235
  • 1
  • 22
  • 36
  • 1
    i don't see the code for the LocalNotification – Lamour Jul 29 '15 at 15:10
  • @Lamar here is the code – Nikhil C Kanamarla Jul 29 '15 at 15:13
  • Refer this simple [example](https://www.topcoder.com/blog/notifications-in-ios-8-using-swift/) or this [one](http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/) – iRiziya Jul 29 '15 at 15:13
  • I post some answer for you – Lamour Jul 29 '15 at 15:25
  • [UILocalNotification](https://developer.apple.com/documentation/uikit/uilocalnotification) has been deprecated. Take a look at [this Swift 3 example](https://stackoverflow.com/a/45247943/4754881) for local notifications with [UNUserNotificationCenter](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter). – Derek Soike Jul 21 '17 at 23:11

1 Answers1

1

Base on what I see you implement some function inside of the ButtonAction function which is wrong... you should implement the sendNotficationfunction 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)
        }

    }
Lamour
  • 3,002
  • 2
  • 16
  • 28