【Unity】MobileNotificationについてメモ(iOS)

概要

MobileNotificationはローカル通知を出すためのパッケージです。

パッケージの導入で躓いたところをメモとして残します。

※ この記事ではiOSでの実行をメインとしています。

実証環境

  • OS:Mac
  • Unityバージョン:2021.2.8f1
  • Xcodeバージョン:12.5
  • iPhone機種:iPhone SE
  • iosバージョン:14.5
  • MobileNotification 1.4.3

メモ

OnNotificationReceivedのタイミング

Subscribe to this event to receive a callback whenever a local notification or a remote is shown to the user.

このイベントをサブスクライブして、ローカル通知またはリモートがユーザーに表示されるたびにコールバックを受信します。

https://docs.unity3d.com/Packages/com.unity.mobile.notifications@1.4/api/Unity.Notifications.iOS.iOSNotificationCenter.html?q=OnNotificationReceived

「iOSNotificationCenter.OnNotificationReceived」はローカル通知を受け取ったときにコールバックされます。

バックグラウンド中に受け取った場合はフォアグラウンドにした時にコールバックを受け取れますが、アプリ終了中に受け取った通知は次回アプリ起動時には検知できません。

また、通知を受け取ったことを検知するので「通知を開いた」タイミングの検知は対象外になります。

OnRemoteNotificationReceivedというイベントも存在しますが、こちらではローカル通知は検知されません。

通知にパラメータを設定する

Arbitrary string data which can be retrieved when the notification is used to open the app or is received while the app is running.

任意の文字列データこれは、通知を使用してアプリを開くとき、またはアプリの実行中に受信したときに取得できます。

https://docs.unity3d.com/Packages/com.unity.mobile.notifications@1.4/api/Unity.Notifications.iOS.iOSNotification.html?q=Data

Key-value collection sent or received with the notification. Note, that some of the other notification properties are transfered using this collection, it is not recommended to modify existing items.

通知とともに送受信されるKey-Valueコレクション。他の通知プロパティの一部はこのコレクションを使用して転送されることに注意してください。既存のアイテムを変更することはお勧めしません。

https://docs.unity3d.com/Packages/com.unity.mobile.notifications@1.4/api/Unity.Notifications.iOS.iOSNotification.html?q=UserInfo

通知に任意のパラメータを設定したい場合は「Data」「UserInfo」に設定できます。

「iOSNotificationCenter.OnNotificationReceived」や「iOSNotificationCenter.GetLastRespondedNotification()」で取得した通知情報から抜き出して使えます。

フォアグラウンド状態で通知を表示にする

Presentation options for displaying the local of notification when the app is running. Only works if ShowInForeground is enabled and user has allowed enabled the requested options for your app.

アプリの実行時に通知のローカルを表示するためのプレゼンテーションオプション。ShowInForegroundが有効になっていて、ユーザーがアプリに要求されたオプションの有効化を許可している場合にのみ機能 します。

https://docs.unity3d.com/Packages/com.unity.mobile.notifications@1.4/api/Unity.Notifications.iOS.iOSNotification.html?q=ForegroundPresentationOption

「iOSNotification.ForegroundPresentationOption」にAlertを設定することで表示できます。

フォアグラウンド状態のときは通知が表示されないのがデフォルトです。

バッジ数について

アプリに表示されるバッジ数は最後に受信した通知に設定されているBadgeの値に上書きされます。

ステータスバーから通知が消えても残るため、明示的に変更する必要があります。

バッジ数は1日ほどシステムに残るため、アンインストール時にバッジが残っていた場合は再インストール時にバッジが付いたままになります。

   

コメントを残す


CAPTCHA