0
1

More than 1 year has passed since last update.

Unity Asset "Mobile Notifications"の使い方 (和訳)

Last updated at Posted at 2021-12-30

Unity Asset作家Gleyさんによる有料アセット達のDocumentationを和訳するシリーズです。今回は「Mobile Notifications」というアセットです。

こちらのセットにも同梱されています。

このアセットでできること

  • 通知を表示させることでゲームへのエンゲージメントを獲得できる。
  • たった1行のコードでローカル通知のスケジュールを予約できる。
  • 通知をクリックすることで開始するアプリのセッションを監視するために、自分でカスタマイズしたデバッグメッセージを返すコールバックを設定できる
  • 通知アイコンをカスタマイズできる。
  • 通知内容をカスタマイズできる。
  • デバイスの再起動に対応。
  • AndroidとiOSに対応。どちらのOSでも同じコードのままビルドできる。

初期設定

  1. Window > Package Manager > My Assets を開き、本アセット "Mobile Notifications" をインストールする。
  2. Window > Package Manager > Unity Registry を開き、Unity公式の "Mobile Notifications" パッケージをインストールする。
  3. Window > Gley > Notifications を開くと、Settingウィンドウが表示される。 image.png
  4. Select your platforms でビルドしたいプラットフォームを選択し、Saveボタンを押す。
  5. Edit > Project Settings > Mobile Notifications を開くか、今開いたSettingウィンドウで "Open Mobile Notification Settings" ボタンを押す。

Android 設定

  • デバイスを再起動した後も通知を送りたい場合は "Reschedule Notifications on Device Restart" を有効にする。
  • カスタム通知アイコンを使用したい場合は、SmallおよびLargeサイズのアイコンを選択してそれぞれリストに追加する。ここでカスタム通知アイコンを指定しない場合は、アプリアイコンが代わりに使用される。(このリストに追加しない代わりに、res/drawableフォルダに手動で保存することでも設定できる。)
  • Smallサイズは48x48pixel以上、Largeサイズは192x192pixel以上である必要がある。Smallサイズのアイコンには白pixelで絵を描画して背景を透過させたものを使用する。Largeサイズのアイコンでは色の制約はない。 image.png

iOS 設定

  • 初回アプリ起動時に「〇〇(アプリ名)は通知を送信します。よろしいですか?」ダイアログを表示したい場合は "Request Authorization on App Launch" を有効にする。 image.png

Script作成

初期化

// このメソッドは新しい通知チャンネルを作成し、現在保留中の通知をすべてキャンセルします。
// ユーザーがアプリを起動するたびに毎回呼び出されるメソッドです。
GleyNotifications.Initialize();

初期化(保留中の通知をキャンセルしない)

// 引数をfalseにすると、現在保留中のすべての通知をキャンセルさせずに初期化できます。
// これらの通知はユーザーがアプリを起動中でもおかまいなしに表示されます。
GleyNotifications.Initialize(false);

通知のスケジュール予約

GleyNotifications.SendNotification(
    string title, string text, System.TimeSpan timeDelayFromNow,
    string smallIcon = null, string largeIcon = null, string customData = "");
引数名 説明
string title 通知タイトル
string text 通知メッセージ内容
System.TimeSpan timeDelayFromNow 通知の送信を遅延させる時間。現在時刻に加算される
string smallIcon 使用したいSmallサイズのカスタム通知アイコンの名前(Identifier)
string largeIcon 使用したいLargeサイズのカスタム通知アイコンの名前(Identifier)
string customData ユーザーが通知をタップしてアプリを開いた場合に取得されるデータ

通知のスケジュール予約(反復)

GleyNotifications.SendRepeatNotification(
    string title, string text, System.TimeSpan timeDelayFromNow,
    System.TimeSpan repeatInterval, string smallIcon = null,
    string largeIcon = null, string customData = "");
引数名 説明
System.TimeSpan repeatInterval 次回の通知が送信されるまでの時間

通知がタップされたかどうかを取得する

// このメソッドは、現在のセッションが通知をタップして開かれた場合にはcustomDataを返し、それ以外の場合にはnullを返します。
string GleyNotifications.AppWasOpenFromNotification()

参考

0
1
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
0
1