Android の通知
通知チャンネルの管理
Android 8.0 以降、通知はすべて、通知チャンネルに割り当てなければならなくなりました。Unity Mobile Notifications パッケージには、通知チャンネルを管理するための一連の API が用意されています。以下の例は、通知チャンネルの作成方法を示しています。
var channel = new AndroidNotificationChannel()
{
Id = "channel_id",
Name = "Default Channel",
Importance = Importance.Default,
Description = "Generic notifications",
};
AndroidNotificationCenter.RegisterNotificationChannel(channel);
設定できる他のプロパティの詳細については、AndroidNotificationChannel を参照してください。
通知チャンネルに対しては、取得や削除など、他のアクションを実行することもできます。通知チャンネルに関連する API の詳細については、AndroidNotificationCenter を参照してください。
通知チャンネルの作成後、チャンネルの動作を変更することはできません。詳細については、通知チャンネルの作成と管理 に関する Android デベロッパードキュメントを参照してください。
Android 8.0 より前のバージョンのデバイスでは、このパッケージは Importance
などの通知チャンネルプロパティを個々の通知に適用して同じ動作をエミュレートします。
通知時刻を正確にスケジュール
Android 6.0 以前では、通知をおおよその時刻にしかスケジュールできません。
Android 12.0 (API レベル 31) 以降で正確なスケジューリングを有効にするには、android.permission.SCHEDULE_EXACT_ALARM アクセス許可をマニフェストに追加する必要があります。ドキュメント を参照してください。
Android 13.0 (API レベル 33) 以降では、正確なスケジューリングを有効にするために、代わりに android.permission.USE_EXACT_ALARM アクセス許可も使用できます。
Android では、電池の消費が激しくなることを理由に、正確なスケジューリングの使用は推奨されていません。Unity の通知設定を使用すると、先述のアクセス許可を自動的に追加することも、常におおよその時刻でのスケジューリングを使用するようにすることもできます。
通知配信アクセス許可のリクエスト
Android 13.0 (API レベル 33) 以降では、ユーザーの許可がないと通知を配信できません。通知をスケジュールすることはできますが、ユーザーに UI が表示されないので認識されません。許可をリクエストするには、コルーチンで以下のメソッドを実行します。
IEnumerator RequestNotificationPermission()
{
var request = new PermissionRequest();
while (request.Status == PermissionStatus.RequestPending)
yield return null;
// ここで request.Status を使用してユーザーのレスポンスを判断
}
通知の管理
このパッケージには、通知を管理するための一式の API が用意されています。これらの API により、通知の送信、更新、削除といったアクションが実行可能になります。通知に関連する API の詳細については、AndroidNotificationCenter を参照してください。
シンプルな通知の送信
以下の例は、前の手順で作成した通知チャンネルでシンプルなテキスト通知をスケジュールする方法を示しています。
var notification = new AndroidNotification();
notification.Title = "Your Title";
notification.Text = "Your Text";
notification.FireTime = System.DateTime.Now.AddMinutes(1);
AndroidNotificationCenter.SendNotification(notification, "channel_id");
設定できる他のプロパティの詳細については、AndroidNotification を参照してください。
アイコンの設定
小アイコンにカスタムアイコンを設定して、各通知に表示できます。小アイコンを何も指定しない場合、通知にはデフォルトのアプリケーションアイコンが代わりに表示されます。必要に応じて大アイコンを設定して、通知ドロワーに表示することもできます。これらのアイコンは、通知設定で設定できます。詳細については、通知設定 を参照してください。
以下の例は、通知設定で設定したアイコン ID を使用して小アイコンと大アイコンを設定する方法を示しています。
notification.SmallIcon = "my_custom_icon_id";
notification.LargeIcon = "my_custom_large_icon_id";
通知 ID
Unity では通常、通知のスケジュール設定後、各通知に対して固有の ID が生成されます。以下の例は、生成された通知 ID を取得する方法を示しています。
var id = AndroidNotificationCenter.SendNotification(notification, "channel_id");
この ID を使用して、通知の追跡、キャンセル、更新を行うことができます。以下の例は、通知ステータスを確認し、確認結果に応じてアクションを実行する方法を示しています。通知ステータスの追跡は、Android 6.0 Marshmallow 以降でのみ可能です。
var notificationStatus = AndroidNotificationCenter.CheckScheduledNotificationStatus(id);
if (notificationStatus == NotificationStatus.Scheduled)
{
// スケジュールされた通知を新しい通知に置き換える
AndroidNotificationCenter.UpdateScheduledNotification(id, newNotification, "channel_id");
}
else if (notificationStatus == NotificationStatus.Delivered)
{
// 過去に表示された通知をステータスバーから削除する
AndroidNotificationCenter.CancelNotification(id);
}
else if (notificationStatus == NotificationStatus.Unknown)
{
AndroidNotificationCenter.SendNotification(newNotification, "channel_id");
}
独自の通知 ID を明示的に設定することもできます。
var notificationID = 10000;
AndroidNotificationCenter.SendNotificationWithExplicitID(notification, "channel_id", notificationId);
この API を使用して、同じ ID を持つ配信済みの通知を更新できます。
通知受信コールバック
AndroidNotificationCenter.OnNotificationReceived イベントをサブスクライブすると、アプリケーションの実行中に通知が配信された後にコールバックを受け取ることができます。
AndroidNotificationCenter.NotificationReceivedCallback receivedNotificationHandler =
delegate(AndroidNotificationIntentData data)
{
var msg = "Notification received : " + data.Id + "\n";
msg += "\n Notification received: ";
msg += "\n .Title: " + data.Notification.Title;
msg += "\n .Body: " + data.Notification.Text;
msg += "\n .Channel: " + data.Channel;
Debug.Log(msg);
};
AndroidNotificationCenter.OnNotificationReceived += receivedNotificationHandler;
カスタムデータの格納と取得
AndroidNotification.IntentData を使用して通知に任意の文字列データを格納し、ユーザーが通知をタップしてアプリケーションを開いたときにそのデータを取得することができます。
var notification = new AndroidNotification();
notification.IntentData = "{\"title\": \"Notification 1\", \"data\": \"200\"}";
AndroidNotificationCenter.SendNotification(notification, "channel_id");
ユーザーが通知をタップしてアプリケーションを開いたときに、以下の例のように、通知とその通知に割り当てられたデータを取得できます。
var notificationIntentData = AndroidNotificationCenter.GetLastNotificationIntent();
if (notificationIntentData != null)
{
var id = notificationIntentData.Id;
var channel = notificationIntentData.Channel;
var notification = notificationIntentData.Notification;
}
他の方法でアプリケーションを開いた場合、AndroidNotificationCenter.GetLastNotificationIntent は null を返します。