Local notifications are a powerful tool for keeping users engaged with your app even when they are not actively using it. In this guide, we’ll walk you through the process of creating local notifications in Swift 5.
Step 1: Request Permission
Before you can create and schedule local notifications, you need to request permission from the user. This is done using the UNUserNotificationCenter
class, which is part of the User Notifications framework.
To request permission, add the following code to your app delegate’s didFinishLaunchingWithOptions
method:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in //use as per your requirement I have used .alert,.badge,.sound if granted { print("Notification permission granted") } else { print("Notification permission denied: \(error?.localizedDescription ?? "")") } }
This code requests permission to display alerts, update the app icon badge, and play sounds. You can adjust the options based on your app’s needs.
Step 2: Create a Notification Content
Once you have permission to display notifications, you can create a notification content using the UNMutableNotificationContent
class. This class represents the content of the notification, including the title, body, and any associated data.
Here’s an example of how to create a simple notification content:
let content = UNMutableNotificationContent() content.title = "My App" content.body = "Don't forget to open my app today!"
Step 3: Create a Notification Trigger
To schedule a local notification, you need to create a notification trigger. A trigger specifies when the notification should be delivered, such as at a specific date and time, or after a certain amount of time has passed.
Here’s an example of how to create a trigger that delivers the notification after 24 hours:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 24 * 60 * 60, repeats: false)
Step 4: Create a Notification Request
Now that you have a notification content and trigger, you can create a notification request using the UNNotificationRequest
class. This class represents a request to display a local notification at a specified time.
Here’s an example of how to create a notification request:
let request = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)
The identifier
parameter is a unique identifier for the notification. You can use this identifier to update or remove the notification later.
Step 5: Schedule the Notification
To schedule the notification request, add it to the notification center using the add
method:
UNUserNotificationCenter.current().add(request) { error in if error != nil { print("Error scheduling notification: \(error!.localizedDescription)") } else { print("Notification scheduled successfully") } }
Step 6: Handle User Interaction
When a local notification is delivered, the user can interact with it by tapping on it or swiping it. To handle these interactions, implement the userNotificationCenter(_:didReceive:withCompletionHandler:)
method in your app delegate:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // Handle the user's response completionHandler() }
In this method, you can check the response
parameter to determine what action the user took.
Local notifications are a powerful tool for engaging users with your app. By following the steps outlined in this guide, you can easily create local notifications