what's new in user notifications framework - wwdc16. meetup @wantedly with...

36
What’s New in User Notifications Framework Masayuki Ono WWDC16. Meetup @Wantedly with

Upload: -

Post on 12-Feb-2017

3.599 views

Category:

Software


2 download

TRANSCRIPT

Page 1: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

What’s New in User Notifications Framework

Masayuki Ono WWDC16. Meetup @Wantedly with

Page 3: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notifications Sessions, WWDC 2016

Session 707: Introduction to Notifications

Session 708: Advanced Notifications

(Session 724: What's New in the Apple Push Notification Service)

tvOS: Session 206: What's New in tvOS watchOS: Session 211: Quick Interaction Techniques for watchOS

Page 4: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

User Notifications frameworkSingle Notification API (UserNotifications Framework)

• Across platforms (iOS, watchOS, tvOS) • For local and remote notifications

Local notifications improvement

Notification management

In-app presentation option

Notification Extensions • Media attachments • Custom UI

Page 5: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

User Notifications frameworkSingle Notification API (UserNotifications Framework)

• Across platforms (iOS, watchOS, tvOS)• For local and remote notifications

Local notifications improvement

Notification management

In-app presentation option

Notification Extensions • Media attachments • Custom UI

Page 6: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

- Existing support for forwarded notifications - Local Notifications on the watch

- Support to badge app icons

Single Notification API across platforms

Page 7: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

iOS Notifications API HistoryAPI Feature Image

iOS 7UIKit

(registerForRemoteNotificationTypes:, etc.)

- Visual alert - App icon badging - Sound and vibration - Background/Silent

notification (iOS 7)

iOS 8/9UIKit

(registerUserNotificationSettings:, registerForRemoteNotifications, etc.)

- Notification Actions (iOS 8) - Text Input Action(iOS 9)

iOS 10 UserNotifications(UI)

- Local notifications improvement

- Notification management - In-app presentation option - Notification Extensions

Page 8: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Local Notifications Remote Notifications

Different APIs & callbacks

UserNotifications Framework

Page 9: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Local Notifications

UNTimeIntervalNotificationTrigger

UNCalendarNotificationTrigger

UNLocationNotificationTrigger

Page 10: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Local Notifications

// Create content let content = UNMutableNotificationContent() content.title = "title" content.body = "body" content.sound = UNNotificationSound.default() // Create trigger let trigger = UNTimeIntervalNotificationTrigger(timeInterval:5, repeats: false) let identifier = "time_interval_\(NSDate())" // Create request with content and trigger let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger) // Add to notification center let center = UNUserNotificationCenter.current() center.add(request) { error in print("error: \(error)") }

Page 11: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Handling

extension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is opened by notification func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { completionHandler() } // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { } }

Page 12: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Handling

extension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is opened by notification func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { completionHandler() } // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { } }

Page 13: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Handling

extension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is opened by notification func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { completionHandler() } // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { } }

Page 14: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Handlingextension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { if let trigger = notification.request.trigger { switch trigger { case let n as UNPushNotificationTrigger: print("UNPushNotificationTrigger: \(n)") case let n as UNTimeIntervalNotificationTrigger: print("UNTimeIntervalNotificationTrigger: \(n)") case let n as UNCalendarNotificationTrigger: print("UNCalendarNotificationTrigger: \(n)") case let n as UNLocationNotificationTrigger: print("UNLocationNotificationTrigger: \(n)") default: assert(false) break } } }

Trigger

Page 15: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

In-app presentation option

extension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { } }

Page 16: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

In-app presentation option

