We’ll explore how to setup local push notification in iOS 14 along with its benefits and limitations.
Local push notification unlike remote notification do not rely on any backend push server and thus its delivery works on the basis of a scheduler. (Apple Developer)
We have to pre-define local push notification with its variables like Title, Description, Time and Date on when this notification will be sent. If needed we can add deeplink that gets fired when a user interacts with push notification. With deeplink we can perform extra task related to push notification.
- Local notifications do not require any backend push server. Everything is client based.
- It can be scheduled and sent at any time with proper setup.
- Client has more control over local notification as compared to remote notification.
- We can keep track of all delivered / pending notifications from client
- Deeplink can be easily setup in local notification and this makes testing it more easier.
- It works even if there is no internet connection.
- Since local push notifications are client based its setup requires slightly more work.
- Once scheduled we cannot remove local notifications unless user opens the app and we perform some sort of check to reschedule or remove notifications.
- Older iOS version limit. (Should be taken with grain of salt as we use new framework in iOS 10 and above)
- An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.
- This should not affect if you are scheduling less than 64 notifications per app.
Below are the steps to setup local notification in any iOS App.
1. AppDelegate Setup
This setup is optional as even without AppDelegate setup, user will still receive local push notification but if we want to show push notification when app is in foreground and perform certain actions when user taps on the notification we need to conform to UNUserNotificationCenterDelegate
2. Check and request authorization
Using UserNotifications framework (UNUserNotificationCenter) we can check if user has given iOS permission to send push notifications.
Note: In all code snippets the center is referred to as UNUserNotificationCenter.current() but we only need to define it once.
3. Ask for authorization
If push notifications are not authorized then we need to ask users to grant permission to send push notifications. Depending on the type of push notification we want to send we can request with permissions like (alert, badge and sound)
Local notification can be scheduled based on time interval or it can be sent on specific date and time with custom Title, Body (description) and a unique id that can help us find the notification in case we would like to remove or modify it.
5. Find and modify
There might be cases where we need to find a scheduled or delivered notification and for that UserNotifications framework has some methods that allow us to find all or specific notifications using the unique Id we set for it while scheduling push notification.
We can remove a specific notification by searching for its unique id or we can remove all delivered and pending notifications.
Below are some reference links along with a simple project to learn more about local push notifications in Swift.