AlarmManager + BroadcastReceiver দিয়ে ভবিষ্যতে নোটিফিকেশন শিডিউল করা
নির্দিষ্ট সময় পর (৫, ১০, ৩০ সেকেন্ড) নোটিফিকেশন দেখানোর জন্য `AlarmManager` এবং `BroadcastReceiver` কফিনেশন ব্যবহার করা হয়। এটি অ্যাপ বন্ধ থাকলেও কাজ করে।
ধাপ ১: প্রজেক্ট তৈরি
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" />
ধাপ ৩: মেনু রিসোর্স - টাইম সিলেকশন
res/menu/main_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_5" android:title="৫ সেকেন্ড" app:showAsAction="never" />
<item android:id="@+id/action_10" android:title="১০ সেকেন্ড" app:showAsAction="never" />
<item android:id="@+id/action_30" android:title="৩০ সেকেন্ড" app:showAsAction="never" />
</menu>
ধাপ ৪: MainActivity.java - শিডিউল লজিক
package com.example.schedulednotification;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_ID = "scheduled_channel";
private static final String DEFAULT_CH = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int delay = 0;
String msg = "";
if (item.getItemId() == R.id.action_5) { delay = 5000; msg = "৫ সেকেন্ড विलম্ব"; }
else if (item.getItemId() == R.id.action_10) { delay = 10000; msg = "১০ সেকেন্ড विलম্ব"; }
else if (item.getItemId() == R.id.action_30) { delay = 30000; msg = "৩০ সেকেন্ড विलম্ব"; }
else return super.onOptionsItemSelected(item);
scheduleNotification(getNotification(msg), delay);
return true;
}
private void scheduleNotification(Notification notification, int delay) {
Intent intent = new Intent(this, MyNotificationPublisher.class);
intent.putExtra(MyNotificationPublisher.EXTRA_NOTIFICATION_ID, 1);
intent.putExtra(MyNotificationPublisher.EXTRA_NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
long futureMillis = SystemClock.elapsedRealtime() + delay;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureMillis, pendingIntent);
}
}
private Notification getNotification(String content) {
return new NotificationCompat.Builder(this, DEFAULT_CH)
.setContentTitle("শিডিউল্ড নোটিফিকেশন")
.setContentText(content)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true)
.setChannelId(CHANNEL_ID)
.build();
}
}
ধাপ ৫: MyNotificationPublisher.java - BroadcastReceiver
package com.example.schedulednotification;
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 static com.example.schedulednotification.MainActivity.CHANNEL_ID;
public class MyNotificationPublisher extends BroadcastReceiver {
public static final String EXTRA_NOTIFICATION_ID = "notification-id";
public static final String EXTRA_NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(EXTRA_NOTIFICATION);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Scheduled Channel", NotificationManager.IMPORTANCE_HIGH);
if (nm != null) nm.createNotificationChannel(ch);
}
int id = intent.getIntExtra(EXTRA_NOTIFICATION_ID, 0);
if (nm != null && notification != null) nm.notify(id, notification);
}
}
ধাপ ৬: AndroidManifest.xml - রিসিভার রেজিস্টার
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.schedulednotification">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> <!-- Android 12+ -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.ScheduledNotification">
<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=".MyNotificationPublisher"
android:exported="false" />
</application>
</manifest>
গুরুত্বপূর্ণ নোট
- Android 12 (API 31)+ এ `SCHEDULE_EXACT_ALARM` পারমিশন লাগে।
- Doze mode / App Standby-এ `setExactAndAllowWhileIdle()` বা `setAlarmClock()` ব্যবহার করুন।
- বেশি নির্ভরযোগ্য শিডিউলিংয়ের জন্য `WorkManager` ব্যবহার করুন।
পরীক্ষা
অ্যাপ রান করুন → অপশন মেনু থেকে ৫/১০/৩০ সেকেন্ড সিলেক্ট করুন → নির্দিষ্ট সময় পর নোটিফিকেশন আসবে।
