# Fire action only after notification appears (swift3)

**URL:** <https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323>\
**Category:** Swift\
**Created:** [July 18, 2017, 7:23pm UTC](https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323 "2017-07-18T19:23:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [July 18, 2017, 7:23pm UTC](https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323/1 "2017-07-18T19:23:55Z")

</div>

My code uses a datePicker, that when the datePicker is changed and matches the current date of the user device. It fires a notification. However the AVSpeechUtterance I am using fires as soon as the datePickers time is changed not when the notification appears. I would like the AVSpeechUtterance and notification to be be fired at the same time.

```
    import UIKit
    import AVFoundation
   import UserNotifications

   class ViewController: UIViewController {
 @IBOutlet var datePicker: UIDatePicker!

   @IBAction func datePicker(_ sender: Any) {
let c = UNMutableNotificationContent()
c.title = "Lets Roll"
c.subtitle = "s"
c.body = "d"

let begin = AVSpeechUtterance(string: " Hello ")

let synthesizer = AVSpeechSynthesizer()

begin.voice = AVSpeechSynthesisVoice(language: "en-US")
begin.rate = 0.08

synthesizer.speak(begin)

let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date )
let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

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

```

---

<div class="post-metadata">

**Author:** ![nikita\_gaydukov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nikita_gaydukov/32/77683_2.png) [@nikita\_gaydukov](https://forums.kodeco.com/u/nikita_gaydukov)\
**Post date:** [July 19, 2017, 6:36pm UTC](https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323/2 "2017-07-19T18:36:37Z")

</div>

Hi @zalubski

You can move this code:

```
let begin = AVSpeechUtterance(string: " Hello ")
let synthesizer = AVSpeechSynthesizer()
begin.voice = AVSpeechSynthesisVoice(language: "en-US")
begin.rate = 0.08
synthesizer.speak(begin)

```

To this delegate method of UNUserNotificationCenterDelegate:

`userNotificationCenter(_:willPresent:withCompletionHandler:)`

Note it will work only when your app is in foreground.

Nikita

---

<div class="post-metadata">

**Author:** ![zalubski](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/zalubski/32/81494_2.png) [@zalubski](https://forums.kodeco.com/u/zalubski)\
**Post date:** [July 20, 2017, 12:14am UTC](https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323/3 "2017-07-20T00:14:45Z")

</div>

Thanks for your help.

I dont know how to call this function.

---

<div class="post-metadata">

**Author:** ![nikita\_gaydukov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nikita_gaydukov/32/77683_2.png) [@nikita\_gaydukov](https://forums.kodeco.com/u/nikita_gaydukov)\
**Post date:** [July 20, 2017, 7:01pm UTC](https://forums.kodeco.com/t/fire-action-only-after-notification-appears-swift3/32323/4 "2017-07-20T19:01:02Z")

</div>

You shouldn’t call it. It is called automatically when your app receives a notification and your app is in the foreground.  
You need to declare your AppDelegate as UNUserNotificationCenterDelegate and implement two methods of that protocol.  
Here is an example:

> **[class AppDelegate: UIResponder, UIApplicationDelegate,...](https://pastebin.com/XGwGcUPv)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

Nikita
