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

অ্যান্ড্রয়েডে ক্লিক করলেও না মুছে যাওয়া নোটিফিকেশন তৈরি করা


অটো-ক্যানসেল বন্ধ করে নোটিফিকেশন স্থায়ী রাখা

ডিফল্টভাবে Android নোটিফিকেশনটি ক্লিক করলে মুছে যায়। এই উদাহরণে setAutoCancel(false) ব্যবহার করে নোটিফিকেশনটি ক্লিক করার পরও নোটিফিকেশন প্যানেলে রাখার পদ্ধতি দেখানো হয়েছে।

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

Android Studio-তে File → New Project এ যান এবং প্রজেক্ট সেটআপ করুন।

ধাপ ২: লেআউট XML

res/layout/activity_main.xml এ একটি বাটন যোগ করুন:

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

ধাপ ৩: MainActivity.java - মূল লজিক

নিচের কোডে .setAutoCancel(false) লাইনটি নোটিফিকেশনকে ক্লিকের পরও বজায় রাখে:

package com.example.persistentnotification;

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

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

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
                    .setAutoCancel(false) // ★ এই লাইনটি নোটিফিকেশনকে স্থায়ী করে
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("স্থায়ী নোটিফিকেশন")
                    .setContentText("এটি ক্লিক করলেও মুছে যাবে না। ম্যানুয়ালি সোয়াইপ করে মুছতে হবে।")
                    .setContentIntent(pendingIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText("এটি একটি বিস্তারিত নোটিফিকেশন যা ক্লিক করার পরও থাকবে।"));

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

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

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

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

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

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

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

ফলাফল

অ্যাপ রান করুন এবং বাটনে ক্লিক করুন। নোটিফিকেশন আসবে। এটি ট্যাপ করলে অ্যাপ খুলবে কিন্তু নোটিফিকেশনটি নোটিফিকেশন শেডে 그대로 থাকবে। ব্যবহারকারী সোয়াইপ করে বা "Clear All" দিয়ে ही এটি মুছে ফেলতে পারবেন।

অ্যান্ড্রয়েডে ক্লিক করলেও না মুছে যাওয়া নোটিফিকেশন তৈরি করা

  1. অ্যান্ড্রয়েডে বাটন ক্লিক করলে ডায়ালগ বন্ধ না হওয়া নিশ্চিত করা

  2. অ্যান্ড্রয়েডে টেক্সট ফাইল তৈরি করে ডেটা লিখার পদ্ধতি

  3. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  4. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি