Notification Framework
DMI Internal Tech Talk
Sokna Ly
Associate Software Engineer, iOS
Agenda
Notifications Overview
User Notifications Framework
• Registration
• Content
• Trigger
• Management
• Actions
Agenda
Service Extensions
Media Attachments
Notifications User Interface
Overview
Existing API Overview
• Different callbacks for local and remote notifications
Limited control after notifications are scheduled
• Different support across multiple platforms
• Depreciated in iOS 10
User Notifications Framework
• Familiar API
• Expanded content
• Same code path for local and remote notification handling S
• Better notification management
• In-app presentation option
• Schedule and handle notifications in extensions
User Notifications Framework
Registration
User Authorization Options
• Badging
• Sound Alerts
• Banners
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
// Code for handling goes here
}
Needed for both local and remote notifications
User Notifications Framework
Content
Title
Subtitle
Body
Media Attachment
User Notifications Framework
let content = UNMutableNotificationContent()
content.title = “This is title"
content.subtitle = “This is subtitle"
content.body = “I’m body…wahooo!”
content.badge = 1
Local Notification
Remote Notification
{
"aps": {
"alert": {
"title": "This is title",
"subtitle": "This is subtitle",
"body": "I'm body...wahooo!"
},
"badge": 1
}
}
User Notifications Framework
Trigger
Time Interval CalendarLocation Push
Trigger
Time Interval
Example
UNTimeIntervalNotificationTrigger(timeInterval: 60,
repeats: false)
• “In 1 minute from now”
• “Repeat every hour starting now”
UNTimeIntervalNotificationTrigger(timeInterval: 3600,
repeats: false)
Trigger
Calendar
Example
let dateComponents = DateComponents()
// Configure dateComponents
UNCalendarNotificationTrigger(dateMatching: dateComponents,
repeats: false)
• “2:00pm tomorrow for meeting”
• “Repeat every Weekdays at 6:00am for gym”
Trigger
Calendar
Example
let region = CLRegion()
// Configure region
UNLocationNotificationTrigger(region: region,
repeats: false)
• “When leaving home”
• “When arriving in proximity of shopping store”
User Notifications Framework
Management
Add notification request
Update and promote Notifications
Remove Notifications (Pending, Delivered)
User Notifications Framework
Actions
Buttons with customizable title
Background and foreground
Text input
iOS and watchOS
Actions
Registration
let replyAction = UNTextInputNotificationAction(identifier: "reply",
title: "Reply",
options: [],
textInputButtonTitle: "Send",
textInputPlaceholder: "Type message here...")
let seenAction = UNNotificationAction(identifier:"seen",
title:"Mark as Read",
options:[])
let dismissAction = UNNotificationAction(identifier:"dismiss",
title:"Dismiss",
options:[])
let category = UNNotificationCategory(identifier: "chat-event",
actions: [replyAction, seenAction, dismissAction],
minimalActions: [replyAction,dismissAction],
intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
Actions
Presentation
Local Notification
Remote Notification
let content = UNMutableNotificationContent()
content.categoryIdentifier = "chat-event"
{
"aps": {
"alert": "Someone chat to you.",
"category":"chat-event"
}
}
Service Extension
Basic
• Non UI iOS Extension
• Augment or Replace of content of
Remote Notifications
• End-to-end encryption
• Short execution time
• Add attachments
Service Extension
Old school
Server-side App APNs Device
New school
Server-side App APNs DeviceExtension
Service Extension
Implementation
It’s time for implementation
Media Attachments
Basic
• Local and remote notifications
• Support image, audio, video, gif
• Download in the service extension
• Limited processing time and size
Media Attachment
Implementation
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler:(UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
let content = (request.content.mutableCopy() as! UNMutableNotificationContent)
// Handle attachment downloading
let url = // file url
let attachment = try? UNNotificationAttachment(identifier: "image",
url: url,
options: nil)
content.attachments = [attachment!]
contentHandler(content)
}
Media Attachment
Push
Local Notification
Remote Notification
let content = UNMutableNotificationContent()
let imagePath = Bundle.main.pathForResource("test", ofType: ".jpg")
let attachment = try? UNNotificationAttachment(identifier: "image",
url: URL(fileURLWithPath: imagePath!),
options: nil)
content.attachments = [attachment!]
{
"aps": {
"alert": "Someone chat to you.",
“mutable-content”:1
},
“attachment-url”:”…”
}
Notification User Interface
• Notification content extension
• Custom views
• No interactions
• Respond to notification actions
Implementation
Thanks

Notification Framework