Local Notifications in Swift 5 and iOS 13 with UNUSerNotificationCenter

Lachlan Miller
4 min readOct 8, 2019
Thanks to drawkit.io for the nice illustration

I’ve been delving into iOS programming lately. I’ve had a great experience with React Native, but some things are just nicer done native. Notifications, for example, have different UI/UX and limitations depending on the OS.

This article covers local notifications using UNUserNotificationCenter. I'm using Swift 5, Xcode 11 and iOS 13.

This article is also available for reading on my personal blog, sans paywall and large amounts of JavaScript.

Create a new Single Page app! That’s it.

This app will use a very simple interface built using the new SwiftUI. In ContentView.swift, add the following:

You should be able to preview this using the canvas. If it is not shown, click "Help" and type "canvas". It looks like this:

This will let us set a notification by pressing a button. Let’s move onto the main topic: UNUserNotificationCenter. Create a new file, LocalNotificationManager.swift, with some boilerplate code:

Now we will build four functions to handle the notification flow. We need to request permission to show notifications, then schedule some notifications.

Add a requestAuthorization method. This will request permission to show notifications, and if permission is granted, then schedule any existing notifications the application has created.

You can use the simulator or a real device to try this. You should see the following:

Next, let’s make a method to add notifications. This will not schedule them — that comes later.

Simple stuff. We use a UUID to ensure the id is unique.

Now we have a way to request permission and to add some notifications — let’s schedule them with a scheduleNotifications function.

There are few interesting things here. Firstly, we use UNMutableNotificationContent. The rest of the code is modelled after the example from the Apple Developer docs. The name Mutable suggests there is an Immutable type - ther isn't. From what I gathered, the mutable here refers to the part of a UNNotification that is mutable - that is, the content the developer is free to modify.

This example sets a notification to appear in 5 seconds. Other types of notifications include UNCalendarNotificationTriggger. UNLocationNotificationTrigger and UNPushNotificationTrigger.

If you have a device, or use the simulator, you can try this out by updating the setNotification function in ContentView.swift:

Click on “Set Notification” and minimize the test app. After 5 seconds pass, you should see the notification! If you did not minimize the app, you will notice the notification does not show. We will address this later. Until iOS 12, notifications would not show if the app was in the foreground, however from iOS 12 you are able to show notifications in both the foreground and background. To do this, you need to add a bit of extra code in AppDelegate.swift. When you extend a class using a delegate, you enable the class to hand off some behaviour to an instance of another type.

According to the docs, the UNUserNotificationCenterDelegate is used to handle incoming notifications. We can use the userNotificationCenter(_:willPresent:withCompletionHandler:) to "ask the delegate how to handle a notification that arrived while the app was running in the foreground". This is exactly what we need.

Update AppDelegate.swift:

There are several changes:

  • Add UNUserNotificationCenterDelegate to the class declaration
  • Add a userNotificationCenter function that calls the completionHandler
  • Assign UNUserNotificationCenter.current().delegate to self in application

Now you can press “Set Notification” and see a notification appear in-app.

A Refactor using getNotificationSettings

We can improve the code a bit — namely, only ask for permission if we haven’t already asked. Add a schedule function to the LocalNotificationManager class:

Now we don’t need to know the authorization status — just call schedule. Since we still want to schedule notifications after asking for permission, update requestPermission:

The final code for ContentView.swift is now as follows:

This article introduces a number of things, including:

  1. UNUserNotificationCenter
  2. SwiftUI (not the main focus, but still nice!)
  3. Using delegates, specifically the UNUserNotificationCenterDelegate
  4. The requestAuthorization API

Originally published on my personal blog.

--

--

Lachlan Miller

I write about frontend, Vue.js, and TDD on https://vuejs-course.com. You can reach me on @lmiller1990 on Github and @Lachlan19900 on Twitter.