পারসিস্টেন্ট নোটিফিকেশন: বুট কমপ্লিট হ্যান্ডলিং
সাধারণত ডিভাইস রিবুট হলে সব নোটিফিকেশন ক্লিয়ার হয়ে যায়। এই টিউটোরিয়ালে দেখানো হয়েছে কীভাবে BOOT_COMPLETED ব্রডকাস্ট রিসিভার ব্যবহার করে রিবুটের পর আটোমেটিক্যালি নোটিফিকেশন পুনরায় gösterা যায়।
কীবোর্ড কম্পোনেন্ট
- USBStateReceiver (ব্রডকাস্ট রিসিভার) — BOOT_COMPLETED ও USB_STATE লিসেন করে
- NotificationManager — নোটিফিকেশন পোস্ট/ক্যাঞ্চেল
- Notification Channel — Android 8.0+ এর জন্য বাধ্যতামূলক
- Static notification ID — একই ID দিয়ে আপডেট/ক্যাঞ্চেল
ধাপ ১: প্রজেক্ট সেটআপ
Package: com.example.persistentnotification
ধাপ ২: লেআউট (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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="নোটিফিকেশন বুট কমপ্লিট után আটোমেটিক্যালি দেখাবে"
android:textSize="16sp"
android:gravity="center"
android:padding="16dp"/>
</RelativeLayout>
ধাপ ৩: MainActivity.java (সাদা রাখুন)
package com.example.persistentnotification;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
ধাপ ৪: USBStateReceiver.java — মূল লজিক
package com.example.persistentnotification;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import androidx.core.app.NotificationCompat;
public class USBStateReceiver extends BroadcastReceiver {
public static final String CHANNEL_ID = "persistent_notif_channel";
private static final String DEFAULT_CHANNEL = "default";
private static final String PREFS_NAME = "NotifState";
private static final String KEY_CONNECTED = "connected";
private static final int NOTIFICATION_ID = 1;
@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("PersistentNotif", "Received action: " + action);
if (action == null) return;
NotificationManager manager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
// চ্যানেল নিশ্চিত করুন
ensureChannel(manager);
// শেয়ার্ড প্রিফারেন্স থেকে স্টেট লোড
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean connected = prefs.getBoolean(KEY_CONNECTED, true);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DEFAULT_CHANNEL)
.setContentTitle("USB - নোটিফিকেশন")
.setContentText(connected ? "সংযুক্ত" : "অসংযুক্ত")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(false) // পারসিস্টেন্ট রাখতে false
.setOngoing(true) // Ongoing নোটিফিকেশন (সোয়াইপ করে যাবে না)
.setChannelId(CHANNEL_ID);
Notification notification = builder.build();
if (Intent.ACTION_BOOT_COMPLETED.equals(action) ||
Intent.ACTION_USB_STATE.equals(action)) {
if (connected) {
manager.notify(NOTIFICATION_ID, notification);
prefs.edit().putBoolean(KEY_CONNECTED, false).apply();
} else {
manager.cancel(NOTIFICATION_ID);
prefs.edit().putBoolean(KEY_CONNECTED, true).apply();
}
}
}
private void ensureChannel(NotificationManager manager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, "পারসিস্টেন্ট নোটিফিকেশন",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("রিবুটের পরও কায়ম থাকবে");
manager.createNotificationChannel(channel);
}
}
}
ধাপ ৫: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.persistentnotification">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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.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>
<receiver
android:name=".USBStateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
গুরুত্বপূর্ণ নোট: Android 8.0+ (API 26+) এ পরিবর্তন
- Ongoing notifications (setOngoing(true)) ফোরগ্রাউন্ড সার্ভিস ছাড়া লিমিটেড。
- Android 14+ (API 34) এ ফোরগ্রাউন্ড সার্ভিস টাইপ
FOREGROUND_SERVICE_TYPE_SPECIAL_USEবা উপযুক্ত টাইপ ডিক্লেয়ার করতে হয়। - বेहतर প্যাটার্ন: Foreground Service +
Service.startForeground()ব্যবহার করুন।
উন্নত সংস্করণ: Foreground Service প্যাটার্ন
// Manifest-এ
<service
android:name=".PersistentForegroundService"
android:foregroundServiceType="specialUse"
android:exported="false" />
// Service-এ
public class PersistentForegroundService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = createNotification();
startForeground(1, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE);
return START_STICKY;
}
// ...
}
// BootReceiver-এ
Intent serviceIntent = new Intent(context, PersistentForegroundService.class);
ContextCompat.startForegroundService(context, serviceIntent);
পরীক্ষা পদ্ধতি
- অ্যাপ ইনস্টল করুন → একবার খুলুন (রিসিভার রেজিস্টার হবে)
- ডিভাইস রিবুট করুন
- লক স্ক্রিন বা নোটিফিকেশন প্যানেলে "USB - নোটিফিকেশন" দেখুন
- USB ক্যাবল লাগিয়ে/খুলে স্টেট পরিবর্তন চেক করুন
সাধারণ সমস্যা ও সমাধান
| সমস্যা | সমাধান |
|---|---|
| রিবুটের পর নোটিফিকেশন আসে না | Receiver exported=true, BOOT_COMPLETED action есть, অ্যাপ একবার লঞ্চ করা হয়েছে |
| নোটিফিকেশন সোয়াইপ করলে চলে যায় | setOngoing(true) সেট করুন, বা Foreground Service ব্যবহার করুন |
| Android 13+ তে নোটিফিকেশন দেখাচ্ছে না | POST_NOTIFICATIONS রানটাইম পারমিশন চেক করুন |