অ্যান্ড্রয়েডে নির্দিষ্ট সময়ের পর নোটিফিকেশন অটো-রিমুভ করার পদ্ধতি
এই উদাহরণটি দেখায় কীভাবে একটি Android নোটিফিকেশন তৈরি করতে হয় যা কয়েক সেকেন্ড পর স্বয়ংক্রিয়ভাবে মুছে যাবে। এটি হ্যান্ডলার (Handler) এবং পোস্টডিলেড (postDelayed) মেথড ব্যবহার করে বাস্তবায়ন করা হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio খুলুন, File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল সংশোধন করুন
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-এ লজিক লিখুন
src/MainActivity.java ফাইলে নিচের কোড যোগ করুন। নোট: আধুনিক Android বিকাশের জন্য AndroidX লাইব্রেরি ব্যবহার করা হয়েছে।
package com.example.autoclearnotification;
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 android.os.Handler;
import android.os.Looper;
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(new View.OnClickListener() {
@Override
public void onClick(View v) {
// নোটিফিকেশন ক্লিক করলে মূল অ্যাক্টিভিটি খুলবে
Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
// নোটিফিকেশন বিল্ডার
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, DEFAULT_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("টেস্ট নোটিফিকেশন")
.setContentText("হ্যালো! এটি ৫ সেকেন্ড পর মুছে যাবে।")
.setContentIntent(pendingIntent)
.setAutoCancel(false); // ব্যবহারকারী ক্লিক করলেও না মুছে, টাইমার মুছে দেবে
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Android Oreo (API 26+) এর জন্য নোটিফিকেশন চ্যানেল বাধ্যতামূলক
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "নোটিফিকেশন চ্যানেল", importance);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
// ইউনিক নোটিফিকেশন আইডি (বর্তমান টাইমস্ট্যাম্প)
final int notificationId = (int) System.currentTimeMillis();
if (notificationManager != null) {
notificationManager.notify(notificationId, builder.build());
}
// ৫ সেকেন্ড (৫০০০ মিলিসেকেন্ড) পর নোটিফিকেশন মুছে ফেলা
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (notificationManager != null) {
notificationManager.cancel(notificationId);
}
}, 5000);
}
});
}
}
ধাপ ৪: AndroidManifest.xml আপডেট করুন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.autoclearnotification">
<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.AutoClearNotification">
<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 ডিভাইস কানেক্ট করে Android Studio থেকে Run আইকনে ক্লিক করুন। ডিভাইস সিলেক্ট করার পর "নোটিফিকেশন তৈরি করুন" বাটনে ট্যাপ করুন। নোটিফিকেশন প্রদর্শিত হবে এবং ৫ সেকেন্ড পর স্বয়ংক্রিয়ভাবে গায়ব হয়ে যাবে।