extension AppDelegate: UNUserNotificationCenterDelegate { // Called when the application is in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { completionHandler([.badge, .alert, .sound]) } }

Page 17: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification management

Access • Pending Notifications • Delivered Notifications

Remove Notifications

Update and promote Notifications

Page 18: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Remove delivered notification by identifier

Local Notifications Identifier • Set on Notification Request

let center = UNUserNotificationCenter.current() center.removeDeliveredNotifications(withIdentifiers: [“SOME_IDENTIFIER"])

Remote Notifications Identifier • HTTP/2 request header: apns-collapse-id

Page 19: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Actions

(iOS 8) Button with customizable title

(iOS 9) Text input action

Page 20: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Actions

Page 21: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Actions

// Configure categories let center = UNUserNotificationCenter.current() center.delegate = self let action = UNNotificationAction(identifier: "reply", title: "Reply", options: []) let category = UNNotificationCategory(identifier: "message”, actions: [action], minimalActions: [action], intentIdentifiers: [], options: []) center.setNotificationCategories([category])

// Specify category let content = UNMutableNotificationContent() content.title = "title" content.body = "body" content.sound = UNNotificationSound.default() content.categoryIdentifier = "message"

Page 22: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

// Configure categories let center = UNUserNotificationCenter.current() center.delegate = self let action = UNNotificationAction(identifier: "reply", title: "Reply", options: []) let category = UNNotificationCategory(identifier: "message”, actions: [action], minimalActions: [action], intentIdentifiers: [], options: [.customDismissAction]) center.setNotificationCategories([category])

// Specify category let content = UNMutableNotificationContent() content.title = "title" content.body = "body" content.sound = UNNotificationSound.default() content.categoryIdentifier = "message"

Notification Actions

Page 23: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Actions Handling

// iOS 8/9 func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: () -> Void) { completionHandler() }

// iOS 10: func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) { let identifier = response.actionIdentifier completionHandler() }

Page 24: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Service Extension

Notification Content Extension

Page 25: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Service ExtensionNon UI iOS Extension Augment or Replace the content of visible Remote Notifications

Extension

Page 26: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Service ExtensionEnd-to-end encryption Media Attachments

Extension

Page 27: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Media Attachments

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

Page 28: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Service Extensionclass NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? // Called when the notification recieved override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler:(UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) if let bestAttemptContent = bestAttemptContent { // Modify content bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" contentHandler(bestAttemptContent) } } // Called when the extension time will expire override func serviceExtensionTimeWillExpire() { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } }

Page 29: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Service Extensionclass NotificationService: UNNotificationServiceExtension { var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent? // Called when the notification recieved override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler:(UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) if let bestAttemptContent = bestAttemptContent { // Modify content bestAttemptContent.title = "\(bestAttemptContent.title) [modified]" contentHandler(bestAttemptContent) } } // Called when the extension time will expire override func serviceExtensionTimeWillExpire() { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { contentHandler(bestAttemptContent) } } }

Page 30: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Content Extension

Page 31: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

Notification Content Extensionclass NotificationViewController: UIViewController, UNNotificationContentExtension { @IBOutlet var label: UILabel! override func viewDidLoad() { super.viewDidLoad() } func didReceive(_ notification: UNNotification) { self.label.text = notification.request.content.body } }

Page 32: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

84% of devices are using iOS 9. (May 9, 2016)

https://developer.apple.com/support/app-store/

Problem: Fragmentation

Page 33: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

iOS Notifications API HistoryAPI Feature Image

iOS 7UIKit

(registerForRemoteNotificationTypes:, etc.)

- Visual alert - App icon badging - Sound and vibration - Background/Silent

notification (iOS 7)

iOS 8/9UIKit

(registerUserNotificationSettings:, registerForRemoteNotifications, etc.)

- Notification Actions (iOS 8) - Text Input Action(iOS 9)

iOS 10 UserNotifications(UI)

- Local notifications improvement

- Notification management - In-app presentation option - Notification Extensions

Page 34: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

How to support multiple iOS versions?

Ignore new UserNotifications APIs • No code changes are required?

Wrapper LIbraries • https://github.com/square/SuperDelegate

Page 35: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

APNs Payload Format

{ "aps": { "alert": { "title": “Introduction to Notifications", "subtitle": “Session 707", "body": "Woah! These new notifications look amazing! Don’t you agree?" }, "badge": 1 } }

iOS 10Title

SubtitleBody

Page 36: What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本経済新聞社

LinksWWDC

• Session 707: Introduction to Notifications

• Session 708: Advanced Notifications

• Session 724: What's New in the Apple Push Notification Service

• Session 206: What's New in tvOS (tvOS)

• Session 211: Quick Interaction Techniques for watchOS (watchOS)

Reference

• UserNotificaitons

• UserNotificationsUI

• UNNotificationServiceExtension

• UNNotificationContentExtension

Articles

• iOS User Notifications framework | | Developers.IO