알림 그룹 만들기

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_ALL 드림 및 GROUP_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)
}

자바

// 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개 이상의 그룹 키 또는 그룹 요약을 지정하지 않으면 시스템은 자동으로 그룹화됩니다. 알림이 자동으로 그룹화됨 여기 보이는 일부 텍스트 스니펫으로 레이블이 지정된 그룹 요약 알림을 그룹화된 알림 사용자는 이 요약 알림을 펼쳐서 각 항목을 확인할 수 있습니다. 수동으로 그룹화된 알림과 마찬가지로 개별 알림을 수신합니다.

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