会社のLTで話した時のメモをQiitaに書いておきます。
実装方法の概要のみ記載しているので、詳細は参考リンクをご覧ください。
一番大切な変更点
通知センターの機能が強化され、ユーザーが不要に感じた通知を気軽にオフにできるようになりました。
ユーザーにとってより有益な通知となるよう、この機会に改めて通知を見直しましょう。
新機能
通知のグループ化
通知がグループ化されるようになりました。
デフォルトではアプリごとにグループ化されますが、どのようにグループ化するかをアプリ側から指定できることができます。
たとえばメッセンジャーアプリの場合、送信元別に分けてグルーピングが可能です。
以下が詳しいです(ソースコードはこのサイトから引用しています)
http://ashishkakkad.com/2018/08/tutorial-lets-take-quick-dive-grouped-notifications/
https://dev.classmethod.jp/smartphone/iphone/user-notifications-grouped-local-notifications/
基本
通知に含まれる content.threadIdentifier
を指定すると、文字列が同じものがグルーピングされます。
let content = UNMutableNotificationContent()content.title = "Notifications Group"
content.body = "Tutorial by Ashish Kakkad"
content.threadIdentifier = "notify-team-ios"
応用
ほか3件の通知
の部分を指定することができます(カテゴリの概要)。
1. 通知のカテゴリを作成する
let summaryFormat = "%u more messages from %@"
return UNNotificationCategory(identifier: "group-messages", actions: [], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: nil, categorySummaryFormat: summaryFormat, options: [])
%u
にはグルーピングされた通知の数(または content.summaryArgumentCount
で設定した値)が、 %@
には content.summaryArgument
が表示されます
2. 通知センターにそのカテゴリを入れる
UNUserNotificationCenter.current().setNotificationCategories([category])
3. カテゴリを設定した UNUserNotificationCenter
を使って通知を送信
このとき、カテゴリ引数(content.summaryArgument
)を指定します
let content = UNMutableNotificationContent()
content.body = "…"
content.threadIdentifier = "notify-team-ios"
content.summaryArgument = "by Ashish"
content.summaryArgumentCount = 2
UNUserNotificationCenter.current().add(content)
通知許可ダイアログを出さずに初回通知を送る
以下が詳しいです:
https://qiita.com/peka2/items/d2ce23c078fd35a92f15
UNUserNotificationCenter.current.requestAuthorization(option:completion:)
のoption
配列に .provisional
を含めると、ダイアログを出さずに通知を送ることができます。
ただし、この通知は通知センターのみに配信されるため、ロック画面などでは確認できません。
このオプションを追加しておくと、クロージャに渡される granted が初回起動でかならず true
になります。
配信サーバーに端末IDの登録などを行っている場合、端末数が増加することがあるので注意が必要です。
設定アプリからアプリの設定画面へリンク
以下が詳しいです:
https://dev.classmethod.jp/smartphone/iphone/user-notifications-open-inapp-settings/
設定アプリの通知設定画面に、アプリ内の通知設定画面へのディープリンクを仕込めるようになりました。
通知をカテゴリごとにオンオフできるようにしておくと、ここからアプリの設定画面を立ち上げることが可能です。
UNUserNotificationCenter.current.requestAuthorization(option:completion:)
のoption
に .providesAppNotificationSettings
を含めると、リンクを出すことができます。
起動時にUNUserNotificationCenter.current().delegate
を指定すると、デリゲートのuserNotificationCenter(_:openSettingsFor:)
が呼ばれますので、この中でアプリ内の設定画面へ遷移する処理を記述します。