Group Group Group Group Group Group Group Group Group

Local notification not appearing (Swift4)


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)
    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();} catch let error as NSError {print(error.description)}}

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

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(, equalTo: Date(), toGranularity: .minute) {
        if let passingDate = passingDate, Calendar.current.isDate(, equalTo: passingDate, toGranularity: .minute)

        passingDate =

      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 = + 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:



@zalubski Thanks very much for your question!

Have you tried using break points?

Just as an FYI, I don’t see where your method “playSound” is being called from within your code. Perhaps that is the problem? Just wondering.


Hi John (@zalubski),
The only print in your code is in the else part of if dptext.text != "" and the else part says “No it’s not empty” but in reality it IS empty.

However, the next bit is that you have no code running in the DispatchQueue at the end of the deadline use that to see that your 20 seconds are up.

lastly, you are setting up a notification center but what happens when a message is received? Where or how is it handled?