20
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

今からiOS12 SDKへ対応する人のためのプッシュ通知変更点まとめ

Last updated at Posted at 2018-10-23

会社の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:)が呼ばれますので、この中でアプリ内の設定画面へ遷移する処理を記述します。

20
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?