708 advanced-notifications 05 finaldevstreaming.apple.com/.../708/708_advanced_notifications.pdf ·...

123
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Session 708 #WWDC16 Advanced Notifications Michele Campeotto iOS Notifications System Frameworks

Upload: dodieu

Post on 31-Jan-2018

232 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Session 708

#WWDC16

Advanced Notifications

Michele CampeottoiOS Notifications

System Frameworks

Page 2: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Agenda

Notifications User InterfaceMedia AttachmentsCustomization

Page 3: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Notifications User Interface

Page 4: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 5: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 6: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 7: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 8: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 9: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 10: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 11: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 12: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 13: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 14: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Page 15: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

Page 16: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

{ aps: { alert: { … }, mutable-content: 1 } my-attachment: "https://example.com/photo.jpg"}

Page 17: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

{ aps: { alert: { … }, mutable-content: 1 } my-attachment: "https://example.com/photo.jpg"}

Page 18: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

{ aps: { alert: { … }, mutable-content: 1 } my-attachment: "https://example.com/photo.jpg"}

Page 19: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

Service Extension

Page 20: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Push Payload

Service Extension

Page 21: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 22: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 23: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 24: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 25: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 26: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Adding an attachment to a user notification

public class NotificationService: UNNotificationServiceExtension { override public func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: (UNNotificationContent) -> Void) {

let fileURL = // ... let attachment = UNNotificationAttachment(identifier: "image", url: fileURL, options: nil) let content = request.content.mutableCopy as! UNMutableNotificationContent content.attachments = [ attachment ]

contentHandler(content) } }

Page 27: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 28: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Page 29: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notifications

Page 30: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notificationsImage, audio, video

Page 31: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notificationsImage, audio, videoDownload in the service extension

Page 32: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notificationsImage, audio, videoDownload in the service extension• Limited processing time and size

Page 33: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notificationsImage, audio, videoDownload in the service extension• Limited processing time and size

Add attachment to notification

Page 34: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Local and remote notificationsImage, audio, videoDownload in the service extension• Limited processing time and size

Add attachment to notification• File is moved and managed by the system

Page 35: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 36: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Page 37: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Page 38: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Notification content extension

Page 39: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Notification content extensionCustom views

Page 40: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Notification content extensionCustom viewsNo interaction

Page 41: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom User Interface

Notification content extensionCustom viewsNo interactionRespond to notification actions

Page 42: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 43: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 44: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 45: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 46: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 47: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 48: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Notification Content Extension

Page 49: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 50: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 51: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 52: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 53: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Minimal Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var label: UILabel?

override func viewDidLoad() {

super.viewDidLoad()

// Do any required interface initialization here.

}

func didReceive(_ notification: UNNotification) {

label?.text = notification.request.content.body

}

}

Page 54: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Minimal Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var label: UILabel?

override func viewDidLoad() {

super.viewDidLoad()

// Do any required interface initialization here.

}

func didReceive(_ notification: UNNotification) {

label?.text = notification.request.content.body

}

}

Page 55: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Minimal Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var label: UILabel?

override func viewDidLoad() {

super.viewDidLoad()

// Do any required interface initialization here.

}

func didReceive(_ notification: UNNotification) {

label?.text = notification.request.content.body

}

}

Page 56: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<plist version="1.0"> <dict>

// ...

<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> </dict> // ... </dict> </dict> </plist>

Page 57: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<plist version="1.0"> <dict>

// ...

<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> </dict> // ... </dict> </dict> </plist>

Page 58: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<plist version="1.0"> <dict>

// ...

<key>NSExtension</key> <dict> <key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <array> <string>event-invite</string> <string>event-changed</string> </array> </dict> // ... </dict> </dict> </plist>

Page 59: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom Views

Page 60: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

Page 61: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

Page 62: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

Page 63: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

Page 64: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

@IBOutlet var eventTitle: UILabel! @IBOutlet var eventDate: UILabel! @IBOutlet var eventLocation: UILabel! @IBOutlet var eventMessage: UILabel! func didReceive(_ notification: UNNotification) { let content = notification.request.content eventTitle.text = content.title eventDate.text = content.subtitle eventMessage.text = content.body if let location = content.userInfo["location"] as? String { eventLocation.text = location } } }

Page 65: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 66: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 67: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 68: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 69: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> <key>UNNotificationExtensionDefaultContentHidden</key> <true/> </dict>

Page 70: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> <key>UNNotificationExtensionDefaultContentHidden</key> <true/> </dict>

Page 71: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

