通話アプリ向けの通話スタイル通知を作成する

Android 12.0(API レベル 31)以降では、着信通知を他の種類の通知と区別するために、CallStyle 通知テンプレートが用意されています。着信通知または通話中通知を作成するには、このテンプレートを使用します。このテンプレートは、発信者の情報や必要なアクション(通話の応答や拒否など)を含む大きな形式の通知をサポートしています。

着信と進行中の通話は優先度の高いイベントであるため、これらの通知は通知シェードで最優先されます。また、このランク付けにより、システムは、こうした優先呼び出しを他のデバイスに転送することもできます。

CallStyle 通知テンプレートには、次の必要なアクションが含まれています。

  • 着信があった場合に [応答] または [拒否]。
  • 進行中の通話がある場合は、電話を切ってください。
  • [応答] または [電話を切って] を選択して通話スクリーニングを行います。

このスタイルのアクションはボタンとして表示され、適切なアイコンとテキストが自動的に追加されます。ボタンの手動ラベル付けはサポートされていません。通知の設計原則について詳しくは、通知をご覧ください。

ラベル付きのボタンを使用した通話スタイルの通知
図 1. 着信および進行中の通話用の CallStyle テンプレート

必要なアクションは、次のセクションの hangupIntentanswerIntent などのインテントとして渡されます。それぞれが、システムによって管理されているトークンへの参照です。トークンは、さまざまなアプリやプロセス間で渡すことができる軽量のオブジェクトです。トークンの存続期間を管理し、トークンを作成したアプリが実行されなくても PendingIntent を使用できるようにする役割はシステムにあります。別のアプリに PendingIntent を付与すると、拒否や応答など、指定した処理を実行する権限を別のアプリに付与することになります。この権限は、インテントを作成したアプリが現在実行されていない場合でも付与されます。詳細については、PendingIntent のリファレンス ドキュメントをご覧ください。

Android 14(API レベル 34)以降では、着信通知を非表示にできないように構成できます。これを行うには、Notification.FLAG_ONGOING_EVENT から Notification.Builder#setOngoing(true)CallStyle 通知を使用します。

CallStlye 通知でさまざまなメソッドを使用する例を以下に示します。

Kotlin

// Create a new call, setting the user as the caller.
val incomingCaller = Person.Builder()
    .setName("Jane Doe")
    .setImportant(true)
    .build()

Java

// Create a new call with the user as the caller.
Person incomingCaller = new Person.Builder()
    .setName("Jane Doe")
    .setImportant(true)
    .build();

着信

forIncomingCall() メソッドを使用して、着信の通話スタイル通知を作成します。

Kotlin

// Create a call style notification for an incoming call.
val builder = Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
         Notification.CallStyle.forIncomingCall(caller, declineIntent, answerIntent))
    .addPerson(incomingCaller)

Java

// Create a call style notification for an incoming call.
Notification.Builder builder = Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
        Notification.CallStyle.forIncomingCall(caller, declineIntent, answerIntent))
    .addPerson(incomingCaller);

通話中

進行中の通話の通話スタイル通知を作成するには、forOngoingCall() メソッドを使用します。

Kotlin

// Create a call style notification for an ongoing call.
val builder = Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
         Notification.CallStyle.forOngoingCall(caller, hangupIntent))
    .addPerson(second_caller)

Java

// Create a call style notification for an ongoing call.
Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
        Notification.CallStyle.forOngoingCall(caller, hangupIntent))
    .addPerson(second_caller);

通話をスクリーニングする

通話をスクリーニングする通話スタイル通知を作成するには、forScreeningCall() メソッドを使用します。

Kotlin

// Create a call style notification for screening a call.
val builder = Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
         Notification.CallStyle.forScreeningCall(caller, hangupIntent, answerIntent))
    .addPerson(second_caller)

Java

// Create a call style notification for screening a call.
Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID)
    .setContentIntent(contentIntent)
    .setSmallIcon(smallIcon)
    .setStyle(
        Notification.CallStyle.forScreeningCall(caller, hangupIntent, answerIntent))
    .addPerson(second_caller);

より多くの Android バージョン間で互換性を提供する

API バージョン 30 以前では、CallStyle 通知にフォアグラウンド サービスを関連付けて、API レベル 31 以降で与えられている高ランクを割り当てます。また、API バージョン 30 以前の CallStyle 通知でも、setColorized() メソッドを使用して通知を色付きとしてマークすることで、同様のランキングを実現できます。

CallStyle 通知で Telecom API を使用します。詳細については、通信フレームワークの概要をご覧ください。