Member-only story
Before we begin, you can download the initial draft. The application is a table with a list of types of notifications.
Now, if you tap on any line, Alert pops up with the name of the notification. By the end of this tutorial, this action will send a local notification with the contents of the string. In addition, the user will be able to interact with this notification, choosing to do this or that action.
Before your application can send any notifications, the user must give their consent to this. To do this, when you first start the application, you must fulfill the request for sending notifications, so that the user can approve or reject it.To create such a request, go to theAppDelegate
class and import the UserNotifications
framework.
import UserNotifications
In the class properties, create a new notificationCenter
property and assign an instance of the UNUserNotificationCenter
class to it to manage notifications. This will be our notification center.
let notificationCenter = UNUserNotificationCenter.current()
Go to the didFinishLaunchingWithOptions
method and declare the options
, which will contain the options available for our notifications. In total, we now have four properties available: badge
, sound
, alert
and carPlay
.
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
Having decided on the list of notification parameters, we can ask the user for permission…