为通话应用创建通话样式通知

在 Android 12.0(API 级别 31)及更高版本中,系统提供 CallStyle 通知模板,以将来电通知与其他类型的通知区分开来。使用此模板可以创建来电或正在进行的通话通知。此模板支持大格式通知,其中包含来电信息和所需的操作(如接听或拒接来电)。

由于来电和正在进行的通话属于高优先级事件,因此这些通知在通知栏中会获得最高优先级。此排名还使系统能够将这些优先级较高的通话转接到其他设备。

CallStyle 通知模板包含以下必需操作:

  • 接听拒接来电。
  • 挂断当前通话。
  • 接听挂断电话以过滤来电。

采用此样式的操作会显示为按钮,系统会自动添加适当的图标和文本。不支持手动为按钮添加标签。如需详细了解通知设计原则,请参阅通知

带有带标签的按钮的通话样式通知
图 1. 适用于来电和正在进行的通话的 CallStyle 模板。

所需操作会以 intent 的形式传递,例如后面几部分中的 hangupIntentanswerIntent。其中每个字段都是对系统维护的令牌的引用。令牌是一个轻量级对象,可以在不同应用和进程之间传递。系统负责管理令牌的生命周期,并确保即使创建 PendingIntent 的应用不再运行,它仍然可用。如果您为另一个应用提供 PendingIntent,即表示您授予该应用执行指定操作的权限,如拒绝或接听。即使创建该 intent 的应用当前未运行,也会授予此权限。如需了解详情,请参阅 PendingIntent 的参考文档。

从 Android 14(API 级别 34)开始,您可以将来电通知配置为不可关闭。为此,请通过 Notification.Builder#setOngoing(true) 使用 Notification.FLAG_ONGOING_EVENTCallStyle 通知。

以下是针对 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() 方法将通知标记为彩色,从而达到类似排名。

将 Telecom API 与 CallStyle 通知搭配使用。如需了解详情,请参阅 Telecom 框架概览