notifications in ios10

18
No#fica#ons in iOS10 2016/07/21 @TachibanaKaoru

Upload: toyship

Post on 12-Jan-2017

106 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Notifications in iOS10

No#fica#ons in iOS102016/07/21@TachibanaKaoru

Page 2: Notifications in iOS10

自己紹介フリーランスのiOSエンジニアをしています。(2016年8月から!)

• Twi%er: @TachibanaKaoru

• Blog : h%p://www.toyship.org/

• ボルダリグン好き

Page 3: Notifications in iOS10

What's new in No.fica.on of iOS

• 通知に"tle/sub"tleがつけられるようになった

• 通知にイメージや画像がつけられるようになった

• 通知にCustom UIがつけられるようになった

• ユーザーが通知を許可したかどうかの設定がとれるようになった

• 配信・未配信の通知を簡単にハンドリングできるようになった

Page 4: Notifications in iOS10

配信・未配信の通知を簡単にハンドリングできるようになった

Page 5: Notifications in iOS10

Local No(fica(onとRemote No(fica(onが統一されました。

Page 6: Notifications in iOS10

Remote No(fica(on {"aps" : { "alert" : { "title" : "Introduction to Notifications", "subtitle" : "Session 707", "body" : "Woah! These new notifications look amazing!"},"badge" : 1 },}

Page 7: Notifications in iOS10

Local No(fica(on let center = UNUserNotificationCenter.current()

let content = UNMutableNotificationContent() content.title = "Hello" content.body = "This is a big news!" content.sound = UNNotificationSound.default()

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 10, repeats: false)

let request = UNNotificationRequest.init( identifier: "NewsUpdate", // このIDをキーにして削除や編集をします content: content, trigger: trigger)

center.add(request)

Page 8: Notifications in iOS10

通知の管理まず、通知の状態には下記の3つがあります。• Pending : まだ発火していない通知

• Delivered : 発火したが、ユーザーが処理(タップなど)をしていない通知

• (Read) : ユーザーが処理をした通知 (この状態の通知は取得できません。)

Page 9: Notifications in iOS10

通知の状態遷移• 登録された通知は、Pending状態となる

• トリガーイベント(時間や位置など)により発火してDelivered状態となる

• ユーザーが処理をするとReadとなり、UNUserNo2fica2onCenterからは取得できなくなる

Page 10: Notifications in iOS10

get pending no*fica*ons UNUserNotificationCenter.current().getPendingNotificationRequests { requests in

print("== Pending Notification : \(requests.count) == ")

for request in requests { request.identifier request.content.title request.content.subtitle request.content.body request.trigger

print(" -- title : \(request.content.title) body:\(request.content.body)")

} }

Page 11: Notifications in iOS10

delete pending no+fica+ons//特定のpending通知を消すUNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["NewsUpdate","NewsDelivery"])

//全部のpending通知を消すUNUserNotificationCenter.current().removeAllPendingNotificationRequests()

Page 12: Notifications in iOS10

update pending no,fica,ons // 新しいcontentをつくる let content = UNMutableNotificationContent() content.title = "Hello" content.body = "This is a more big news!" content.sound = UNNotificationSound.default() let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)

// 新しいrequestをつくる let newRequest = UNNotificationRequest.init(identifier: "NewsUpdate", content: content, trigger: trigger)

// 新しいrequestをつくる(古いものをdeleteする必要はない) UNUserNotificationCenter.current().add(newRequest) { error in // error }

Page 13: Notifications in iOS10

get delivered no,fica,ons UNUserNotificationCenter.current().getDeliveredNotifications { notifications in // [UNNotification])

print("== Delivred Notification : \(notifications.count) == ")

for notification in notifications { notification.date notification.request

print(" -- title : \(notification.request.content.title) body:\(notification.request.content.body) date: \(notification.date)")

}

}

Page 14: Notifications in iOS10

delete delivered no+fica+ons //特定のdeliverされた通知を消す UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["NewsUpdate","NewsDelivery"])

//全部のdeliverされた通知を消す UNUserNotificationCenter.current().removeAllDeliveredNotifications()

Page 15: Notifications in iOS10

No#fica#on in ac#on• アプリが Foregroundにある状態で通知が配信された場合に、システムの通知画面をだせるようになりました。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

print("! Notification came!\(notification.request.content.title)")

completionHandler([.alert, .sound])// これをいれておくと、システムの通知画面をだしてくれる }

Page 16: Notifications in iOS10

iOS9とiOS10の通知ハンドリングの違い• Fav通知

• iOS9までは、Favされるたびに通知がくるので、通知欄が特定のtweetのFav通知でうまってしまった

• iOS10では、Favされるたびに通知を更新するため、特定のtweetのFav通知は常に一つ

Page 17: Notifications in iOS10

iOS9とiOS10の通知ハンドリングの違い• コメント通知

• iOS9までは、コメントがつくたびに通知を送っていたため、もりあがっているスレッドのコメント通知でうまってしまった

• iOS10では、コメントがつくたびに通知を更新するため、特定のスレッドの通知は常に一つ

Page 18: Notifications in iOS10

まとめ• iOS10では通知の内容を変更することができるようになったため、柔軟な通知設計ができるようになりました

• 無駄な通知をふやさず、すでにある通知を更新しよう

• アプリ内通知で、システム通知画面を表示しよう