NotificationCompat.Builder.addAction() দিয়ে ইন্টারেক্টিভ বাটন যোগ করা
নোটিফিকেশনে অ্যাকশন বাটন যোগ করলে ব্যবহারকারী অ্যাপ খোলা ছাড়াই কুইক অ্যাকশন নিতে পারে (যেমন: Reply, Snooze, Delete, Play/Pause)। এই উদাহরণে "Snooze" বাটন যোগ করা হয়েছে।
ধাপ ১: প্রজেক্ট তৈরি
Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট
<?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="অ্যাকশন বাটনসহ নোটিফিকেশন" />
</RelativeLayout>
ধাপ ৩: MainActivity.java - addAction() ব্যবহার
package com.example.notificationactionbutton;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_ID = "action_button_channel";
private static final String DEFAULT_CH = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btnCreateNotification);
btn.setOnClickListener(v -> {
final int notifId = (int) System.currentTimeMillis();
// Snooze অ্যাকশনের জন্য BroadcastReceiver-এ পাঠানো ইন্টেন্ট
Intent snoozeIntent = new Intent(this, NotificationActionReceiver.class);
snoozeIntent.setAction("ACTION_SNOOZE");
snoozeIntent.putExtra("EXTRA_NOTIFICATION_ID", notifId);
PendingIntent snoozePendingIntent = PendingIntent.getBroadcast(
this, 0, snoozeIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
.setContentTitle("অ্যাকশন বাটনসহ নোটিফিকেশন")
.setContentText("নিচে 'Snooze' বাটনটি চাপুন")
.setTicker("নতুন নোটিফিকেশন")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.addAction(R.drawable.ic_launcher_foreground, "Snooze", snoozePendingIntent) // ★ অ্যাকশন বাটন
.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Action Channel", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);
if (nm != null) nm.createNotificationChannel(ch);
}
if (nm != null) nm.notify(notifId, builder.build());
});
}
}
ধাপ ৪: BroadcastReceiver - অ্যাকশন হ্যান্ডলিং
src/NotificationActionReceiver.java:
package com.example.notificationactionbutton;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("ACTION_SNOOZE".equals(intent.getAction())) {
int notifId = intent.getIntExtra("EXTRA_NOTIFICATION_ID", -1);
if (notifId != -1) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (nm != null) nm.cancel(notifId); // নোটিফিকেশন মুছে ফেলা
}
// এখানে Snooze লজিক যোগ করুন (যেমন: ১০ মিনিট পর আবার নোটিফিকেশন শিডিউল)
}
}
}
ধাপ ৫: AndroidManifest.xml - রিসিভার যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationactionbutton">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.NotificationActionButton">
<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>
<receiver
android:name=".NotificationActionReceiver"
android:exported="false" />
</application>
</manifest>
অ্যাডভান্সড: Android 10+ (API 29) এ RemoteInput দিয়ে Inline Reply
// রিপ্লাই বাটন যোগ করা
RemoteInput remoteInput = new RemoteInput.Builder("reply_key")
.setLabel("উত্তর লিখুন...")
.build();
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_reply_icon, "Reply", replyPendingIntent)
.addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build();
builder.addAction(replyAction);
পরীক্ষা
অ্যাপ রান করুন → নোটিফিকেশন এক্সপ্যান্ড করুন → "Snooze" বাটন চাপুন → নোটিফিকেশন মুছে যাবে।
