상태 표시줄에 아이콘 없이 안드로이드 알림 만들기
이 튜토리얼에서는 RemoteViews를 활용한 커스텀 알림 레이아웃과 VISIBILITY_SECRET 설정을 통해, 상태 표시줄에는 아이콘이 노출되지 않는 안드로이드 알림을 만드는 방법을 단계별로 살펴봅니다.
핵심 아이디어는 알림 콘텐츠를 직접 디자인한 레이아웃으로 구성하되, 알림 공개 범위(Visibility)를 '비밀(Secret)'로 지정하여 잠금 화면이나 상태 표시줄에 민감한 정보가 드러나지 않도록 하는 것입니다.
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"
tools:context=".MainActivity">
<Button
android:onClick="createNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:text="Create notification" />
</RelativeLayout>3단계: 커스텀 알림 레이아웃 작성
res/layout/custom_notification_layout.xml 파일을 새로 만들고 다음 코드를 추가합니다. 이 레이아웃은 알림 창에 실제로 표시될 사용자 정의 UI를 정의합니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="96dp"
android:padding="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/image"
android:text="Testing"
android:textColor="#000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="8dp"
android:layout_toEndOf="@+id/image"
android:hint="We are testing without icon"
android:inputType="text"
android:textSize="14sp" />
</RelativeLayout>4단계: MainActivity.java 작성
src/MainActivity.java 파일에 다음 코드를 추가합니다. 여기서 핵심은 두 가지입니다. 첫째, setContent() 메서드에 RemoteViews 객체를 전달해 커스텀 레이아웃을 적용하고, 둘째, setVisibility(VISIBILITY_SECRET)를 호출하여 상태 표시줄과 잠금 화면에 알림 내용이 노출되지 않도록 설정하는 것입니다.
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;
import android.widget.RemoteViews;
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);
onNewIntent(getIntent());
}
public void createNotification(View view) {
// 커스텀 알림 레이아웃을 RemoteViews로 생성
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContent(contentView);
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
// 알림 공개 범위를 SECRET으로 설정 → 상태 표시줄·잠금화면에 내용 미노출
mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
long[] VIBRATE_PATTERN = {0, 500};
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());
}
}5단계: 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>앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터와 연결했다고 가정합니다. 안드로이드 스튜디오에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바의 Run 아이콘을 클릭하세요. 목록에서 본인의 모바일 기기를 선택하면, 기본 화면과 함께 버튼이 표시된 앱이 실행됩니다.
버튼을 눌러 알림을 생성하면, 커스텀 레이아웃으로 구성된 알림이 발송되지만 상태 표시줄에는 아이콘이 나타나지 않는 것을 확인할 수 있습니다. 이는 VISIBILITY_SECRET 설정 덕분이며, 알림 내용은 알림 창을 직접 열었을 때만 확인 가능합니다.
마무리 정리
- RemoteViews: 알림에 표시할 사용자 정의 레이아웃을 적용할 때 사용합니다.
- setVisibility(VISIBILITY_SECRET): 잠금 화면과 상태 표시줄에 알림 내용이 노출되지 않도록 합니다.
- NotificationChannel: 안드로이드 8.0(Oreo, API 26) 이상에서는 반드시 알림 채널을 생성해야 합니다.
이 방법은 금융 앱처럼 민감한 정보를 다루는 경우나, 화면 녹화·방해 금지 환경에서 알림 내용을 숨기고 싶은 경우에 특히 유용하게 활용할 수 있습니다.