override func viewDidLoad() { super.viewDidLoad()

let size = view.bounds.size preferredContentSize = CGSize(width: size.width, height: size.width / 2) }

func didReceive(_ notification: UNNotification) { // ... }

}

Page 72: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension

class NotificationViewController: UIViewController, UNNotificationContentExtension {

override func viewDidLoad() { super.viewDidLoad()

let size = view.bounds.size preferredContentSize = CGSize(width: size.width, height: size.width / 2) }

func didReceive(_ notification: UNNotification) { // ... }

}

Page 73: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 74: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> <key>UNNotificationExtensionDefaultContentHidden</key> <true/> <key>UNNotificationExtensionInitialContentSizeRatio</key> <real>0.5</real> </dict>

Page 75: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Extension Info.plist

<key>NSExtensionAttributes</key> <dict> <key>UNNotificationExtensionCategory</key> <string>event-invite</string> <key>UNNotificationExtensionDefaultContentHidden</key> <true/> <key>UNNotificationExtensionInitialContentSizeRatio</key> <real>0.5</real> </dict>

Page 76: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 77: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom Notification UI

Page 78: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom Notification UI

Presentation sizeUNNotificationExtensionInitialContentSizeRatio

Page 79: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom Notification UI

Presentation sizeUNNotificationExtensionInitialContentSizeRatio

Default contentUNNotificationExtensionDefaultContentHidden

Page 80: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Custom Notification UI

Presentation sizeUNNotificationExtensionInitialContentSizeRatio

Default contentUNNotificationExtensionDefaultContentHidden

Page 81: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Media Attachments

Page 82: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension Attachments

class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var eventImage: UIImageView!

func didReceive(_ notification: UNNotification) { let content = notification.request.content if let attachment = content.attachments.first { if attachment.url.startAccessingSecurityScopedResource() { eventImage.image = UIImage(contentsOfFile: attachment.url.path!) attachment.url.stopAccessingSecurityScopedResource() } } } }

Page 83: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension Attachments

class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var eventImage: UIImageView!

func didReceive(_ notification: UNNotification) { let content = notification.request.content if let attachment = content.attachments.first { if attachment.url.startAccessingSecurityScopedResource() { eventImage.image = UIImage(contentsOfFile: attachment.url.path!) attachment.url.stopAccessingSecurityScopedResource() } } } }

Page 84: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension Attachments

class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var eventImage: UIImageView!

func didReceive(_ notification: UNNotification) { let content = notification.request.content if let attachment = content.attachments.first { if attachment.url.startAccessingSecurityScopedResource() { eventImage.image = UIImage(contentsOfFile: attachment.url.path!) attachment.url.stopAccessingSecurityScopedResource() } } } }

Page 85: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Notification Content Extension Attachments

class NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var eventImage: UIImageView!

func didReceive(_ notification: UNNotification) { let content = notification.request.content if let attachment = content.attachments.first { if attachment.url.startAccessingSecurityScopedResource() { eventImage.image = UIImage(contentsOfFile: attachment.url.path!) attachment.url.stopAccessingSecurityScopedResource() } } } }

Page 86: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 87: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 88: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Actions

Page 89: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Default Action Handling

Page 90: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Default Action Handling

Delivered to the appNotification gets dismissed immediately

Page 91: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Intercepting Action Response

Page 92: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Intercepting Action Response

Delivered to the extensionCan delay dismissal

Page 93: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismiss) } } }

Page 94: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismiss) } } }

Page 95: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismiss) } } }

Page 96: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismiss) } } }

Page 97: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismiss) } } }

Page 98: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Intercepting notification action response

class NotificationViewController: UIViewController, UNNotificationContentExtension { func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

server.postEventResponse(response.actionIdentifier) { if response.actionIdentifier == "accept" { eventResponse.text = "Going!" eventResponse.textColor = UIColor.green() } else if response.actionIdentifier == "decline" { eventResponse.text = "Not going :(" eventResponse.textColor = UIColor.red() }

done(.dismissAndForwardAction) } } }

Page 99: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 100: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Text Input Action

Page 101: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text Input Action

private func makeEventExtensionCategory() -> UNNotificationCategory { let commentAction = UNTextInputNotificationAction( identifier: "comment", title: "Comment", options: [], textInputButtonTitle: "Send", textInputPlaceholder: "Type here…")

return UNNotificationCategory(identifier: "event-invite", actions: [ acceptAction, declineAction, commentAction ], minimalActions: [ acceptAction, declineAction ], intentIdentifiers: [], options: []) }

