미디어 앱에 알림 추가

오디오 또는 동영상을 처리하는 미디어 앱을 빌드할 때는 올바른 알림 및 알림 채널을 사용하는 것이 중요합니다. 이렇게 하면 알림에 다음과 같은 중요한 기능이 포함됩니다.

  • 알림 우선순위가 있음
  • 닫을 수 없음
  • 벨소리에 오디오 속성 사용

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

수신 전화 중에 사용자 기기가 잠긴 사용 사례를 해결하려면 전체 화면 알림을 사용하여 사용자가 전화를 받을 수 있는 활동을 표시합니다.

// 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를 사용하여 통화 알림을 다른 유형의 알림과 구별하는 방법은 통화 앱의 통화 스타일 알림 만들기를 참고하세요.