ภาพรวม
ตัวอย่างนี้สาธิตการสร้าง Local Notification พื้นฐานที่จะหายไปอัตโนมัติเมื่อผู้ใช้คลิก โดยใช้ setAutoCancel(true)
ขั้นตอนที่ 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: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.java
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
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 manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this, default_notification_channel_id)
.setContentTitle("My Notification")
.setContentText("Notification Listener Service Example")
.setTicker("Notification Listener Service Example")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true); // สำคัญ: ปิดอัตโนมัติเมื่อคลิก
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "Channel", importance);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert manager != null;
manager.createNotificationChannel(channel);
}
assert manager != null;
manager.notify((int) System.currentTimeMillis(), builder.build());
}
}
ขั้นตอนที่ 4: 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>
คำอธิบาย
setAutoCancel(true) ทำให้การแจ้งเตือนถูกลบออกจากแถบสถานะทันทีที่ผู้ใช้แตะคลิก นี่คือพฤติกรรมมาตรฐานของ Notification ส่วนใหญ่ ไม่ต้องเขียนโค้ดเพิ่มเติมในการจัดการการคลิก