---
title: "#259: Notification Triggers 💭"
url: https://littlebitesofcocoa.com/259-notification-triggers
author: Jake Marsh
published: 2016-09-06
topics: ["Notifications"]
description: "Continuing our look at Notifications in iOS 10, today we'll check out the different notification triggers we can use to display notifications in our app."
---

# #259: Notification Triggers 💭

By Jake Marsh, September 6, 2016. HTML: https://littlebitesofcocoa.com/259-notification-triggers

Continuing our look at [Notifications](https://www.littlebitesofcocoa.com/topics/29-notifications?v=l) in **iOS 10**, today we'll check out the different _notification triggers_ we can use to display **notifications** in our **app**. Let's get started.

We'll use the same standard authorization and testbed setup we did in [Bite #258](https://littlebitesofcocoa.com/258), only this time, we'll create some _triggers_.

We'll compose a quick `UNNotificationContent`:

``` swift
let notification = UNMutableNotificationContent()

notification.title = "Entered Mothership!"
notification.body = "Welcome to Apple."
```

Now, we'll start trying out triggers. First, let's look at a _location trigger_:

``` swift
let region = CLCircularRegion(
  center: CLLocationCoordinate2DMake(
    37.3278964, 
    -122.0215575
  ),
  radius: 100.0,
  identifier: "Mothership"
)

region.notifyOnExit = false

let trigger = UNLocationNotificationTrigger(
  region: region, 
  repeats: false
)
```

This will cause our notification to be displayed when the user enters the area around [Apple's new Spaceship campus](http://www.macworld.co.uk/feature/apple/complete-guide-apple-campus-2-september-3489704/). Neat!

Then, we'll hand everything off to the **notification center** via a `UNNotificationRequest`, just as we did before:

``` swift
UNUserNotificationCenter.current().add(
  UNNotificationRequest(
    identifier: "mothership-1",
    content: notification,
    trigger: trigger
  )
)
```

When it comes to _time-based triggers_, we're given a couple of options.

We'll compose another quick `UNNotificationContent`:

``` swift
let notification = UNMutableNotificationContent()

notification.title = "WAKE UP!"
notification.body = "You're late for the Mothership!"
```

There's `UNCalendarNotificationTrigger`. This allows us to specify <i>only the components we want</i> to trigger on. For example, here we're creating a **trigger** that will repeat every morning at 7:15 AM:

``` swift
let trigger = UNCalendarNotificationTrigger(
  dateMatching: DateComponents(hour: 7, minute: 15),
  repeats: true
)
```

We can also easily **trigger** a **notification** after a **delay**. Here we show one after 30 seconds:

``` swift
let trigger = UNTimeIntervalNotificationTrigger(
  timeInterval: 30,
  repeats: false
)
```
