Scheduled User Notifications [Local Notification] iOS

Doyeon
doyeona
Published in
4 min readJan 14, 2021

Use notifications to communicate important information to users of your app, regardless of whether your app is running on the user’s device.

There are two types of notifications

  • Local Notification: it’s scheduled and managed by iOS devices such as the default app Reminders from iOS.
  • Push Notification: it requires a remote computer to determine when and what to send and push these notifications to all of the devices that are registered in the server.

This post will be about Local Notification ONLY because i haven’t studied Push Notification yet! but i will be posting that too soon 🤦🏻‍♀️

Local Notification

Again, Local notification is a way to deliver specific messages with specific time created from the app through Notification Center. It’s very popular ways to deliver a message when your app is closed or in the background because it’s the best way get user’s attention 🤭

Easy steps on Local Notification

❗️ To use UserNotifications you have to import UserNotifications framework in AppDelegate.Swift

import UserNotifications

Create UNUserNotificationCenter.current()

❗ Create a new notificationCenter property in the AppDelegate class’ didFinishLaunchingWithOptions so that it will be assigned very beginning like before the first screen is launching. It will manage notifications such as check the number of schedules, and send the message at the right time. UNUserNotificationCenter is a singleton pattern so do not make an instance directly like ❌ let notificationCenter = UNUserNotificationCenter() but instead of this,.current to get reference.

let notificationCenter = UNUserNotificationCenter.current()

Ask User Authorization

❗ Next step is to Ask User Authorization to get a permission whether we can send a notification. I created the method requestAuthForLocalNotification in AppDelegate class.

func requestAuthForLocalNotifications(){     notificationCenter.delegate = self     let options: UNAuthorizationOptions = [.alert, .sound, .badge]     notificationCenter.requestAuthorization(options: options) { (didAllow, error) in
if
!didAllow {
print("User has declined notification")
}
}
}

Declare options which contains .alert, .sound, .badge properties and we can ask user for permission to send notifications from our app using notificationCenter.requestAuthorization(options: options)

so AppDelegate.swift will look like this below :

When you run your app, it will receive a request to send notifications and the setting can be changed anytime by user.

Create a notification

❗ Next step is to Create the notification

Before that, we have to check the authorizationStatus whether user has allow it or not for us to send the notification. if yes, then we can add contents to the NotificationCenter.

UNMutableNotificationContent is a content containing title,subtitle, body, onContent, badge, sound, launchImageName, userInfo, attachment properties. With mutable keywords, we are allowed to set up the properties. There’s also UNNotificationContent without a Mutable keyword and we cannot edit it but only read.

Notification Trigger

We’ll create UNNotificationTrigger to deliver the notification. There’s 4 types of concrete subclass that define the trigger condition.

  • UNTimeIntervalNotificationTrigger
  • UNCalendarNotificationTrigger
  • UNLocationNotificationTrigger
  • UNPushNotificationTrigger

so which means… We can send notification by location, time, date, ,, 🐤 Since I use datePicker to get the date from the user, I will use UNCalendarNotificationTrigger to make notification which will include year, month, day, hour and minute

btw, I used datePicker to get date and the time at the #1.

Register with Notification Center

❗Finally, we are done with the preparations… Let’s send the request!

In #1, we need an identifier for UNNotificationRequest to identify the notification. It can be used to replace or remove a pending notification request or a delivered notification.

So when it’s several notifications, make your own unique identifier or else, you can use let identifier = UUID().uuidString instead.

Lastly, request notification to NoticicationCenter using .add at #3 ~ #9.

When you run the code, you will get a local notification

You can also give customizing actions when the user tapped the notification at the AppDelegate

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 
// your code
}

Here’s my full code on scheduleLocalNotification(). you can simply call this method whenever you need to request a notification.

Also some Actions can be attached to the notification like Buttons.. 🐽

Now, let’s try to delete the notification from the queue? pending notification.

How to remove Notification

func removePendingNotificationRequests(withIdentifiers identifiers: [String])
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["identifier"])

identifiers : An array of NSString objects, each of which contains the identifier of an active UNNotificationRequest object.

Then you will be able to successfully remove the notification in your pending request list that has the same identifier. For more details, Please check the links below.

Thank you for reading 💜

--

--

Doyeon
doyeona

Passionate iOS developer creating extraordinary apps. Innovative, elegant, and collaborative. Constantly pushing boundaries.Let's build the future together📱✨🚀