কম্পিউটার টিউটোরিয়াল

অ্যাপ খুলবেন না Android-এ নোটিফিকেশন অ্যাকশন কীভাবে বাস্তবায়ন করবেন

অ্যাপ লঞ্চ ছাড়া নোটিফিকেশন অ্যাকশন বাস্তবায়ন

এই টিউটোরিয়ালটি দেখাবে কীভাবে একটি Android নোটিফিকেশনে অ্যাকশন বাটন (যেমন "ক্যান্সেল") যোগ করবেন যাতে ব্যবহারকারী অ্যাপ খুলবেন না সরাসরি নোটিফিকেশন থেকেই কাজ সম্পন্ন করতে পারেন। এটি BroadcastReceiver এবং PendingIntent ব্যবহার করে অর্জিত হয়।

পূর্বশর্ত

  • Android Studio ইনস্টল করা থাকতে হবে
  • Basic Android ডেভেলপমেন্ট জ্ঞান
  • API Level 26+ (Oreo) এর জন্য Notification Channel জ্ঞান

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio খুলুন → File → New Project → "Empty Activity" নির্বাচন করুন → প্রজেক্টের নাম দিন (যেমন: NotifyActionDemo) → Finish ক্লিক করুন।

ধাপ ২: লেআউট ফাইল সংশোধন করুন

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="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:onClick="createNotification"
        android:text="নোটিফিকেশন তৈরি করুন" />

</RelativeLayout>

ধাপ ৩: MainActivity.java বাস্তবায়ন

src/MainActivity.java

package com.example.notifyactiondemo;

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;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

public class MainActivity extends AppCompatActivity {

    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private static final String DEFAULT_CHANNEL_ID = "default";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createNotification(View view) {
        int notificationId = (int) System.currentTimeMillis();

        // ট্যাপ করলে অ্যাপ খুলবে (ঐচ্ছিক)
        PendingIntent contentIntent = PendingIntent.getActivity(
                this, 0, new Intent(), PendingIntent.FLAG_IMMUTABLE);

        // অ্যাকশন বাটনের জন্য Broadcast Intent
        Intent buttonIntent = new Intent(this, NotificationActionReceiver.class);
        buttonIntent.putExtra("notificationId", notificationId);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(
                this, 0, buttonIntent, PendingIntent.FLAG_IMMUTABLE);

        NotificationManager notificationManager = 
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
                .setContentTitle("আমার নোটিফিকেশন")
                .setContentText("অ্যাকশন বাটন সহ নোটিফিকেশন উদাহরণ")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentIntent(contentIntent)
                .addAction(R.drawable.ic_launcher_foreground, "রদ্দ করুন", actionPendingIntent)
                .setAutoCancel(true)
                .setDeleteIntent(getDeleteIntent());

        // Android Oreo+ এর জন্য চ্যানেল তৈরি
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "নোটিফিকেশন চ্যানেল", 
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
            builder.setChannelId(NOTIFICATION_CHANNEL_ID);
        }

        notificationManager.notify(notificationId, builder.build());
    }

    private PendingIntent getDeleteIntent() {
        Intent intent = new Intent(this, NotificationActionReceiver.class);
        intent.setAction("notification_cancelled");
        return PendingIntent.getBroadcast(this, 0, intent, 
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }
}

ধাপ ৪: BroadcastReceiver তৈরি করুন

src/NotificationActionReceiver.java

package com.example.notifyactiondemo;

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) {
        int notificationId = intent.getIntExtra("notificationId", 0);
        String action = intent.getAction();

        NotificationManager manager = (NotificationManager) 
                context.getSystemService(Context.NOTIFICATION_SERVICE);

        if ("notification_cancelled".equals(action)) {
            // ব্যবহারকারী নোটিফিকেশন ডিসমিস করল
            manager.cancel(notificationId);
        } else {
            // অ্যাকশন বাটনে ক্লিক করল (রদ্দ করুন)
            manager.cancel(notificationId);
        }
    }
}

ধাপ ৫: AndroidManifest.xml এ রেজিস্টার করুন

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.notifyactiondemo">

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <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/Theme.NotifyActionDemo">

        <receiver
            android:name=".NotificationActionReceiver"
            android:enabled="true"
            android:exported="true" />

        <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>

চলাচল ও পরীক্ষা

  1. আসলে ডিভাইস বা এমুলেটর সংযুক্ত করুন
  2. Android Studio-তে Run আইকনে ক্লিক করুন
  3. অ্যাপ খুললে "নোটিফিকেশন তৈরি করুন" বাটনে ট্যাপ করুন
  4. নোটিফিকেশন প্যানেলে যান — "রদ্দ করুন" বাটন দেখবেন
  5. বাটনে ট্যাপ করলে নোটিফিকেশন চলে যাবে, অ্যাপ খুলবে না

মূল পয়েন্ট সমূহ

  • PendingIntent.getBroadcast() ব্যবহার করুন অ্যাকশন বাটনের জন্য
  • BroadcastReceiver অ্যাকশন হ্যান্ডেল করে
  • android:exported="true" রিসিভারের জন্য আবশ্যক (Android 12+)
  • FLAG_IMMUTABLE ব্যবহার করুন PendingIntent-এ (API 23+)
  • নোটিফিকেশন চ্যানেল Android 8.0+ এর জন্য বাধ্যতামূলক

সাধারণ সমস্যা ও সমাধান

সমস্যাসমাধান
অ্যাকশন বাটন কাজ করে নাReceiver manifest-এ declared আছে কিনা চেক করুন, exported=true আছে কিনা
নোটিফিকেশন দেখাচ্ছে নাNotification Channel সঠিকভাবে তৈরি হয়েছে কিনা যাচাই করুন (API 26+)
Crash হয়PendingIntent-এ FLAG_IMMUTABLE বা FLAG_MUTABLE যোগ করুন
  1. অ্যান্ড্রয়েড অ্যাপের RAM ব্যবহার পরিমাপ করার পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপের বিল্ড ভার্সন নাম্বার কীভাবে পেতে হয়?

  3. অ্যান্ড্রয়েডে ActionBar/Toolbar-এর উচ্চতা পাওয়ার পদ্ধতি

  4. অ্যান্ড্রয়েডে ফেসবুক ব্যবহার করুন সব ইনভেসিভ পারমিশন ছাড়া