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

Android-এ প্রোগ্রাম্যাটিক্যালি নোটিফিকেশন বার থেকে নোটিফিকেশন সরানোর পদ্ধতি

নোটিফিকেশন প্রোগ্রাম্যাটিক্যালি ক্যাঞ্চেল/মুছে ফেলা

এই উদাহরণে দেখানো হয়েছে কীভাবে একটি কাস্টম লেআউটের নোটিফিকেশন তৈরি করবেন এবং পরবর্তীতে একটি বাটন চাপে বা যেকোনো লজিকের অধিনে সেটি প্রোগ্রাম্যাটিক্যালি মুছে ফেলবেন।

কী কী শিখবেন

  • Custom Notification Layout (RemoteViews)
  • NotificationManager.notify() এবং cancel()
  • Notification ID ট্র্যাক করা
  • Notification Channel (API 26+)

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

Package: com.example.removenotification

ধাপ ২: মূল লেআউট (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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:gravity="center">

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="নোটিফিকেশন তৈরি করুন"
            android:onClick="createNotification"/>

        <Button
            android:id="@+id/btnRemove"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="নোটিফিকেশন সরান"
            android:onClick="removeNotification"
            android:enabled="false"/>

    </LinearLayout>

</RelativeLayout>

ধাপ ৩: কাস্টম নোটিফিকেশন লেআউট (res/layout/custom_notification_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:padding="10dp"
    android:background="@android:color/white">

    <ImageView
        android:id="@+id/image"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="16dp"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:layout_marginTop="8dp"
        android:text="কাস্টম নোটিফিকেশন"
        android:textColor="#000"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toEndOf="@id/image"
        android:layout_marginTop="4dp"
        android:text="এটি একটি কাস্টম লেআউটের নোটিফিকেশন। বাটন চাপলে মুছে যাবে।"
        android:textSize="14sp"
        android:textColor="#666" />

</RelativeLayout>

ধাপ ৪: MainActivity.java

package com.example.removenotification;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
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 CHANNEL_ID = "custom_notif_channel";
    private static final String DEFAULT_CHANNEL = "default";

    private NotificationManager notificationManager;
    private int notificationId = 0;
    private Button btnRemove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnRemove = findViewById(R.id.btnRemove);
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        
        // চ্যানেল প্রি-ক্রিয়েট
        createChannelIfNeeded();
    }

    private void createChannelIfNeeded() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID, "কাস্টম নোটিফিকেশন", 
                    NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("কাস্টম লেআউট দিয়ে তৈরি নোটিফিকেশন");
            notificationManager.createNotificationChannel(channel);
        }
    }

    public void createNotification(View view) {
        // RemoteViews দিয়ে কাস্টম লেআউট লোড
        androidx.widget.RemoteViews contentView = new androidx.widget.RemoteViews(
                getPackageName(), R.layout.custom_notification_layout);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL)
                .setCustomContentView(contentView)  // কাস্টম লেআউট সেট
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setAutoCancel(false) // নিজে মুছব, অটো ক্যাঞ্চেল নয়
                .setOngoing(true)     // Ongoing = সোয়াইপ করে যাবে না
                .setChannelId(CHANNEL_ID);

        notificationId = (int) System.currentTimeMillis();
        
        notificationManager.notify(notificationId, builder.build());
        
        // UI আপডেট
        findViewById(R.id.btnCreate).setEnabled(false);
        btnRemove.setEnabled(true);
    }

    public void removeNotification(View view) {
        if (notificationId != 0) {
            notificationManager.cancel(notificationId);
            notificationId = 0;
            
            // UI রিসেট
            findViewById(R.id.btnCreate).setEnabled(true);
            btnRemove.setEnabled(false);
        }
    }

    // বোনাস: সব নোটিফিকেশন ক্লিয়ার
    public void clearAllNotifications(View view) {
        notificationManager.cancelAll();
        notificationId = 0;
        findViewById(R.id.btnCreate).setEnabled(true);
        btnRemove.setEnabled(false);
    }
}

ধাপ ৫: AndroidManifest.xml

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

    <uses-permission android:name="android.permission.VIBRATE" />
    <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/Theme.RemoveNotification">

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

নোটিফিকেশন ক্যাঞ্চেল করার বিভিন্ন পদ্ধতি

মেথডব্যবহারস্কোপ
cancel(int id)নির্দিষ্ট ID-র নোটিফিকেশন মুছে দেয়একক নোটিফিকেশন
cancel(String tag, int id)Tag + ID দিয়ে নির্দিষ্ট নোটিফিকেশনএকক (Tagged)
cancelAll()অ্যাপের সব নোটিফিকেশন মুছে দেয়সব নোটিফিকেশন
setAutoCancel(true)ব্যবহারকারী ট্যাপ করলে আটোমেটিক মুছে যায়ইন্টারঅ্যাকশন-ভিত্তিক
setDeleteIntent(pendingIntent)ডিসমিস হলে কলব্যাক পাওয়া যায়ইভেন্ট-ভিত্তিক

Notification ID ম্যানেজমেন্ট স্ট্র্যাটেজি

// কনস্ট্যান্ট ID (একই টাইপের নোটিফিকেশন আপডেট করার জন্য)
private static final int REMINDER_NOTIFICATION_ID = 1001;
private static final int CHAT_NOTIFICATION_ID = 1002;

// বা ইউনিক ID (প্রতিটি আলাদা নোটিফিকেশনের জন্য)
int uniqueId = (int) System.currentTimeMillis(); // বা AtomicInteger

// Tag + ID কম্বিনেশন (একই ID-র একাধিক ভেরিয়েন্ট)
manager.notify("chat_user_123", 1001, notification);
manager.cancel("chat_user_123", 1001);

কাস্টম লেআউটের সীমাবদ্ধতা

  • RemoteViews শুধু লিমিটেড ভিউ সাপোর্ট করে (TextView, ImageView, Button, ProgressBar, etc.)
  • Custom View, WebView, RecyclerView সাপোর্ট করে না
  • লেআউট হাইট সাধারণত ৬৪-২৫৬dp পর্যন্ত সীমাবদ্ধ
  • Android 12+ (API 31) এ কাস্টম নোটিফিকেশন লেআউট ডিপ্রিকেটেড হয়েছে; Notification.DecoratedCustomViewStyle ব্যবহার করুন

আধুনিক পদ্ধতি: Notification.DecoratedCustomViewStyle (API 31+)

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setCustomContentView(contentView)
        .setCustomBigContentView(expandedView) // এক্সপ্যান্ডেড ভিউ
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setChannelId(CHANNEL_ID);

পরীক্ষা পদ্ধতি

  1. অ্যাপ চালু করুন → "নোটিফিকেশন তৈরি করুন" চাপুন
  2. নোটিফিকেশন প্যানেলে কাস্টম লেআউট দেখুন
  3. "নোটিফিকেশন সরান" চাপুন → নোটিফিকেশন গিয়ে যাবে
  4. পুনরায় "তৈরি করুন" চাপুন → নতুন নোটিফিকেশন আসবে
  1. Android-এ প্রোগ্রাম্যাটিক্যালি অ্যারে থেকে Spinner তৈরি করার পদ্ধতি

  2. অ্যান্ড্রয়েড থেকে ম্যালওয়্যার সরানোর উপায়

  3. অ্যান্ড্রয়েড ফোন থেকে স্পাইওয়্যার শনাক্ত ও অপসারণের পূর্ণ নির্দেশিকা

  4. আপনার অ্যান্ড্রয়েড ফোন থেকে ভাইরাস নিরাপদে রিমুভ করার ধাপে ধাপে গাইড