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

অ্যান্ড্রয়েডে লোকাল নোটিফিকেশন তৈরি করা


সার্ভার ছাড়া ডিভাইসেই নোটিফিকেশন জেনারেট করা

লোকাল নোটিফিকেশন মানে সার্ভার (FCM/GCM) ছাড়া অ্যাপ নিজে থেকে নির্দিষ্ট সময় বা ইভেন্টে নোটিফিকেশন তৈরি করে। এটি রিমাইন্ডার, এলার্ম, অফলাইন অ্যাপের জন্য আদর্শ।

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

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.localnotification;

import android.app.NotificationChannel;
import android.app.NotificationManager;
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 = "local_notif_channel";
    private static final String DEFAULT_CH = "default";

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

    public void createNotification(View view) {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, 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, "Local Notifications", 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.localnotification">

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

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

উন্নত লোকাল নোটিফিকেশন (বোনাস)

নির্দিষ্ট সময়ে নোটিফিকেশন देखने জন্য AlarmManager বা WorkManager ব্যবহার করুন। নিচে একটি ছোট উদাহরণ:

// ১০ সেকেন্ড পর নোটিফিকেশন শিডিউল করা
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, NotificationPublisher.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
long triggerTime = System.currentTimeMillis() + 10_000;
alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);

পরীক্ষা

অ্যাপ রান করুন এবং বাটন চাপুন। তাত্ক্ষণিক নোটিফিকেশন আসবে। ইন্টারনেট ছাড়াও কাজ করবে।

অ্যান্ড্রয়েডে লোকাল নোটিফিকেশন তৈরি করা অ্যান্ড্রয়েডে লোকাল নোটিফিকেশন তৈরি করা

  1. অ্যান্ড্রয়েড অ্যাপে WebView তৈরির পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  3. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি

  4. এন্ড্রয়েড ৮ Snooze মাস্টার করুন: ভালো ঘুমের জন্য নোটিফিকেশন ম্যানেজ করুন