Foreground Service ও NotificationListenerService ব্যবহার করে ব্যাকগ্রাউন্ড নোটিফিকেশন
যখন অ্যাপটি সম্পূর্ণ বন্ধ (swipe away from recent) থাকে, তখনও নোটিফিকেশন পাওয়ার জন্য **Foreground Service** বা **WorkManager** / **AlarmManager** ব্যবহার করা হয়। নিচের উদাহরণে একটি `Service` ব্যবহার করা হয়েছে যা অ্যাপ বন্ধ হওয়ার পরও রান করে এবং পরিয়ডিক্যালি নোটিফিকেশন দেখায়।
ধাপ ১: প্রজেক্ট তৈরি
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="closeApp"
android:text="অ্যাপ বন্ধ করুন (নোটিফিকেশন পাওয়ার জন্য)" />
</RelativeLayout>
ধাপ ৩: MainActivity.java - onStop-এ সার্ভিস শুরু
package com.example.backgroundnotification;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStop() {
super.onStop();
// অ্যাপ ব্যাকগ্রাউন্ডে গেলে বা বন্ধ হলে সার্ভিস শুরু
startService(new Intent(this, NotificationService.class));
}
public void closeApp(View view) {
finish(); // অ্যাপ বন্ধ করুন
}
}
ধাপ ৪: NotificationService.java - টাইমার দিয়ে পরিয়ডিক্যালি নোটিফিকেশন
package com.example.backgroundnotification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class NotificationService extends Service {
public static final String CHANNEL_ID = "bg_notif_channel";
private static final String DEFAULT_CH = "default";
private Timer timer;
private TimerTask timerTask;
private static final String TAG = "NotificationService";
private static final int INTERVAL_SECS = 5;
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
startTimer();
return START_STICKY; // সিস্টেম কিল করলে পুনরায় শুরু হবে
}
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
stopTimerTask();
super.onDestroy();
}
private void startTimer() {
timer = new Timer();
initializeTimerTask();
timer.schedule(timerTask, 5000, INTERVAL_SECS * 1000L);
}
private void stopTimerTask() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
private void initializeTimerTask() {
timerTask = new TimerTask() {
@Override
public void run() {
new Handler(Looper.getMainLooper()).post(this::createNotification);
}
};
}
private void createNotification() {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), DEFAULT_CH)
.setContentTitle("ব্যাকগ্রাউন্ড নোটিফিকেশন")
.setContentText("অ্যাপ বন্ধ থাকলেও এটি আসছে!")
.setTicker("নতুন নোটিফিকেশন")
.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, "ব্যাকগ্রাউন্ড চ্যানেল", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(CHANNEL_ID);
if (nm != null) nm.createNotificationChannel(ch);
}
if (nm != null) nm.notify((int) System.currentTimeMillis(), builder.build());
}
}
ধাপ ৫: AndroidManifest.xml - সার্ভিস ডিক্লেয়ার ও পারমিশন
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.backgroundnotification">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Android 14+ জন্য ফোরগ্রাউন্ড সার্ভিস টাইপ -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.BackgroundNotification">
<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>
<service
android:name=".NotificationService"
android:exported="false"
android:foregroundServiceType="dataSync">
<intent-filter>
<action android:name="com.example.backgroundnotification.NotificationService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
গুরুত্বপূর্ণ নোট
- Android 8.0+ (Oreo) থেকে ব্যাকগ্রাউন্ড এক্সিকিউশন লিমিট আছে। সার্ভিস ফোরগ্রাউন্ডে চালাতে হলে `startForeground()` কল করতে হয়।
- আধুনিক Android-এ (12+) `WorkManager` বা `AlarmManager` (with exact alarms) ব্যবহার করা ভালো praktyice।
- ব্যবহারকারী অ্যাপকে "Force Stop" করলে সার্ভিস রান করবে না।
পরীক্ষা
অ্যাপ ইনস্টল করুন → "অ্যাপ বন্ধ করুন" বাটন চাপুন → ৫ সেকেন্ড অপেক্ষা করুন। নোটিফিকেশন আসবে অ্যাপ বন্ধ থাকলেও।