Start a Project

Local Notification in Swift

A ‘local notification’ is a short text-based message which appears on the top of the screen. It lets users know that there is some information for them.

One can send and receive notifications from two types:

  1. Push Notification
  2. Local Notification

‘Push Notification’ => It is sent through the internet

‘Local Notification’ =>It is the schedule and sent locally to the iPhone

In this blog, we will learn about the local Notification. Before our application can send any notifications,  we must fulfil the request for sending notifications so that the user can approve or reject it.

To successfully send the local Notification we have to follow the following steps:-

Steps 1=> First We have to import UserNotifications in Appdelegates, and then we have to create a new 'notificationCenter' property and assign an instance of the ‘UNUserNotificationCenter' class to it to manage notifications. This will be our notification centre.

For more information about ‘UNUserNotificationCenter’ you can check Apple developer link https://developer.apple.com/documentation/usernotifications/unusernotificationcenter

Steps 2=> In the ‘didFinishLaunchingWithOptions‘ methods we will declare an option for our notification, we will use the ‘badge‘, ‘sound‘, and ‘alert

Before sending the notification we have to ask permission from the user

It will look like this when the app launches.

Step 3= > Now to trigger the ‘Notification’ we will create a method and call it when we like to call, like on any button click.

We have set the time interval of 4 seconds. So, after 4 seconds the notification will show when the apps in the background.

Conclusion

So please follow the above step to integrate Local Notification, and if you have any issue or suggestion you can leave your query/suggestion in the comment section.

Exit mobile version