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

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

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

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

  • 接听拒接来电。
  • 挂断正在进行通话。
  • 接听挂断以进行来电过滤。

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

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

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

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

以下是针对 CallStyle 通知使用各种方法的示例。

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 框架概览