UserNotifications
Local notifications comes in handy when application is terminated or in background state, and want to update the user with some info. These notifications can be displayed in lock screen, as banner, as alert and in notification center. Till iOS 9, Apple provided UILocalNotification class to support notifications. This framework is now deprecated in iOS 10.
Starting from iOS 10, Apple introduced a new framework, UserNotifications to support the delivery and handling of remote and local notifications.
I will be using Swift language to demonstrate the example.
In AppDelegate.swift, add the following code in didFinishLaunchingWithOptions method.
let notificationCentre = UNUserNotificationCenter.current()
notificationCentre.requestAuthorization(options: [.badge, .alert, .sound, .carplay]) {
(granted, error) in
// Enable or disable features based on authorization }
To create a local notification using UserNotification framework, add the below code in ViewController.swift
let centre = UNUserNotificationCenter.current()
centre.getNotificationSettings { (settings) in
if settings.authorizationStatus != UNAuthorizationStatus.authorized {
print("Not Authorised")
} else {
print("Authorised")
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "This is the title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "The message body goes here.", arguments: nil)
// Deliver the notification in five seconds.
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// Schedule the notification.
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
}
}
Starting from iOS 10, Apple introduced a new framework, UserNotifications to support the delivery and handling of remote and local notifications.
I will be using Swift language to demonstrate the example.
In AppDelegate.swift, add the following code in didFinishLaunchingWithOptions method.
let notificationCentre = UNUserNotificationCenter.current()
notificationCentre.requestAuthorization(options: [.badge, .alert, .sound, .carplay]) {
(granted, error) in
// Enable or disable features based on authorization }
- The above block will prompt the user to authorise the application to show notifications.
- For any application, this alert is shown only once.
- Successive times, the user will not be prompted any alert. Instead, the completion block will provide the notification access details provided by user.
- In our example, we have added four authorisation options. For more info, click here
To create a local notification using UserNotification framework, add the below code in ViewController.swift
let centre = UNUserNotificationCenter.current()
centre.getNotificationSettings { (settings) in
if settings.authorizationStatus != UNAuthorizationStatus.authorized {
print("Not Authorised")
} else {
print("Authorised")
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "This is the title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "The message body goes here.", arguments: nil)
// Deliver the notification in five seconds.
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// Schedule the notification.
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: nil)
}
}
- Get the singleton object for UNUserNotificationCenter.
- Get the notification authorisation status from Settings. Users can change this status anytime in Settings app. So before we create, call the getNotificationSettings and verify whether the application has access to display notifications.
- getNotificationSettings method has a closure with a single argument. This will provide the authorisation status.
- If the status != authorized, application does not have access to deliver notifications.
- Else case, create an object for UNMutableNotificationContent.
- Assign title, body, sound values to the object.
- Create an instance of UNNotificationRequest passing trigger date and UNMutableNotificationContent object.
- Trigger date defines the date and time when the local notification should be displayed.
- Add the UNNotificationRequest instance to UNUserNotificationCenter.
- The local notification will be displayed on the iOS device at the triggered date and time.
Comments
Post a Comment