ภาพรวม
ตัวอย่างนี้แสดงวิธีแสดงจำนวนการแจ้งเตือน (Badge Count) บนไอคอนแอปใน Launcher โดยใช้ NotificationCompat.Builder และ setNumber() รองรับ Android 8.0+ (API 26+) ด้วย Notification Channel
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
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="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="createNotification"
android:text="Create Notification" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity (src/MainActivity.java)
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static int count = 0;
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private static final String DEFAULT_NOTIFICATION_CHANNEL_ID = "default";
@Override
protected void onResume() {
super.onResume();
// รีเซ็ตจำนวนเมื่อกลับมาที่แอป
count = 0;
// ล้าง Badge (Android 8.0+)
NotificationManagerCompat.from(this).cancelAll();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNotificationChannel();
}
@SuppressLint("WrongConstant")
public void createNotification(View view) {
count++;
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("fromNotification", true);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, notificationIntent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_IMMUTABLE : 0
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_NOTIFICATION_CHANNEL_ID)
.setContentTitle("My Notification")
.setContentText("Notification #" + count)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true)
.setNumber(count) // ตั้งค่า Badge Count
.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify((int) System.currentTimeMillis(), builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Notifications";
String description = "Channel for notifications";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
ขั้นตอนที่ 4: Manifest (Manifests/AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.app.sample">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- สำหรับ Android 13+ ต้องขอสิทธิ์ POST_NOTIFICATIONS -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<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"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ข้อควรทราบเกี่ยวกับ Notification Badge
- Android 8.0+ (API 26+): รองรับ Badge อย่างเป็นทางการผ่าน Notification Channel และ setNumber()
- Launcher Support: ไอคอน Badge จะแสดงได้ก็ต่อเมื่อ Launcher รองรับ (Pixel Launcher, Nova, OneUI, MIUI, ColorOS รองรับ)
- Android 13+ (API 33+): ต้องขอสิทธิ์
POST_NOTIFICATIONSใน Runtime - Clearing Badge: ใช้
notificationManager.cancel(id)หรือcancelAll()หรือตั้ง setNumber(0)
การทดสอบ
เชื่อมต่ออุปกรณ์ Android จริง ใน Android Studio คลิก Run - กดปุ่ม Create Notification หลายครั้ง - ดูไอคอนแอปใน Launcher จะแสดงตัวเลข Badge เพิ่มขึ้น