ภาพรวม
ตัวอย่างนี้สาธิตการสร้างการแจ้งเตือนที่ไม่แสดงไอคอนใน Status Bar (แถบสถานะ) แต่ยังคงปรากฏใน Notification Shade (ม่านการแจ้งเตือน) โดยใช้ setVisibility(VISIBILITY_SECRET) และ Custom Layout
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: 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: Custom Layout (custom_notification_layout.xml) - ไม่มี ImageView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_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 - ใช้ VISIBILITY_SECRET
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
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 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);
// ยังต้องมี Small Icon (บังคับ) แต่จะซ่อนด้วย VISIBILITY_SECRET
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
// สำคัญ: ซ่อนจาก Status Bar และ Lock Screen
mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);
long[] VIBRATE_PATTERN = {0, 500};
if (Build.VERSION.SDK_INT >= 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
<?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>
Visibility Levels ใน Notification
| Constant | ค่า | พฤติกรรม |
|---|---|---|
| VISIBILITY_PUBLIC | 1 | แสดงเต็มรูปแบบบน Lock Screen และ Status Bar (ค่าเริ่มต้น) |
| VISIBILITY_PRIVATE | 0 | ซ่อนเนื้อหาเฉพาะส่วนตัวบน Lock Screen แสดงเฉพาะชื่อแอปและไอคอน |
| VISIBILITY_SECRET | -1 | ซ่อนทั้งหมดบน Lock Screen และ ไม่แสดงไอคอนใน Status Bar |
การใช้งาน VISIBILITY_SECRET
เมื่อใดควรใช้
- การแจ้งเตือนที่ละเอียดอ่อน (ข้อความส่วนตัว, รหัส OTP, ข้อมูลสุขภาพ)
- การแจ้งเตือนระบบภายในที่ไม่ต้องการให้ผู้ใช้เห็นใน Status Bar
- แอปที่ต้องการความเป็นส่วนตัวสูง
ข้อจำกัด
- ยังคงต้องมี
setSmallIcon()(Android ตรวจสอบและ Crash หากไม่มี) - ไม่แสดงใน Status Bar เลย - ผู้ใช้อาจพลาดการแจ้งเตือน
- บน Lock Screen จะไม่แสดงเลย (แม้ตั้งค่าแสดง Notification บน Lock Screen)
- ยังคงปรากฏใน Notification Shade เมื่อลากม่านลง
ทางเลือกอื่น: Minimal Notification
// สร้าง Notification ที่มีขั้นต่ำ - ไม่มีไอคอนใหญ่ ไม่มีข้อความมาก
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_transparent) // ใช้ transparent icon 1x1 pixel
.setContentTitle("")
.setContentText("")
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setPriority(NotificationCompat.PRIORITY_MIN);
// ic_transparent.xml (Vector Drawable 1x1 transparent)
/*
<vector xmlns:android="https://schemas.android.com/apk/res/android"
android:width="1dp" android:height="1dp"
android:viewportWidth="1" android:viewportHeight="1">
<path android:fillColor="#00000000" android:pathData="M0,0 L1,1"/>
</vector>
*/
⚠️ ข้อควรระวัง
- Android 12+ (API 31+) จำกัดการใช้ PRIORITY_MIN และ VISIBILITY_SECRET อย่างเคร่งครัด
- อาจต้องการ Permission
FOREGROUND_SERVICEสำหรับการแจ้งเตือนที่ต่อเนื่อง - ผู้ใช้สามารถปิดการแจ้งเตือนของแอปได้ทั้งหมดจาก Settings
- ควรใช้เฉพาะกรณีที่จำเป็นจริงๆ เพื่อประสบการณ์ผู้ใช้ที่ดี