A timed notification is the simplest notification to schedule, which makes it a great candidate to explore first. Before we start, you should add the following method call after both calls to hideNotificationsUI in ViewController.swift:
self?.scheduleNotification()
This method will provide a nice spot to implement the scheduling of different notifications. Add the following implementation for this method to the ViewController class:
func scheduleNotification () {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "new-quote", content: createNotification(), trigger: trigger)
notificationCenter.add(request, withCompletionHandler: nil)
}
First, a trigger is created for the notification. In this case, we're creating a notification that is set up to fire after a certain amount of time has passed, in this case 10 seconds, so we create an instance of UNTimeIntervalNotificationTrigger. Next, we create a notification request and pass it an identifier, the trigger we just created, and the content we want to display. Finally, we ask the notification center to add our notification request.
The identifier attribute for a notification should be unique for the content of your notification. The system uses this identifier to avoid sending multiple notifications with the same content. For our app, we wouldn't want the user to have multiple unread notifications, because the content will always be the same. If you do want to display multiple entries in the Notification Center you should make sure that every notification you schedule has a unique identifier.
If you want to send a repeating notification to a user, you must set the repeats property of the time interval trigger to true.