Do statement not firing notification (swift4)

First off I am not very familiar with do statements but I think they can be used just like a action button. My code below is a alarm. All I wanted to do is when the alarm goes off for the notification to appear. Thats it. Right now the alarm goes off (successT is printed in log file) the notification does not appear.

    override func viewDidLoad() {
      UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (didAllow, error) in



     @objc func testDate() {
    if Calendar.current.isDate(, equalTo: Date(), toGranularity: .minute) {

        passingDate =
        do {

        let content = UNMutableNotificationContent()
        content.title = "3 seconds are up"
        content.badge = 1
         content.subtitle = "d"
         content.body = "theBody"

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger )

        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)


do statements are not like action buttons – all it does is introduce a new scope such that any new names you introduce via let or var cannot be seen outside of its close curly brace. The most common way to use them is with the optional catch clauses for exception handling. By itself, do doesn’t do much.

The issue you are having is that notifications are not shown (by default) while the app is in the foreground. It is expected that the app itself uses its own UI to alert the user. Notifications are for when your app is not in the foreground.

But, if you are not in the foreground, then testDate() is not going to be called. For an alarm, the thing to do is to set the UserNotification to the point in the future when you want it to go off – then if you are not running, the notification will be sent. So don’t make it at the point it’s supposed to go off – make the notification object when the user sets the time (and use timeInterval to be that time)

If you are running in the foreground when the time is up, put up your own UI for the alarm. This StackOverflow answer shows how you can do that easily (by telling iOS to show the regular notification):

This topic was automatically closed after 166 days. New replies are no longer allowed.