맞춤 알림 레이아웃 만들기

여러 Android 버전에서 알림이 가장 잘 보이도록 하려면 항상 표준 알림 템플릿을 사용하여 알림을 만들어야 합니다. 하지만 시스템 템플릿이 요구를 충족하지 않을 경우 자체 알림 레이아웃을 제공할 수 있습니다.

알림에 콘텐츠를 추가하려면 맞춤 레이아웃을 만드는 대신 확장 가능한 알림 템플릿 중 하나를 사용하는 것도 고려해 보세요.

주의: 맞춤 알림 레이아웃을 사용하는 경우 맞춤 레이아웃이 다양한 기기 방향과 해상도에서 작동하는지 확인하세요. 이 주의사항은 모든 UI 레이아웃에 적용되지만 알림 창의 공간이 매우 제한적이므로 알림의 경우 특히 중요합니다. 맞춤 알림 레이아웃에 사용할 수 있는 높이는 알림 뷰에 따라 달라집니다. 일반적으로 축소된 뷰 레이아웃은 64dp로 제한되고 확장된 뷰 레이아웃은 256dp로 제한됩니다.

콘텐츠 영역의 맞춤 레이아웃 만들기

맞춤 레이아웃이 필요한 경우 알림에 NotificationCompat.DecoratedCustomViewStyle을 적용할 수 있습니다. 이 API를 사용하면 알림 아이콘, 타임스탬프, 하위 텍스트, 작업 버튼에 시스템 데코레이션을 사용하면서 일반적으로 제목과 텍스트 콘텐츠가 차지하는 콘텐츠 영역에 맞춤 레이아웃을 제공할 수 있습니다.

이 API는 다음과 같이 기본 알림 레이아웃을 기반으로 작성되어 확장 가능한 알림 템플릿과 유사하게 작동합니다.

  1. NotificationCompat.Builder기본 알림을 빌드합니다.
  2. setStyle()을 호출하여 NotificationCompat.DecoratedCustomViewStyle의 인스턴스를 전달합니다.
  3. 맞춤 레이아웃을 RemoteViews의 인스턴스로 확장합니다.
  4. setCustomContentView()를 호출하여 접힌 알림의 레이아웃을 설정합니다.

    선택적으로 setCustomBigContentView()를 호출하여 펼쳐진 알림에 다른 레이아웃을 설정합니다.

예:

Kotlin

    // Get the layouts to use in the custom notification
    val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
    val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)

    // Apply the layouts to the notification
    val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .build()
    

자바

    // Get the layouts to use in the custom notification
    RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
    RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

    // Apply the layouts to the notification
    Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .build();
    

알림의 배경 색상은 기기 및 버전에 따라 다를 수 있습니다. 따라서, 맞춤 레이아웃의 텍스트와 제목에는 각각 TextAppearance_Compat_NotificationTextAppearance_Compat_Notification_Title과 같은 지원 라이브러리 스타일을 항상 적용해야 합니다. 이러한 스타일은 색상 변화에 맞게 조정되므로 검은색 배경에 검은색 텍스트나 흰색 배경에 흰색 텍스트로 표시되지 않습니다. 예:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="@string/notification_title"
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title" />
    

또한, 텍스트 색상을 읽을 수 없게 되므로 RemoteViews 객체에 배경 이미지를 설정하지 마세요.

완전한 맞춤 알림 레이아웃 만들기

표준 알림 아이콘 및 헤더로 알림에 데코레이션을 적용하지 않으려면 위의 단계에 따라 setCustomBigContentView()를 사용하되 setStyle()을 호출하지 마세요.

Android 4.1(API 수준 16) 이전의 Android 버전을 지원하려면 setContent()도 호출하여 동일한 RemoteViews 객체를 전달해야 합니다.

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