Push Notifications in iOS — Swift 5.2
Local Notification by UNNotificationTrigger
Push Notification will help to boost the App Engagement, App Retention Rates and Higher Click-Through Rate. It also helps in Encouraging to continue Abandoned Activity e.g., if the user exits from the checkout. Also we can engage user specific activities.
This post will take you on a deep dive into Local Push Notifications.
In the first part of this post, you’ll start by the easiest form of notifications — Local Notifications. It doesn’t require a server and it is a quick way to reach users by delivering messages and content.
In this tutorial, we are going to build a simple app that will notify the user locally. Notification based on three different criteria and they are
- Time (notify the user to drink water on a specific interval of time)
- Date (wishing the user on special occasions or notify about a task/event)
- Location (welcome a user to enter his home after a long back)
Download the demo project from the GitHub.
Setting up NotificationService Class
First, now you are going to configure the NotificationService class. It is a helper class that contains all the functions related to notification along with UNUserNotificationCenterDelegate methods.
- Authorization
- Extend UNUserNotificationCenterDelegate methods in NotificationService.
willPresentNotification
method will be called if notification is presented only if the application is in the foreground state anddidReceiveNotificationResponse
method will be called when the user responded to the notification.
- Call the authorization — I always call the authorization either in a splash screen or in a home screen and not in the AppDelegate’s method didFinishLaunchingWithOptions. Because it will pull down the app launching time.
- Now, run the application and you will see the alert, and then the user may grant or deny authorization. The system stores the user’s response so that subsequent calls to this method do not prompt the user again.
1. Timer Trigger
In the demo application, we gonna remind the user to drink enough water in the specified interval of time and it can be repeated if needed.
1.1. Setting up Timer Trigger
It is all about how actual notification is composed. The notification is made of the notification request which is consists of three different parts.
UNMutableNotificationContent
— Create an object that specifies the payload for a local notification. The payload may be of title and subtitle for an alert, the sound to play, or the value to assign to your app's badge, specify a custom launch image as launchImageName, and a thread identifier for visually grouping related notifications as categoryIdentifier.UNTimeIntervalNotificationTrigger
— can be scheduled on the device to notify after the time interval, and optionally repeat.UNNotificationRequest
— A request to schedule a local notification, which includes the content of the notification and the trigger conditions for delivery.
Note: the time interval must be at least 60 if repeating else it will lead to application crash due to ‘NSInternalInconsistencyException’
1.2. Trigger the Timer Notification
Now run the demo application and tap the top icon on HomeViewController
screen, an UIDatePicker will be shown to the user to get his choice of the time interval and the choice of repeat mode. Then the timer can be started by using the method as shown below.
The content in the Notification is based on what we configured in the UNMutableNotificationContent
i.e., it has a title, subtitle, and sound.
2. Date Trigger
In the demo application, we want to send the anniversary wishes to the user based on the date and time.
2.1. Setting up Date Trigger
UNCalendarNotificationTrigger
is used to configure the date based trigger and the remaining are the same as in timer-based trigger configuration.
2.2. Trigger the Date based Notification
In the demo application and tap the bottom right icon on HomeViewController
screen, an UIDatePicker will be shown to the user to get his choice of the date and time and the choice of repeat mode. Then the notification can be triggered by using the method as shown below.
3. Location Trigger
In the demo application, we send a welcome back notification when he returns back home after vacation spot.
3.1. Setting up Location Trigger
3.1. Setting up LocationService
Before setting up a location-based trigger, we need to do configure the LocationService
class which involves the CoreLocation framework.
updateLocation()
method used to check the authorization to access the user’s location. If it is authorized, then the startUpdatingLocation() method will be triggered to access the location from GPS.
getCurrentRegion
method used to get the CLCircularRegion from the given location and radius.
didUpdateLocation
method from CLLocationManagerDelegate will be invoked when new locations are available. Desired locations will be received as an array of CLLocation objects in chronological order.
3.2. Setting up Location Trigger
UNLocationNotificationTrigger
is used to configure the location-based trigger and the remaining is the same as in timer and the date based trigger configuration. A trigger causes a notification to be delivered when the user’s device enters or exits the specified geographic region. properties to specify whether you want notifications to be delivered on entry, on exit, or both.
removePendingNotification
removes if any pending notifications are available in the current UNUserNotificationCenter. Pending Notifications are identified by the identifier given while configuring the UNNotificationRequest.
To test the above scenario in the simulator, tap the left bottom icon in the home screen, and simulate the location e.g., Mumbai, India in Xcode’s debug area. Now the home location is considered as Mumbai, India. Next considered that you have started for vacation and reached London (you need to simulate the location to London, England). Once your vacation has been over you planned back to your home (you need to simulate the location to Mumbai, India). Then you will get the notification as in the above screenshots and the whole step is shown in the below image.
Source: Apple Docs and sources from the Internet
Get the full source code at GitHub
Reach me on LinkedIn
Feel like a good post?, Recommend this post (by clicking 👏 button)