Android 알림에 InboxStyle 적용하기
이 튜토리얼에서는 InboxStyle을 사용하여 Android 알림에 스타일을 적용하는 방법을 단계별로 살펴봅니다. InboxStyle은 이메일 수신함처럼 여러 줄의 요약 텍스트를 한 번에 보여줄 수 있는 확장형 알림 스타일로, 메시지 목록이나 여러 항목을 간결하게 전달할 때 유용합니다.
1단계: 새 프로젝트 만들기
Android Studio를 열고 File → New Project 메뉴로 이동한 뒤, 새 프로젝트 생성에 필요한 모든 정보를 입력하여 프로젝트를 만듭니다.
2단계: activity_main.xml 레이아웃 작성
다음 코드를 res/layout/activity_main.xml 파일에 추가합니다. 화면 중앙에 알림을 생성하는 버튼 하나를 배치하는 구성입니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCreateNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:text="Create Notification" />
</RelativeLayout>3단계: MainActivity에 알림 로직 구현
다음 코드를 src/MainActivity에 추가합니다. 핵심은 mBuilder.setStyle(new NotificationCompat.InboxStyle()) 부분으로, 이 코드가 알림을 InboxStyle 형태로 지정합니다.
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContentTitle("My Notification");
mBuilder.setStyle(new NotificationCompat.InboxStyle());
mBuilder.setContentText("Notification Listener Service Example");
mBuilder.setTicker("Notification Listener Service Example");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}참고: Android 8.0(Oreo, API 26) 이상에서는 반드시 알림 채널(NotificationChannel)을 생성해야 알림이 정상적으로 표시됩니다. 위 코드는 SDK 버전을 확인하여 해당 기능을 조건부로 처리하고 있습니다.
4단계: AndroidManifest.xml 설정
다음 코드를 AndroidManifest.xml 파일에 추가합니다. 진동 권한과 액티비티 등록 정보를 포함하고 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 스마트폰을 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 상단 툴바의 Run(실행) 아이콘을 클릭하세요. 목록에서 본인의 모바일 기기를 선택하면 앱이 설치되어 실행됩니다.
앱이 실행되면 화면 중앙의 Create Notification 버튼을 눌러보세요. 아래와 같이 InboxStyle이 적용된 알림이 기기에 표시되는 것을 확인할 수 있습니다.

