How To Implement Local Notifications with the UserNotifications framework

Push user-facing notifications to the user’s device from your app.

Luciano Amoroso
2 min readMay 15, 2020

User-facing notifications communicate important information to the users of your app, regardless of whether your app is running on the user’s device. The notifications can also tell your app to download information and update its interface. Notifications can also display an alert, play a sound, or badge the app’s icon. A Local Notification is a notification in which the app creates the notification details locally and passes it to device. To integrate local notifications into your app, Apple provides a framework called UserNotifications

To put UserNotificaton in your app, add an import near the top of YourView.swift

import UserNotifications

Now we have to ask the user for Authorization!

An Alert will be shown to the user; when the user make their choice, a function we provide will get called and tell us whether the request was successful or not. Change the viewDidLoad method to:

override func viewDidLoad() {
super.viewDidLoad()
//....
UNUserNotificationCenter.current().requestAuthorization(options: [.alert]) {
(granted, error) in
if granted {
//print("Authorization OK!") //Authorization granted
} else {
//print(error.localizedDescription) //authorization not granted
}
}
}

Now, let’s see the main function of our project.

If the user grants permission, we can finally implement our notification.

  • The notificationContent is what you want to show to the user and can be a title, subtitle, sound or an image.
  • The trigger is used to indicate when the notification should be displayed.
  • Through the notificationRequest you can choose the content and trigger; but also add a unique identifier so you can edit or remove specific notification. If you don’t want to edit this, use UUID().uuidString to get a random identifier.
let notificationContent = UNMutableNotificationContent()                       notificationContent.title = "Title of the notification"                       notificationContent.subtitle = "Description"                       notificationContent.sound = UNNotificationSound.default // Show this notification one minutes from now (timeInterval it's based on seconds)                      
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false) //through repeats we can repeat the notification every time based on timeInterval
// Choose an identifier and insert the content and trigger
let notificationRequest = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
// Add our notification request to UserNotification UNUserNotificationCenter.current().add(notificationRequest)

I hope you found this article helpful. If you have any questions, feel free to comment below and I’ll answer it as soon as I can. Thanks!

You can find me on LinkedIn!

--

--

Luciano Amoroso

iOS Developer @AppleAcademy | Web Developer @Freelance