Now that we have a time, let's show our notification along with the time selected:
- In the RestaurantDetailViewController.swift file, after import LetsEatDataKit, add the following:
import UserNotifications
- Next, inside of the showNotification() method, add the following:
let content = UNMutableNotificationContent()
if let name = selectedRestaurant?.name { content.title = name }
if let time = sender { content.body = "Table for 7, tonight at \(time) " }
content.subtitle = "Restaurant Reservation"
content.badge = 1
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let identifier = "letsEatReservation"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
// handle error
})
In the preceding code, we are creating a notification content object. In this object, we are going to set the title, the body, the subtitle, the badge, and the sound.
- After the initialize() method, add the following method:
func setupNotificationDefaults() {
UNUserNotificationCenter.current().delegate = self
}
This method is our delegate method for notifications. We get an error for our delegate, because we have not yet implemented the required functions.
- Let's do that now by creating an extension at the end of this file, after the last curly brace. You may already have an extension in this file for our map if you tackled the challenges at the end of Chapter 11, Designing Static Tables; if so, add this new extension after the last curly brace of that Map extension. In either case, add the following code:
extension RestaurantDetailViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
}
- Finally, we just need to call the setupNotificationDefaults() method inside of our initialize() method. Your updated initialize() method should now look like the following:
func initialize() {
setupLabels()
setupMap()
setupNotificationDefaults()
}
- Build and run the project by hitting the Play button (or using ⌘ + R). Open a restaurant detail page, tap the time button, and wait five seconds. You should see the following:

- Then, if you click and pull down on the notification, you should see the following:

We just implemented a basic notification; however, we can do so much more. Next, let's get an image inside of our notification.