알림 그룹 만들기

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

자바

    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) 이상에서는 시스템이 각 알림의 텍스트 스니펫을 사용하여 자동으로 그룹의 요약을 만듭니다. 사용자는 그림 1과 같이 이 알림을 확장하여 개별 알림을 볼 수 있습니다. 중첩된 알림 그룹을 표시하지 못하는 이전 버전을 지원하려면 요약 역할을 하는 추가 알림을 만들어야 합니다. 이 알림이 유일한 알림으로 표시되고 다른 알림은 시스템에서 모두 숨깁니다. 따라서, 이 요약에는 사용자가 앱을 열기 위해 탭할 수 있는 다른 모든 알림의 스니펫이 포함되어야 합니다.

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

  1. 그룹의 설명이 포함된 새로운 알림을 만듭니다. 보통 받은편지함 스타일 알림을 사용하는 것이 가장 좋습니다.
  2. setGroup()을 호출하여 그룹에 요약 알림을 추가합니다.
  3. setGroupSummary(true)를 호출하여 그룹 요약으로 사용하도록 지정합니다.

예:

Kotlin

    //use constant ID for notification 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)
    }
    

자바

    //use constant ID for notification 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 알림 샘플을 참조하세요.