local notification not appearing (Swift4)












0














My code below is supposed to act as an alarm clock. When the date and time matches nothing is happening but I am seeing my print in the log file. Please try this code and test what I am doing it wrong. It seemed liked it worked before. This is supposed to still go off in the background if the user is not using the app.



      import UIKit;import AVFoundation;import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {var timer = Timer();var isGrantedAccess = false;var player: AVAudioPlayer?
var passingDate : Date?
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()
@IBOutlet var taskIDo: UITextView!



override func viewWillAppear(_ animated: Bool)
{
createDatePicker()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
}

func playSound() {
let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }

player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()

let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)

dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
}


func testDate() {
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
{
return
}

passingDate = datePicker.date


if dptext.text != ""
{

let c = UNMutableNotificationContent()
c.title = "Lets Roll"


let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
/////


let when = DispatchTime.now() + 20
DispatchQueue.main.asyncAfter(deadline: when) {



}

} else {
print("No its not empty")}}}


func donePressed() {

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)



}}









share|improve this question





























    0














    My code below is supposed to act as an alarm clock. When the date and time matches nothing is happening but I am seeing my print in the log file. Please try this code and test what I am doing it wrong. It seemed liked it worked before. This is supposed to still go off in the background if the user is not using the app.



          import UIKit;import AVFoundation;import UserNotifications
    class ViewController: UIViewController, UNUserNotificationCenterDelegate {var timer = Timer();var isGrantedAccess = false;var player: AVAudioPlayer?
    var passingDate : Date?
    @IBOutlet var dptext: UITextField!
    let datePicker = UIDatePicker()
    @IBOutlet var taskIDo: UITextView!



    override func viewWillAppear(_ animated: Bool)
    {
    createDatePicker()
    timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
    }

    func playSound() {
    let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
    do {
    player = try AVAudioPlayer(contentsOf: url)
    guard let player = player else { return }

    player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


    func createDatePicker() {
    datePicker.datePickerMode = .dateAndTime
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
    toolbar.setItems([doneButton], animated: false)

    dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
    }


    func testDate() {
    if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
    if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
    {
    return
    }

    passingDate = datePicker.date


    if dptext.text != ""
    {

    let c = UNMutableNotificationContent()
    c.title = "Lets Roll"


    let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

    UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
    /////


    let when = DispatchTime.now() + 20
    DispatchQueue.main.asyncAfter(deadline: when) {



    }

    } else {
    print("No its not empty")}}}


    func donePressed() {

    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .short
    dateFormatter.timeStyle = .short
    dptext.text = dateFormatter.string(from: datePicker.date)
    self.view.endEditing(true)



    }}









    share|improve this question



























      0












      0








      0







      My code below is supposed to act as an alarm clock. When the date and time matches nothing is happening but I am seeing my print in the log file. Please try this code and test what I am doing it wrong. It seemed liked it worked before. This is supposed to still go off in the background if the user is not using the app.



            import UIKit;import AVFoundation;import UserNotifications
      class ViewController: UIViewController, UNUserNotificationCenterDelegate {var timer = Timer();var isGrantedAccess = false;var player: AVAudioPlayer?
      var passingDate : Date?
      @IBOutlet var dptext: UITextField!
      let datePicker = UIDatePicker()
      @IBOutlet var taskIDo: UITextView!



      override func viewWillAppear(_ animated: Bool)
      {
      createDatePicker()
      timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
      }

      func playSound() {
      let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
      do {
      player = try AVAudioPlayer(contentsOf: url)
      guard let player = player else { return }

      player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


      func createDatePicker() {
      datePicker.datePickerMode = .dateAndTime
      let toolbar = UIToolbar()
      toolbar.sizeToFit()

      let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
      toolbar.setItems([doneButton], animated: false)

      dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
      }


      func testDate() {
      if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
      if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
      {
      return
      }

      passingDate = datePicker.date


      if dptext.text != ""
      {

      let c = UNMutableNotificationContent()
      c.title = "Lets Roll"


      let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
      let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

      UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
      /////


      let when = DispatchTime.now() + 20
      DispatchQueue.main.asyncAfter(deadline: when) {



      }

      } else {
      print("No its not empty")}}}


      func donePressed() {

      let dateFormatter = DateFormatter()
      dateFormatter.dateStyle = .short
      dateFormatter.timeStyle = .short
      dptext.text = dateFormatter.string(from: datePicker.date)
      self.view.endEditing(true)



      }}









      share|improve this question















      My code below is supposed to act as an alarm clock. When the date and time matches nothing is happening but I am seeing my print in the log file. Please try this code and test what I am doing it wrong. It seemed liked it worked before. This is supposed to still go off in the background if the user is not using the app.



            import UIKit;import AVFoundation;import UserNotifications
      class ViewController: UIViewController, UNUserNotificationCenterDelegate {var timer = Timer();var isGrantedAccess = false;var player: AVAudioPlayer?
      var passingDate : Date?
      @IBOutlet var dptext: UITextField!
      let datePicker = UIDatePicker()
      @IBOutlet var taskIDo: UITextView!



      override func viewWillAppear(_ animated: Bool)
      {
      createDatePicker()
      timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
      }

      func playSound() {
      let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
      do {
      player = try AVAudioPlayer(contentsOf: url)
      guard let player = player else { return }

      player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


      func createDatePicker() {
      datePicker.datePickerMode = .dateAndTime
      let toolbar = UIToolbar()
      toolbar.sizeToFit()

      let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
      toolbar.setItems([doneButton], animated: false)

      dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
      }


      func testDate() {
      if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
      if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
      {
      return
      }

      passingDate = datePicker.date


      if dptext.text != ""
      {

      let c = UNMutableNotificationContent()
      c.title = "Lets Roll"


      let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
      let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

      UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
      /////


      let when = DispatchTime.now() + 20
      DispatchQueue.main.asyncAfter(deadline: when) {



      }

      } else {
      print("No its not empty")}}}


      func donePressed() {

      let dateFormatter = DateFormatter()
      dateFormatter.dateStyle = .short
      dateFormatter.timeStyle = .short
      dptext.text = dateFormatter.string(from: datePicker.date)
      self.view.endEditing(true)



      }}






      datepicker notifications avfoundation swift4 unusernotificationcenter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 27 '18 at 22:15

























      asked Dec 27 '18 at 21:47









      silly milly

      213




      213
























          1 Answer
          1






          active

          oldest

          votes


















          0














          A few things:




          • Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.

          • If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.


          • Request permissions for notifications in your app delegate:



            center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
            }



          Here's the code I used to test your configuration:



          import UIKit
          import AVFoundation
          import UserNotifications

          let center = UNUserNotificationCenter.current() // usernotification center

          class ViewController: UIViewController, UNUserNotificationCenterDelegate {
          var timer = Timer()
          var isGrantedAccess = false
          var player: AVAudioPlayer?
          var passingDate : Date?
          @IBOutlet var dptext: UITextField!
          let datePicker = UIDatePicker()
          @IBOutlet var taskIDo: UITextView!

          override func viewWillAppear(_ animated: Bool) {

          center.delegate = self
          createDatePicker()
          timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)

          }

          func playSound() {
          let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
          do {
          player = try AVAudioPlayer(contentsOf: url)
          guard let player = player else { return }

          player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


          func createDatePicker() {
          datePicker.datePickerMode = .dateAndTime
          let toolbar = UIToolbar()
          toolbar.sizeToFit()

          let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
          toolbar.setItems([doneButton], animated: false)

          dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
          }

          @objc func testDate() {
          print("in testDate")
          if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
          if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
          {
          print("in return")
          return
          }

          passingDate = datePicker.date

          let content = UNMutableNotificationContent()
          content.badge = true
          content.title = "Test"
          content.body = "Lets Roll!"
          content.sound = UNNotificationSound.default

          let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
          let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)

          UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
          dump(error)
          })

          }
          }

          @objc func donePressed() {

          let dateFormatter = DateFormatter()
          dateFormatter.dateStyle = .short
          dateFormatter.timeStyle = .short
          dptext.text = dateFormatter.string(from: datePicker.date)
          self.view.endEditing(true)

          }
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951191%2flocal-notification-not-appearing-swift4%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            A few things:




            • Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.

            • If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.


            • Request permissions for notifications in your app delegate:



              center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
              // Enable or disable features based on authorization.
              }



            Here's the code I used to test your configuration:



            import UIKit
            import AVFoundation
            import UserNotifications

            let center = UNUserNotificationCenter.current() // usernotification center

            class ViewController: UIViewController, UNUserNotificationCenterDelegate {
            var timer = Timer()
            var isGrantedAccess = false
            var player: AVAudioPlayer?
            var passingDate : Date?
            @IBOutlet var dptext: UITextField!
            let datePicker = UIDatePicker()
            @IBOutlet var taskIDo: UITextView!

            override func viewWillAppear(_ animated: Bool) {

            center.delegate = self
            createDatePicker()
            timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)

            }

            func playSound() {
            let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
            do {
            player = try AVAudioPlayer(contentsOf: url)
            guard let player = player else { return }

            player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


            func createDatePicker() {
            datePicker.datePickerMode = .dateAndTime
            let toolbar = UIToolbar()
            toolbar.sizeToFit()

            let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
            toolbar.setItems([doneButton], animated: false)

            dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
            }

            @objc func testDate() {
            print("in testDate")
            if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
            if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
            {
            print("in return")
            return
            }

            passingDate = datePicker.date

            let content = UNMutableNotificationContent()
            content.badge = true
            content.title = "Test"
            content.body = "Lets Roll!"
            content.sound = UNNotificationSound.default

            let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
            let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)

            UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
            dump(error)
            })

            }
            }

            @objc func donePressed() {

            let dateFormatter = DateFormatter()
            dateFormatter.dateStyle = .short
            dateFormatter.timeStyle = .short
            dptext.text = dateFormatter.string(from: datePicker.date)
            self.view.endEditing(true)

            }
            }





            share|improve this answer


























              0














              A few things:




              • Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.

              • If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.


              • Request permissions for notifications in your app delegate:



                center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
                // Enable or disable features based on authorization.
                }



              Here's the code I used to test your configuration:



              import UIKit
              import AVFoundation
              import UserNotifications

              let center = UNUserNotificationCenter.current() // usernotification center

              class ViewController: UIViewController, UNUserNotificationCenterDelegate {
              var timer = Timer()
              var isGrantedAccess = false
              var player: AVAudioPlayer?
              var passingDate : Date?
              @IBOutlet var dptext: UITextField!
              let datePicker = UIDatePicker()
              @IBOutlet var taskIDo: UITextView!

              override func viewWillAppear(_ animated: Bool) {

              center.delegate = self
              createDatePicker()
              timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)

              }

              func playSound() {
              let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
              do {
              player = try AVAudioPlayer(contentsOf: url)
              guard let player = player else { return }

              player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


              func createDatePicker() {
              datePicker.datePickerMode = .dateAndTime
              let toolbar = UIToolbar()
              toolbar.sizeToFit()

              let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
              toolbar.setItems([doneButton], animated: false)

              dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
              }

              @objc func testDate() {
              print("in testDate")
              if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
              if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
              {
              print("in return")
              return
              }

              passingDate = datePicker.date

              let content = UNMutableNotificationContent()
              content.badge = true
              content.title = "Test"
              content.body = "Lets Roll!"
              content.sound = UNNotificationSound.default

              let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
              let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)

              UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
              dump(error)
              })

              }
              }

              @objc func donePressed() {

              let dateFormatter = DateFormatter()
              dateFormatter.dateStyle = .short
              dateFormatter.timeStyle = .short
              dptext.text = dateFormatter.string(from: datePicker.date)
              self.view.endEditing(true)

              }
              }





              share|improve this answer
























                0












                0








                0






                A few things:




                • Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.

                • If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.


                • Request permissions for notifications in your app delegate:



                  center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
                  // Enable or disable features based on authorization.
                  }



                Here's the code I used to test your configuration:



                import UIKit
                import AVFoundation
                import UserNotifications

                let center = UNUserNotificationCenter.current() // usernotification center

                class ViewController: UIViewController, UNUserNotificationCenterDelegate {
                var timer = Timer()
                var isGrantedAccess = false
                var player: AVAudioPlayer?
                var passingDate : Date?
                @IBOutlet var dptext: UITextField!
                let datePicker = UIDatePicker()
                @IBOutlet var taskIDo: UITextView!

                override func viewWillAppear(_ animated: Bool) {

                center.delegate = self
                createDatePicker()
                timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)

                }

                func playSound() {
                let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
                do {
                player = try AVAudioPlayer(contentsOf: url)
                guard let player = player else { return }

                player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


                func createDatePicker() {
                datePicker.datePickerMode = .dateAndTime
                let toolbar = UIToolbar()
                toolbar.sizeToFit()

                let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
                toolbar.setItems([doneButton], animated: false)

                dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
                }

                @objc func testDate() {
                print("in testDate")
                if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
                if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
                {
                print("in return")
                return
                }

                passingDate = datePicker.date

                let content = UNMutableNotificationContent()
                content.badge = true
                content.title = "Test"
                content.body = "Lets Roll!"
                content.sound = UNNotificationSound.default

                let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
                let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)

                UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
                dump(error)
                })

                }
                }

                @objc func donePressed() {

                let dateFormatter = DateFormatter()
                dateFormatter.dateStyle = .short
                dateFormatter.timeStyle = .short
                dptext.text = dateFormatter.string(from: datePicker.date)
                self.view.endEditing(true)

                }
                }





                share|improve this answer












                A few things:




                • Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.

                • If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.


                • Request permissions for notifications in your app delegate:



                  center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
                  // Enable or disable features based on authorization.
                  }



                Here's the code I used to test your configuration:



                import UIKit
                import AVFoundation
                import UserNotifications

                let center = UNUserNotificationCenter.current() // usernotification center

                class ViewController: UIViewController, UNUserNotificationCenterDelegate {
                var timer = Timer()
                var isGrantedAccess = false
                var player: AVAudioPlayer?
                var passingDate : Date?
                @IBOutlet var dptext: UITextField!
                let datePicker = UIDatePicker()
                @IBOutlet var taskIDo: UITextView!

                override func viewWillAppear(_ animated: Bool) {

                center.delegate = self
                createDatePicker()
                timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)

                }

                func playSound() {
                let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
                do {
                player = try AVAudioPlayer(contentsOf: url)
                guard let player = player else { return }

                player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}


                func createDatePicker() {
                datePicker.datePickerMode = .dateAndTime
                let toolbar = UIToolbar()
                toolbar.sizeToFit()

                let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
                toolbar.setItems([doneButton], animated: false)

                dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
                }

                @objc func testDate() {
                print("in testDate")
                if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
                if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
                {
                print("in return")
                return
                }

                passingDate = datePicker.date

                let content = UNMutableNotificationContent()
                content.badge = true
                content.title = "Test"
                content.body = "Lets Roll!"
                content.sound = UNNotificationSound.default

                let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
                let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)

                UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
                dump(error)
                })

                }
                }

                @objc func donePressed() {

                let dateFormatter = DateFormatter()
                dateFormatter.dateStyle = .short
                dateFormatter.timeStyle = .short
                dptext.text = dateFormatter.string(from: datePicker.date)
                self.view.endEditing(true)

                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 28 '18 at 4:01









                Alex Bailey

                370316




                370316






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53951191%2flocal-notification-not-appearing-swift4%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'