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

ব্যবহারকারী ক্লিক করলে Android নোটিফিকেশন বন্ধ করা


setAutoCancel(true) দিয়ে নোটিফিকেশন অটো-রিমুভ

এটি সবচেয়ে সাধারণ এবং ডিফল্ট আচরণ: ব্যবহারকারী নোটিফিকেশনটিতে ট্যাপ করলে তা নোটিফিকেশন শেড থেকে মুছে যায়। নিচের উদাহরণে এটি স্পষ্টভাবে দেখানো হয়েছে।

ধাপ ১: প্রজেক্ট সেটআপ

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    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="0dp"
        android:layout_height="wrap_content"
        android:text="অটো-ক্যানসেল নোটিফিকেশন"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ধাপ ৩: Java কোড

setAutoCancel(true) সেট করা হয়েছে (যা ডিফল্টও true):

package com.example.autocancelnotification;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
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 = "channel_001";
    private static final String DEFAULT_CH = "default";

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

        findViewById(R.id.btnCreateNotification).setOnClickListener(v -> {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("অটো-ক্যানসেল নোটিফিকেশন")
                    .setContentText("এই নোটিফিকেশনটি ট্যাপ করলে স্বয়ংক্রিয় মুছে যাবে।")
                    .setContentIntent(pIntent)
                    .setAutoCancel(true) // ★ ডিফল্ট আচরণ
                    .setStyle(new NotificationCompat.InboxStyle());

            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "চ্যানেল", NotificationManager.IMPORTANCE_HIGH);
                builder.setChannelId(CHANNEL_ID);
                if (nm != null) nm.createNotificationChannel(ch);
            }

            int id = (int) System.currentTimeMillis();
            if (nm != null) nm.notify(id, builder.build());
        });
    }
}

ধাপ ৪: ম্যানিফেস্ট

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AutoCancel">

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

ফলাফল

নোটিফিকেশন আসবে। ব্যবহারকারী যতক্ষণ সেটিতে ট্যাপ করবে, ততক্ষণ নোটিফিকেশন শেডে থাকবে। ট্যাপ करते ही এটি মুছে যাবে এবং অ্যাপ খুলবে।

ব্যবহারকারী ক্লিক করলে Android নোটিফিকেশন বন্ধ করা

  1. Android-এ Notification.deleteIntent কীভাবে ব্যবহার করবেন

  2. Android-এ রিমাইন্ডার নোটিফিকেশন তৈরি করার পদ্ধতি

  3. অ্যান্ড্রয়েডে EditText-এর বাইরে ক্লিক করলে সফ্ট কিবোর্ড লুকানোর পদ্ধতি

  4. উইন্ডোজ ফিডব্যাক নোটিফিকেশন 永久 বন্ধ করুন