WWDC2016セッションメモ「Introduction to Notifications」
WWDC2016セッションを見たメモです。
文中の画像やコードは公開情報から引用しています。
正確で詳しい内容をお求めの方は一次情報の確認をおすすめします。
Introduction to Notifications
Introduction to Notifications - WWDC 2016 - Videos - Apple Developer
User Notifications Framework

watchOS単独でLocalNotificationが使える
Registration
使うのにRegistration が必要。
ローカルもリモートも必要。
UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in /* ... */}
ユーザ定義を設定する
UNUserNotificationCenter.current().getNotificationSettings { (settings) in /* ... */}
コンテンツ
TitleとSubtitleが追加された。
画像とかメディアも表示出来る。

Local Notification
let content = UNMutableNotificationContent() content.title = "Introduction to Notifications" content.subtitle = "Session 707" content.body = "Woah! These new notifications look amazing! Don’t you agree?" content.badge = 1
Remote Notification
{ "aps" : { "alert" : { "title" : "Introduction to Notifications", "subtitle" : "Session 707", "body" : "Woah! These new notifications look amazing! Don’t you agree?" }, "badge" : 1 }, }
トリガー
Time Interval
//2分後に発火 UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: false) //リピートするならrepeatsをtrueに。
Calendar
//「明日朝8時に」「毎週月曜日の18時に」とか指定できる。 let dateComponents = DateComponents() // dateComponentsに設定する UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
Location
//「家についたら」「アモーレと思い出の場所に近づいたら」とか指定できる。 let region = CLRegion() // ここでregionに設定する UNLocationNotificationTrigger(region: region, repeats: false)
どうやるのか

// Notification Delivery Summary import UserNotifications //許可 UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in // ... } //コンテンツ指定 let content = UNMutableNotificationContent() content.title = "Introduction to Notifications" content.body = "Let's talk about notifications!" //トリガー指定 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) //リクエスト作成 let requestIdentifier = "sampleRequest" let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger) //UNUserNotificationCenterに追加 UNUserNotificationCenter.current().add(request) { (error) in // ... }
ハンドリング
フォアグラウンドの時アプリ内表示ハンドリング
protocol UNUserNotificationCenterDelegate : NSObjectProtocol func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler handlerBlock: (UNNotificationPresentationOptions) -> Void) { // Roll banner and sound alert handlerBlock([.alert, .sound]) }
Notification Management
たくさんの通知うっとおしいよね。
最新の一件だけに更新できますよ。

保留の通知を消す
保留の通知を消すこともできますよ。
// Pending Notification Removal let gameStartIdentifier = "game1.start.identifier" let gameStartRequest = UNNotificationRequest(identifier: gameStartIdentifier, content: content, trigger: startTrigger) UNUserNotificationCenter.current().add(gameStartRequest) { (error) in // ... } // Game was cancelled UNUserNotificationCenter.current.current() .removePendingNotificationRequests(withIdentifiers: [gameStartIdentifier])
保留の通知を更新
保留の通知を更新することもできますよ。
// Pending Notification Update let gameStartIdentifier = "game1.start.identifier" let gameStartRequest = UNNotificationRequest(identifier: gameStartIdentifier, content: content, trigger: startTrigger) UNUserNotificationCenter.current().add(gameStartRequest) { (error) in // ... } // Game start time was updated let updatedGameStartRequest = UNNotificationRequest(identifier: gameStartIdentifier, content: content, trigger: newStartTrigger) UNUserNotificationCenter.current().add(updatedGameStartRequest) { (error) in // ... }
配信済みの通知を削除
// Delivered Notification Removal let gameScoreIdentifier = "game1.score.identifier" let gameScoreRequest = UNNotificationRequest(identifier: gameScoreIdentifier, content: scoreContent, trigger: trigger) UNUserNotificationCenter.current().add(gameScoreRequest) { (error) in // ... } // Wrong game score was published UNUserNotificationCenter.current() .removeDeliveredNotifications(withIdentifiers: [gameScoreIdentifier])
配信済みの通知を更新
// Delivered Notification Update let gameScoreIdentifier = "game1.score.identifier" let gameScoreRequest = UNNotificationRequest(identifier: gameScoreIdentifier, content: scoreContent, trigger: trigger) UNUserNotificationCenter.current().add(gameScoreRequest) { (error) in // ... } // Score game was updated let updateGameScoreRequest = UNNotificationRequest(identifier: gameScoreIdentifier, content: newScoreContent, trigger: newTrigger) UNUserNotificationCenter.current().add(updateGameScoreRequest) { (error) in // ... }
アクション付き通知
- ボタン付き
- 返信とかのテキスト入力
- iOSとwatchOSで使えます。

let action = UNNotificationAction(identifier:"reply",title:"Reply",options:[]) let category = UNNotificationCategory(identifier: "message", actions: [action], minimalActions: [action], intentIdentifiers: [], options: []) UNUserNotificationCenter.current().setNotificationCategories([category])
非表示アクション
通知をスワイプで消せますよ。
optionsに設定します。
customDismissAction: UNNotificationCategoryOptions let category = UNNotificationCategory(identifier: "message", actions: [action], minimalActions: [action], intentIdentifiers: [], options: [.customDismissAction])
感想
できること増えたね!
WWDC2016セッションメモシリーズはこちら
WWDC2016 カテゴリーの記事一覧 - ROADTO みちログ
「Introduction to Notifications」拝聴。出来ること増えたしツール系は嬉しい。
— ひとのみち (@hitonomichi) 2016年6月23日
ゲームにももちろん使えるけど広告になるとうざいかも。