构建处理音频或视频的媒体应用时,请务必使用正确的通知和通知渠道。这可确保通知具有以下重要功能:
- 具有通知优先级
- 不可关闭
- 为铃声使用音频属性
使用 NotificationChannel.Builder 设置两个通知渠道:一个用于来电,另一个用于正在进行的通话。
internal companion object { const val TELECOM_NOTIFICATION_ID = 200 const val TELECOM_NOTIFICATION_ACTION = "telecom_action" const val TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID = "telecom_incoming_channel" const val TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID = "telecom_ongoing_channel" private val ringToneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE) }
如需在所有位置显示通知并允许其播放铃声音频,请将来电通知渠道的重要程度设置为高。
val incomingChannel = NotificationChannelCompat.Builder( TELECOM_NOTIFICATION_INCOMING_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_HIGH, ).setName("Incoming calls") .setDescription("Handles the notifications when receiving a call") .setVibrationEnabled(true).setSound( ringToneUri, AudioAttributes.Builder() .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setLegacyStreamType(AudioManager.STREAM_RING) .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build(), ).build()
只有正在进行的通话需要将重要程度设置为默认。使用以下来电样式,让来电通知不可关闭。
val ongoingChannel = NotificationChannelCompat.Builder( TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_DEFAULT, ) .setName("Ongoing calls") .setDescription("Displays the ongoing call notifications") .build()
为了应对来电期间用户设备处于锁定状态的使用场景,请使用全屏通知来显示 activity,以便用户接听来电。
// on the notification val contentIntent = PendingIntent.getActivity( /* context = */ context, /* requestCode = */ 0, /* intent = */ Intent(context, TelecomCallActivity::class.java), /* flags = */ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, )
请参阅为通话应用创建通话样式通知,了解如何使用 CallStyle 将来电提醒与其他类型的通知区分开来。