notifications in ios10
TRANSCRIPT
No#fica#ons in iOS102016/07/21@TachibanaKaoru
自己紹介フリーランスのiOSエンジニアをしています。(2016年8月から!)
• Twi%er: @TachibanaKaoru
• Blog : h%p://www.toyship.org/
• ボルダリグン好き
What's new in No.fica.on of iOS
• 通知に"tle/sub"tleがつけられるようになった
• 通知にイメージや画像がつけられるようになった
• 通知にCustom UIがつけられるようになった
• ユーザーが通知を許可したかどうかの設定がとれるようになった
• 配信・未配信の通知を簡単にハンドリングできるようになった
配信・未配信の通知を簡単にハンドリングできるようになった
Local No(fica(onとRemote No(fica(onが統一されました。
Remote No(fica(on {"aps" : { "alert" : { "title" : "Introduction to Notifications", "subtitle" : "Session 707", "body" : "Woah! These new notifications look amazing!"},"badge" : 1 },}
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)
通知の管理まず、通知の状態には下記の3つがあります。• Pending : まだ発火していない通知
• Delivered : 発火したが、ユーザーが処理(タップなど)をしていない通知
• (Read) : ユーザーが処理をした通知 (この状態の通知は取得できません。)
通知の状態遷移• 登録された通知は、Pending状態となる
• トリガーイベント(時間や位置など)により発火してDelivered状態となる
• ユーザーが処理をするとReadとなり、UNUserNo2fica2onCenterからは取得できなくなる
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)")
} }
delete pending no+fica+ons//特定のpending通知を消すUNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["NewsUpdate","NewsDelivery"])
//全部のpending通知を消すUNUserNotificationCenter.current().removeAllPendingNotificationRequests()
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 }
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)")
}
}
delete delivered no+fica+ons //特定のdeliverされた通知を消す UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["NewsUpdate","NewsDelivery"])
//全部のdeliverされた通知を消す UNUserNotificationCenter.current().removeAllDeliveredNotifications()
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])// これをいれておくと、システムの通知画面をだしてくれる }
iOS9とiOS10の通知ハンドリングの違い• Fav通知
• iOS9までは、Favされるたびに通知がくるので、通知欄が特定のtweetのFav通知でうまってしまった
• iOS10では、Favされるたびに通知を更新するため、特定のtweetのFav通知は常に一つ
iOS9とiOS10の通知ハンドリングの違い• コメント通知
• iOS9までは、コメントがつくたびに通知を送っていたため、もりあがっているスレッドのコメント通知でうまってしまった
• iOS10では、コメントがつくたびに通知を更新するため、特定のスレッドの通知は常に一つ
まとめ• iOS10では通知の内容を変更することができるようになったため、柔軟な通知設計ができるようになりました
• 無駄な通知をふやさず、すでにある通知を更新しよう
• アプリ内通知で、システム通知画面を表示しよう