[android] 안드로이드 인스턴트 앱의 사용자 알림 관리

안드로이드 인스턴트 앱은 사용자에게 재미있는 기능을 제공하고 빠르게 실행할 수 있는 경험을 제공합니다. 앱의 사용자에게 알림을 통해 정보를 전달하고 상호작용하는 기능은 매우 중요합니다. 이번 글에서는 안드로이드 인스턴트 앱에서 사용자 알림을 관리하는 방법에 대해 살펴보겠습니다.

알림 표시

안드로이드 인스턴트 앱에서 알림을 표시하려면 NotificationCompat.Builder 클래스를 사용하여 알림을 만들어야 합니다. 다음은 기본적인 알림을 만드는 예제 코드입니다.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("알림 제목")
        .setContentText("알림 내용")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

알림 채널

알림 채널은 안드로이드 8.0(API 레벨 26)부터 도입된 기능으로, 사용자에게 알림을 그룹화하고 관리할 수 있도록 합니다. 인스턴트 앱에서 알림 채널을 사용하려면 다음과 같이 채널을 만들고 설정해야 합니다.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "채널 이름";
    String description = "채널 설명";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

상호작용 알림

인스턴트 앱에서 상호작용하는 알림을 표시하려면 알림 액션을 추가해야 합니다. 다음은 알림에 액션을 추가하는 예제 코드입니다.

Intent intent = new Intent(this, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("알림 제목")
        .setContentText("알림 내용")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent);

// 알림 액션 추가
NotificationCompat.Action action = new NotificationCompat.Action.Builder(
        R.drawable.icon, "액션 제목", pendingIntent)
        .build();
builder.addAction(action);

인스턴트 앱에서 알림을 사용자에게 효과적으로 전달하기 위해 이러한 알림 관리 기능을 활용할 수 있습니다. 이를 통해 사용자는 앱의 핵심 기능과 상호작용할 수 있어 더욱 향상된 경험을 얻을 수 있습니다.

참고 자료