ภาพรวม
ตัวอย่างนี้สาธิตการเพิ่มปุ่ม Action (Action Button) ลงในการแจ้งเตือน โดยใช้ addAction() และ PendingIntent.getBroadcast() เพื่อรับเหตุการณ์กดปุ่ม
ขั้นตอนที่ 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 - เพิ่ม Action Button
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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";
public static final String ACTION_SNOOZE = "app.tutorialspoint.com.notifyme.ACTION_SNOOZE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
btnCreateNotification.setOnClickListener(v -> {
// Intent สำหรับปุ่ม Snooze (ใช้ Broadcast)
Intent snoozeIntent = new Intent(ACTION_SNOOZE);
snoozeIntent.putExtra("EXTRA_NOTIFICATION_ID", 0);
PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(
MainActivity.this, 0, snoozeIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this, default_notification_channel_id)
.setContentTitle("My Notification")
.setContentText("Notification with Action Button")
.setTicker("Notification with Action")
.setSmallIcon(R.drawable.ic_launcher_foreground)
// เพิ่มปุ่ม Action: icon, label, PendingIntent
.addAction(R.drawable.ic_launcher_foreground, "Snooze", snoozePendingIntent)
.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: สร้าง BroadcastReceiver สำหรับรับ Action (SnoozeReceiver.java)
package app.tutorialspoint.com.notifyme;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class SnoozeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (MainActivity.ACTION_SNOOZE.equals(intent.getAction())) {
int notificationId = intent.getIntExtra("EXTRA_NOTIFICATION_ID", 0);
Log.d("SnoozeReceiver", "Snooze clicked for notification: " + notificationId);
Toast.makeText(context, "Snooze pressed!", Toast.LENGTH_SHORT).show();
// ทำงานที่นี่: ยกเลิกการแจ้งเตือน, ตั้งนาฬิกาใหม่, ฯลฯ
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
}
ขั้นตอนที่ 5: AndroidManifest.xml - เพิ่ม Receiver
<?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>
<!-- Receiver สำหรับ Action Button -->
<receiver
android:name=".SnoozeReceiver"
android:exported="false">
<intent-filter>
<action android:name="app.tutorialspoint.com.notifyme.ACTION_SNOOZE" />
</intent-filter>
</receiver>
</application>
</manifest>
คำอธิบาย
- addAction(icon, title, pendingIntent): เพิ่มปุ่มได้สูงสุด 3 ปุ่ม (บน Android 7+)
- ใช้
PendingIntent.getBroadcast()สำหรับ Action Button (ไม่ใช่ getActivity) - ต้องประกาศ
<receiver>ใน Manifest พร้อม Intent Filter - Android 10+ อาจจำกัดการเริ่ม Activity จาก Action Button ควรใช้ BroadcastReceiver
การขยาย: เพิ่มปุ่มหลายปุ่ม
.addAction(R.drawable.ic_reply, "Reply", replyPendingIntent) .addAction(R.drawable.ic_delete, "Delete", deletePendingIntent) .addAction(R.drawable.ic_archive, "Archive", archivePendingIntent)