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

অ্যাক্টিভিটি ক্র্যাশ হলে Android নোটিফিকেশন মুছে ফেলা


UncaughtExceptionHandler দিয়ে ক্র্যাশ হ্যান্ডলিং ও নোটিফিকেশন ক্লিনআপ

যদি অ্যাপ ক্র্যাশ করে, তখনও নোটিফিকেশন থাকতে পারে। এটি রিসোর্স লিক এবং খারাপ UX তৈরি করে। `Thread.setDefaultUncaughtExceptionHandler()` ব্যবহার করে গ্লোবাল ক্র্যাশ হ্যান্ডলার সেট করে নোটিফিকেশন মুছে ফেলা যায়।

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

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 - নোটিফিকেশন তৈরি

package com.example.crashclearnotification;

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;

public class MainActivity extends AppCompatActivity {

    public static final String CHANNEL_ID = "crash_channel";
    private static final String DEFAULT_CH = "default";
    public static final int NOTIFICATION_ID = 12345; // ফিক্সড আইডি ক্র্যাশ হ্যান্ডলারের জন্য

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

        // গ্লোবাল ক্র্যাশ হ্যান্ডলার সেট করুন (Application class-এ করা ভালো)
        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));

        findViewById(R.id.btnCreateNotification).setOnClickListener(this::createNotification);
    }

    public void createNotification(View view) {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.putExtra("fromNotification", true);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
                .setContentTitle("ক্র্যাশ-সেফ নোটিফিকেশন")
                .setContentIntent(pendingIntent)
                .setContentText("যদি অ্যাপ ক্র্যাশ করেও নোটিফিকেশন মুছে যাবে।")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setAutoCancel(true);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Crash Channel", NotificationManager.IMPORTANCE_HIGH);
            builder.setChannelId(CHANNEL_ID);
            if (nm != null) nm.createNotificationChannel(ch);
        }

        if (nm != null) nm.notify(NOTIFICATION_ID, builder.build()); // ফিক্সড আইডি
    }
}

ধাপ ৪: CrashHandler.java - ক্র্যাশ হ্যান্ডলার ক্লাস

package com.example.crashclearnotification;

import android.app.NotificationManager;
import android.content.Context;

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static final int NOTIFICATION_ID = 12345; // MainActivity-র সাথে মিল খইলে
    private final Thread.UncaughtExceptionHandler defaultHandler;
    private final NotificationManager notificationManager;

    public CrashHandler(Context context) {
        this.defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        this.notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // ক্র্যাশ হলে নোটিফিকেশন মুছে ফেলা
        if (notificationManager != null) {
            try {
                notificationManager.cancel(NOTIFICATION_ID);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
        // ডিফল্ট হ্যান্ডলার কল করুন (ক্র্যাশ ডায়ালগ দেখাবে/অ্যাপ বন্ধ করবে)
        if (defaultHandler != null) {
            defaultHandler.uncaughtException(thread, ex);
        }
    }
}

ধাপ ৫: AndroidManifest.xml

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

    <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.CrashClearNotification">

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

বेहত্তর পদ্ধতি: Application Class-এ হ্যান্ডলার সেট করা

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));
    }
}

// Manifest-এ android:name=".MyApplication" যোগ করুন

পরীক্ষা

নোটিফিকেশন তৈরি করুন → অ্যাপে ইচ্ছাকৃত ক্র্যাশ তৈরি করুন (যেমন: `throw new RuntimeException("Test crash")`) → নোটিফিকেশন মুছে যাবে।

  1. অ্যান্ড্রয়েডে নোটিফিকেশন থেকে অ্যাক্টিভিটি কল হয়েছে কিনা তা কীভাবে জানবেন?

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

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

  4. এন্ড্রয়েডে অ্যাক্টিভিটি রিস্টার্ট করার পদ্ধতি