알림 그룹 만들기

Android 7.0 (API 수준 24)부터 관련 알림을 그룹에 표시할 수 있습니다. 예를 들어 앱에서 수신된 이메일의 알림을 표시하는 경우 새 이메일 메시지의 모든 알림을 동일한 그룹에 배치하여 함께 축소합니다.

이전 버전을 지원하려면 모든 개별 알림을 요약하기 위해 단독으로 표시되는 요약 알림을 추가합니다. 주로 받은편지함 스타일 알림을 사용할 때 가장 좋습니다.

그림 1. 축소된 (위쪽) 알림 그룹 및 펼쳐진 (하단) 알림 그룹.

사용 사례에서 다음 조건이 모두 충족되면 알림 그룹을 사용하세요.

  • 하위 알림은 완전한 알림이며 그룹 요약 없이 개별적으로 표시될 수 있습니다.

  • 하위 알림을 개별적으로 표시하면 이점이 있습니다. 예를 들면 다음과 같습니다.

    • 알림을 실행할 수 있으며 각 하위 알림에 고유 작업이 있습니다.

    • 각 알림에 사용자가 볼 수 있는 추가 정보가 있습니다.

알림이 위의 기준을 충족하지 않으면 대신 새로운 정보로 기존 알림을 업데이트하거나 동일한 대화에 여러 업데이트를 표시하는 메시지 스타일 알림을 만드는 것이 좋습니다.

그룹을 만들고 그룹에 알림 추가

알림 그룹을 만들려면 그룹의 고유 식별자 문자열을 정의하세요. 그런 다음 그룹에 포함할 각 알림에 관해 setGroup()를 호출하여 그룹 이름을 전달합니다. 예:

Kotlin

val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"

val newMessageNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_mail)
        .setContentTitle(emailObject.getSenderName())
        .setContentText(emailObject.getSubject())
        .setLargeIcon(emailObject.getSenderAvatar())
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build()

Java

String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setSmallIcon(R.drawable.new_mail)
        .setContentTitle(emailObject.getSenderName())
        .setContentText(emailObject.getSubject())
        .setLargeIcon(emailObject.getSenderAvatar())
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build();

기본적으로 알림은 게시된 시기에 따라 정렬되지만 setSortKey()를 호출하여 순서를 변경할 수 있습니다.

알림 그룹의 알림을 다른 알림으로 처리해야 하는 경우 setGroupAlertBehavior()를 호출합니다. 예를 들어 그룹 요약만 알림음을 울리게 하려면 그룹의 모든 하위 요소에 그룹 알림 동작 GROUP_ALERT_SUMMARY이 있어야 합니다. 다른 옵션은 GROUP_ALERT_ALLGROUP_ALERT_CHILDREN입니다.

그룹 요약 설정

그룹화된 알림에는 그룹 요약 역할을 하는 추가 알림이 있어야 합니다. 그룹화된 알림을 사용 설정하려면 그룹 요약을 설정해야 합니다. 이 그룹 요약에는 사용자가 그룹에 포함된 항목을 이해할 수 있도록 그룹 내 다른 각 알림의 텍스트 중 일부가 포함되어야 합니다. 그룹 요약이 표시되는 방법은 Android 버전에 따라 다릅니다.

  • 중첩된 알림 그룹을 표시할 수 없는 7.0 (API 수준 24) 미만의 Android 버전에서는 시스템이 그룹 요약 알림만 표시하고 나머지 알림은 모두 숨깁니다. 사용자는 그룹 요약 알림을 탭하여 앱을 열 수 있습니다.

  • Android 7.0 이상에서는 시스템에서 그룹 요약 알림을 중첩된 각 알림의 텍스트 스니펫으로 라벨이 지정된 중첩된 알림 그룹으로 표시합니다. 그룹 요약 알림에 설정한 텍스트는 표시되지 않습니다. 사용자는 그림 1과 같이 중첩된 알림 그룹을 펼쳐 그룹의 개별 알림을 볼 수 있습니다.

최신 버전의 Android에서는 디자인한 그룹 요약 텍스트가 표시되지 않더라도 그룹화된 알림을 사용 설정하려면 항상 요약을 수동으로 설정해야 합니다. 그룹 요약의 동작은 웨어러블 기기와 같은 일부 기기 유형에 따라 다를 수 있습니다. 그룹 요약에 리치 콘텐츠를 설정하면 모든 기기와 버전에서 최상의 환경을 제공하는 데 도움이 됩니다.

