Ajouter des notifications à une application multimédia

Lorsque vous créez une application multimédia qui traite l'audio ou la vidéo, il est important d'utiliser les notifications et canaux de notification appropriés. Cela garantit que les notifications disposent des fonctionnalités intéressantes suivantes:

  • ont une priorité de notification ;
  • ne peuvent pas être ignorées ;
  • Utiliser des attributs audio pour les sonneries

Utilisez NotificationChannel.Builder pour configurer deux canaux de notification: un pour les appels entrants et l'autre pour les appels actifs.

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)
}

Pour afficher la notification partout et lui permettre de lire le contenu audio de la sonnerie, définissez l'importance du canal de notification entrante sur élevée.

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()

Seuls les appels actifs nécessitent que l'importance soit définie sur la valeur par défaut. Utilisez le style d'appel entrant suivant pour permettre de ne pas ignorer les notifications d'appels entrants.

val ongoingChannel = NotificationChannelCompat.Builder(
        TELECOM_NOTIFICATION_ONGOING_CHANNEL_ID,
        NotificationManagerCompat.IMPORTANCE_DEFAULT,
    )
    .setName("Ongoing calls")
    .setDescription("Displays the ongoing call notifications")
    .build()

Pour les cas d'utilisation où l'appareil de l'utilisateur est verrouillé lors d'un appel entrant, utilisez une notification en plein écran pour afficher une activité permettant à l'utilisateur de répondre à l'appel.

// 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,
)

Consultez Créer une notification de style d'appel pour les applications d'appel pour savoir comment utiliser CallStyle afin de distinguer les notifications d'appel des autres types de notifications.