Notifications in iOS 10

The new version of iOS introduces a number of changes concerning both local and server notifications. We can use a new framework, UserNotifications, which enables both delivering and handling notifications.
Handling notifications in iOS
New notifications allow you to add images (jpg, png, gif) and videos, and to perform other actions. Moreover, we can now use Notification Content Extension through which we can design the exact appearance of notification in our application.
To handle notifications in an application, follow these steps:
- Registering notifications (Register),
- Setting the trigger (Trigger),
- Creating content (Content),
- Calling the notification (Schedule).
Step 1. Registration
Compared to previous versions of iOS, there was a change in the way of their registration:
UNUserNotificationCenter.current().requestAuthorization(options:[.alert, .sound, .badge]) { (greanted, error) in //.. }
We can now check what settings user selected for notifications in the application:
Step 2. Setting up a trigger
There are four types of triggers:
- Push,
- Time Interval.
UNTimeIntercalNotificationTrigger(timeInterval: 120, repeats: false)
- Calendar,
let dateComponents = DateComponents()
// set the dateComponents here
UNCalendarNotificationTrigger( dateMatching: dateComponents, repeats: false )
- Location,
let region - CLRegion()
// set the region here
UNLocationNotificationTrigger( region: region, repeats: false )
Step 3. Creating the notification content
Notification content We now have greater room for maneuver, and thus can set the following: title, subtitle and body for notifications.
let content = UNMutableNotificationContent()
content.title = "Notification title"
content.subtitle = "Notification subtitle"
content.body = "Notification body"
content.sound = UNNotificationSound.default()
content.badge = 1
Step 4. Calling the notification
Last but not least, all we need to do is to call the notification as shown below.
let requestId = "notificationRequestIdentifier1"
let request = UNNotificationRequest(identifier: requestId, content: content, trigger: triger)
UNUserNotificationCenter.current().add(request) { error in //... }