그룹 요약을 추가하려면 다음 단계를 따르세요.

  1. 그룹에 관한 설명이 포함된 새 알림을 만듭니다. 보통 받은편지함 스타일 알림을 사용하는 것이 가장 좋습니다.

  2. setGroup()을 호출하여 그룹에 요약 알림을 추가합니다.

  3. setGroupSummary(true)를 호출하여 그룹 요약으로 사용해야 한다고 지정합니다.

다음 코드는 그룹 요약을 만드는 예를 보여줍니다.

Kotlin

// Use constant ID for notifications used as group summary.
val SUMMARY_ID = 0
val GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL"

val newMessageNotification1 = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject1.getSummary())
        .setContentText("You will not believe...")
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build()

val newMessageNotification2 = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject2.getSummary())
        .setContentText("Please join us to celebrate the...")
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build()

val summaryNotification = NotificationCompat.Builder(this@MainActivity, CHANNEL_ID)
        .setContentTitle(emailObject.getSummary())
        // Set content text to support devices running API level < 24.
        .setContentText("Two new messages")
        .setSmallIcon(R.drawable.ic_notify_summary_status)
        // Build summary info into InboxStyle template.
        .setStyle(NotificationCompat.InboxStyle()
                .addLine("Alex Faarborg Check this out")
                .addLine("Jeff Chang Launch Party")
                .setBigContentTitle("2 new messages")
                .setSummaryText("janedoe@example.com"))
        // Specify which group this notification belongs to.
        .setGroup(GROUP_KEY_WORK_EMAIL)
        // Set this notification as the summary for the group.
        .setGroupSummary(true)
        .build()

NotificationManagerCompat.from(this).apply {
    notify(emailNotificationId1, newMessageNotification1)
    notify(emailNotificationId2, newMessageNotification2)
    notify(SUMMARY_ID, summaryNotification)
}

Java

// Use constant ID for notifications used as group summary.
int SUMMARY_ID = 0;
String GROUP_KEY_WORK_EMAIL = "com.android.example.WORK_EMAIL";

Notification newMessageNotification1 =
    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject1.getSummary())
        .setContentText("You will not believe...")
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build();

Notification newMessageNotification2 =
    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_email_status)
        .setContentTitle(emailObject2.getSummary())
        .setContentText("Please join us to celebrate the...")
        .setGroup(GROUP_KEY_WORK_EMAIL)
        .build();

Notification summaryNotification =
    new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
        .setContentTitle(emailObject.getSummary())
        // Set content text to support devices running API level < 24.
        .setContentText("Two new messages")
        .setSmallIcon(R.drawable.ic_notify_summary_status)
        // Build summary info into InboxStyle template.
        .setStyle(new NotificationCompat.InboxStyle()
                .addLine("Alex Faarborg  Check this out")
                .addLine("Jeff Chang    Launch Party")
                .setBigContentTitle("2 new messages")
                .setSummaryText("janedoe@example.com"))
        // Specify which group this notification belongs to.
        .setGroup(GROUP_KEY_WORK_EMAIL)
        // Set this notification as the summary for the group.
        .setGroupSummary(true)
        .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(emailNotificationId1, newMessageNotification1);
notificationManager.notify(emailNotificationId2, newMessageNotification2);
notificationManager.notify(SUMMARY_ID, summaryNotification);

요약 알림 ID는 한 번만 게시되고 요약 정보가 변경될 경우 나중에 업데이트할 수 있도록 동일하게 유지되어야 합니다. 이후에 그룹을 추가하면 기존 요약이 업데이트됩니다.

알림을 사용하는 샘플 코드는 Android 알림 샘플을 참고하세요.

자동 그룹화

Android 7.0 (API 수준 24) 이상에서는 앱이 4개 이상의 알림을 보내고 그룹 키나 그룹 요약을 지정하지 않으면 시스템에서 이러한 알림을 자동으로 그룹화할 수 있습니다. 그룹화된 알림은 자동으로 그룹화된 알림 중 일부의 텍스트 스니펫으로 라벨이 지정된 그룹 요약 알림과 함께 표시됩니다. 사용자는 수동으로 그룹화된 알림과 마찬가지로 이 요약 알림을 펼쳐 각 개별 알림을 볼 수 있습니다.

자동 그룹화 동작은 일부 기기 유형에 따라 다를 수 있습니다. 모든 기기와 버전에서 최상의 환경을 제공하려면 알림을 그룹화해야 한다는 것을 알고 있는 경우 그룹 키와 그룹 요약을 지정하여 알림이 그룹화되도록 합니다.