Page 102: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text Input Action

private func makeEventExtensionCategory() -> UNNotificationCategory { let commentAction = UNTextInputNotificationAction( identifier: "comment", title: "Comment", options: [], textInputButtonTitle: "Send", textInputPlaceholder: "Type here…")

return UNNotificationCategory(identifier: "event-invite", actions: [ acceptAction, declineAction, commentAction ], minimalActions: [ acceptAction, declineAction ], intentIdentifiers: [], options: []) }

Page 103: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text Input Action

private func makeEventExtensionCategory() -> UNNotificationCategory { let commentAction = UNTextInputNotificationAction( identifier: "comment", title: "Comment", options: [], textInputButtonTitle: "Send", textInputPlaceholder: "Type here…")

return UNNotificationCategory(identifier: "event-invite", actions: [ acceptAction, declineAction, commentAction ], minimalActions: [ acceptAction, declineAction ], intentIdentifiers: [], options: []) }

Page 104: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text Input Action

private func makeEventExtensionCategory() -> UNNotificationCategory { let commentAction = UNTextInputNotificationAction( identifier: "comment", title: "Comment", options: [], textInputButtonTitle: "Send", textInputPlaceholder: "Type here…")

return UNNotificationCategory(identifier: "event-invite", actions: [ acceptAction, declineAction, commentAction ], minimalActions: [ acceptAction, declineAction ], intentIdentifiers: [], options: []) }

Page 105: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 106: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text input action response

class NotificationViewController: UIViewController, UNNotificationContentExtension {

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse { server.send(textResponse.userText) { done(.dismiss) } } }

}

Page 107: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text input action response

class NotificationViewController: UIViewController, UNNotificationContentExtension {

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse { server.send(textResponse.userText) { done(.dismiss) } } }

}

Page 108: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text input action response

class NotificationViewController: UIViewController, UNNotificationContentExtension {

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse { server.send(textResponse.userText) { done(.dismiss) } } }

}

Page 109: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text input action response

class NotificationViewController: UIViewController, UNNotificationContentExtension {

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse { server.send(textResponse.userText) { done(.dismiss) } } }

}

Page 110: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Text input action response

class NotificationViewController: UIViewController, UNNotificationContentExtension {

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) {

if let textResponse = response as? UNTextInputNotificationResponse { server.send(textResponse.userText) { done(.dismiss) } } }

}

Page 111: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 112: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 113: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 114: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Custom input accessory view

class NotificationViewController: UIViewController, UNNotificationContentExtension { override func canBecomeFirstResponder() -> Bool { return true } override var inputAccessoryView: UIView { get { return inputView } }

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "comment" { becomeFirstResponder() textField.becomeFirstResponder() } } }

Page 115: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Custom input accessory view

class NotificationViewController: UIViewController, UNNotificationContentExtension { override func canBecomeFirstResponder() -> Bool { return true } override var inputAccessoryView: UIView { get { return inputView } }

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "comment" { becomeFirstResponder() textField.becomeFirstResponder() } } }

Page 116: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Custom input accessory view

class NotificationViewController: UIViewController, UNNotificationContentExtension { override func canBecomeFirstResponder() -> Bool { return true } override var inputAccessoryView: UIView { get { return inputView } }

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "comment" { becomeFirstResponder() textField.becomeFirstResponder() } } }

Page 117: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

// Custom input accessory view

class NotificationViewController: UIViewController, UNNotificationContentExtension { override func canBecomeFirstResponder() -> Bool { return true } override var inputAccessoryView: UIView { get { return inputView } }

func didReceive(_ response: UNNotificationResponse, completionHandler done: (UNNotificationContentExtensionResponseOption) -> Void) { if response.actionIdentifier == "comment" { becomeFirstResponder() textField.becomeFirstResponder() } } }

Page 118: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from
Page 119: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Summary

Attachments and custom UIAttachments with service extensionCustom UI with content extension• Media attachments• User interaction

Page 120: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

More Information

https://developer.apple.com/wwdc16/708

Page 121: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Related Sessions

Introduction to Notifications Pacific Heights Wednesday 9:00AM

Page 122: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from

Labs

Notifications Lab Frameworks Lab C Wednesday 11:00AM

Notifications Lab Graphics, Games, and Media Lab B Friday 9:00AM

Page 123: 708 Advanced-Notifications 05 FINALdevstreaming.apple.com/.../708/708_advanced_notifications.pdf · Redistribution or public display not permitted without written permission from