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

নোটিফিকেশন পোস্ট হলে 알림 পাওয়া (NotificationListenerService)


NotificationListenerService দিয়ে সিস্টেম-ওয়াইড নোটিফিকেশন শোনা

Android-এ অন্য অ্যাপের নোটিফিকেশন ডিটেক্ট করতে `NotificationListenerService` ব্যবহার করা হয়। এটি একটি সিস্টেম-লেভেল সার্ভিস যা ব্যবহারকারীর অনুমতি (Settings → Notification access) পেলে সকল নোটিফিকেশন পাড় করতে পারে।

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

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লিসেনার ইন্টারফেস

src/MyListener.java — কলব্যাক ইন্টারফেস:

package com.example.notificationlistener;

public interface MyListener {
    void onNotificationPosted(String packageName);
    void onNotificationRemoved(String packageName);
}

ধাপ ৩: NotificationListenerService বাস্তবায়ন

src/NotificationService.java:

package com.example.notificationlistener;

import android.content.Context;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

public class NotificationService extends NotificationListenerService {

    private static final String TAG = "NotifListenerService";
    private static MyListener listener;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        String pkg = sbn.getPackageName();
        String text = sbn.getNotification().extras.getString("android.text", "");
        Log.i(TAG, "Posted: " + pkg + " - " + text);
        if (listener != null) listener.onNotificationPosted("Posted: " + pkg);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        String pkg = sbn.getPackageName();
        Log.i(TAG, "Removed: " + pkg);
        if (listener != null) listener.onNotificationRemoved("Removed: " + pkg);
    }

    public static void setListener(MyListener l) {
        listener = l;
    }
}

ধাপ ৪: মেনু - সেটিংস খোলার অপশন

res/menu/menu_main.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_settings"
        android:title="Notification Access Settings"
        app:showAsAction="never" />
</menu>

ধাপ ৫: লেআউট - টেক্সট ভিউ লগ দেখানোর জন্য

<?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_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:text="Test Notification Create" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btnCreateNotification"
        android:layout_alignStart="@id/btnCreateNotification"
        android:layout_alignEnd="@id/btnCreateNotification"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="NotificationListenerService Example"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </ScrollView>
</RelativeLayout>

ধাপ ৬: MainActivity.java - লিসেনার ইমপ্লিমেন্ট

package com.example.notificationlistener;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements MyListener {

    private TextView txtView;
    public static final String CHANNEL_ID = "listener_channel";
    private static final String DEFAULT_CH = "default";

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

        txtView = findViewById(R.id.textView);
        new NotificationService().setListener(this);

        Button btn = findViewById(R.id.btnCreateNotification);
        btn.setOnClickListener(v -> {
            NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
                    .setContentTitle("Test Notification")
                    .setContentText("Listener Service Test")
                    .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, "Listener Channel", NotificationManager.IMPORTANCE_HIGH);
                builder.setChannelId(CHANNEL_ID);
                if (nm != null) nm.createNotificationChannel(ch);
            }

            if (nm != null) nm.notify((int) System.currentTimeMillis(), builder.build());
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_settings) {
            // Notification Access Settings খোলা
            startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onNotificationPosted(String packageName) {
        txtView.append("\n" + packageName);
    }

    @Override
    public void onNotificationRemoved(String packageName) {
        txtView.append("\n" + packageName);
    }
}

ধাপ ৭: AndroidManifest.xml - সার্ভিস ডিক্লেয়ার ও পারমিশন

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.notificationlistener">

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

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

        <!-- NotificationListenerService ডিক্লেয়ারেশন -->
        <service
            android:name=".NotificationService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
            <intent-filter>
                <action android:name="android.service.notification.NotificationListenerService" />
            </intent-filter>
        </service>

    </application>
</manifest>

চলাচল

  1. অ্যাপ ইন্সটল করুন।
  2. Settings → Apps → Special app access → Notification access → আপনার অ্যাপ অন করুন।
  3. অ্যাপ খুলুন, "Test Notification Create" চাপুন।
  4. লগে দেখবেন নোটিফিকেশন পোস্ট/রিমুভ ইভেন্ট।
  5. অন্য অ্যাপের নোটিফিকেশন আসলে ও শোনাবে।

নোটিফিকেশন পোস্ট হলে 알림 পাওয়া (NotificationListenerService) নোটিফিকেশন পোস্ট হলে 알림 পাওয়া (NotificationListenerService)

  1. নতুন Android নোটিফিকেশন ডিটেক্ট করা (NotificationListenerService)

  2. Android-এ Notification.deleteIntent কীভাবে ব্যবহার করবেন

  3. Android-এ রিমাইন্ডার নোটিফিকেশন তৈরি করার পদ্ধতি

  4. অ্যান্ড্রয়েডে Spinner ভ্যালু পাওয়ার পদ্ধতি