This day covers the first part of Project 21: Local Notifications
in Hacking with Swift.
You can follow along directly here.
I previously created projects alongside Hacking with Swift in a separate repository. For 100 Days of Swift, however, I've been extending things further and adding my projects to this repo under each "Part One" folder.
With that in mind, Day 72 focuses on several specific topics:
- Scheduling Notifications:
UNUserNotificationCenter
andUNNotificationRequest
- Acting on responses
Requesting permission to use notifications is pretty straightforward. Making notifications is a completely different beast. Mainly, it involves three components:
- The content to show.
- The trigger for when/how often to show it.
- Creating a
UNNotificationRequest
object that combines the two.
Together, the number of possible content and trigger combinations give us an incredibly powerful system for enhancing the user experience -- if done right 🙂.
For displaying status updates, we can design our trigger logic and content in a fairly straightforward manner:
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 0
dateComponents.minute = 0
dateComponents.second = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let content = UNMutableNotificationContent()
content.title = "🤯 Woah!"
content.body = "The Earth has spun 0.00417 degrees on its axis since its day began in your time zone."
content.sound = .default
let notificationRequest = UNNotificationRequest(identifier: uuid, content: content, trigger: Trigger.firstSecondOfDay)
By adding UNNotificationCategory
to the mix, we can create buttons to be shown along with our notification that, in turn, allow us to handle the way users interact with them.
This involves a few steps:
- Creating instances of
UNNotificationAction
. - Creating an instance of
UNNotificationCategory
, specifying an array of theUNNotificationAction
s that we want to associate it with. - Registering the
UNNotificationCategory
instance with the notification center.
From there, any UNNotificationRequest
s... with content
... with a category
... matching a registered UNNotificationCategory
's identifier
... will be displayed with the actions
assigned to the category 💪.