यह गाइड **NotificationListenerService** का उपयोग करके सिस्टम-स्तरीय नोटिफिकेशन इवेंट (पोस्ट, हटाना, अपडेट) का पता लगाने का तरीका बताता है। यह अन्य ऐप्स के नोटिफिकेशन की निगरानी करने, कस्टम नोटिफिकेशन प्रबंधन बनाने या स्वचालन ट्रिगर करने के लिए उपयोगी है।
मुख्य घटक: NotificationListenerService
एक सिस्टम सेवा जो ऐप्स को स्टेटस बार नोटिफिकेशन पर इवेंट प्राप्त करने की अनुमति देती है। इसके लिए उपयोगकर्ता को सेटिंग्स में "नोटिफिकेशन एक्सेस" देना आवश्यक है।
चरण 1: प्रोजेक्ट सेटअप
एंड्रॉइड स्टूडियो में Empty Activity प्रोजेक्ट बनाएँ।
चरण 2: लिस्टनर इंटरफेस (MyListener.java)
package com.example.notificationlistener;
public interface NotificationListener {
void onNotificationPosted(String packageName, String title, String text);
void onNotificationRemoved(String packageName);
}
चरण 3: NotificationListenerService कार्यान्वयन
package com.example.notificationlistener;
import android.content.Context;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
public class NotificationMonitorService extends NotificationListenerService {
private static final String TAG = "NotificationMonitor";
private static NotificationListener listener;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "नोटिफिकेशन मॉनिटर सर्विस बनाई गई");
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
String packageName = sbn.getPackageName();
String title = "";
String text = "";
if (sbn.getNotification().extras != null) {
title = sbn.getNotification().extras.getString(android.app.Notification.EXTRA_TITLE, "");
text = sbn.getNotification().extras.getCharSequence(android.app.Notification.EXTRA_TEXT, "").toString();
}
Log.i(TAG, "पोस्ट किया गया: " + packageName + " | " + title + " | " + text);
if (listener != null) {
listener.onNotificationPosted(packageName, title, text);
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
String packageName = sbn.getPackageName();
Log.i(TAG, "हटाया गया: " + packageName);
if (listener != null) {
listener.onNotificationRemoved(packageName);
}
}
public static void setListener(NotificationListener listener) {
NotificationMonitorService.listener = listener;
}
@Override
public void onDestroy() {
super.onDestroy();
listener = null;
Log.d(TAG, "सर्विस नष्ट");
}
}
चरण 4: लेआउट (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/btnTestNotification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="टेस्ट नोटिफिकेशन बनाएँ"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnOpenSettings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="नोटिफिकेशन एक्सेस सेटिंग्स खोलें"
app:layout_constraintTop_toBottomOf="@id/btnTestNotification"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/tvLog"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:text="नोटिफिकेशन इवेंट यहाँ दिखाई देंगे..."
android:textSize="14sp"
android:background="@android:color/darker_gray"
android:padding="8dp"
app:layout_constraintTop_toBottomOf="@id/btnOpenSettings"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 5: 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.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements NotificationListener {
private TextView tvLog;
private static final String CHANNEL_ID = "test_channel";
private static final String CHANNEL_NAME = "टेस्ट नोटिफिकेशन";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvLog = findViewById(R.id.tvLog);
NotificationMonitorService.setListener(this);
Button btnTest = findViewById(R.id.btnTestNotification);
btnTest.setOnClickListener(v -> createTestNotification());
Button btnSettings = findViewById(R.id.btnOpenSettings);
btnSettings.setOnClickListener(v -> openNotificationAccessSettings());
}
private void createTestNotification() {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("टेस्ट नोटिफिकेशन")
.setContentText("यह NotificationListenerService को ट्रिगर करेगा")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify((int) System.currentTimeMillis(), builder.build());
}
private void createNotificationChannel() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
}
private void openNotificationAccessSettings() {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);
}
@Override
public void onNotificationPosted(String packageName, String title, String text) {
runOnUiThread(() -> {
String log = "पोस्ट किया गया: " + packageName + "\nशीर्षक: " + title + "\nपाठ: " + text + "\n---\n";
tvLog.setText(log + tvLog.getText());
});
}
@Override
public void onNotificationRemoved(String packageName) {
runOnUiThread(() -> {
String log = "हटाया गया: " + packageName + "\n---\n";
tvLog.setText(log + tvLog.getText());
});
}
@Override
protected void onDestroy() {
super.onDestroy();
NotificationMonitorService.setListener(null);
}
}
चरण 6: AndroidManifest.xml - महत्वपूर्ण अनुमति
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.notificationlistener">
<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.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=".NotificationMonitorService"
android:label="@string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
चरण 7: उपयोगकर्ता सक्षम करना (आवश्यक)
ऐप इंस्टॉल करने के बाद, उपयोगकर्ता को करना होगा:
- सेटिंग्स → ऐप्स और नोटिफिकेशन → विशेष ऐप एक्सेस → नोटिफिकेशन एक्सेस खोलें
- सूची में अपना ऐप ढूंढें
- टॉगल **चालू** करें
उपरोक्त कोड में "नोटिफिकेशन एक्सेस सेटिंग्स खोलें" बटन उपयोगकर्ता को सीधे वहाँ ले जाता है।
प्राप्त इवेंट प्रकार
| विधि | ट्रिगर | उपयोग का मामला |
|---|---|---|
onNotificationPosted() | नई नोटिफिकेशन दिखाई देती है | लॉगिंग, फ़िल्टरिंग, ऑटो-उत्तर, बैज अपडेट |
onNotificationRemoved() | नोटिफिकेशन खारिज होती है | ट्रैकिंग, सफाई |
onNotificationRankingUpdate() | रैंकिंग/प्राथमिकता बदलती है | उन्नत क्रमबद्ध करना |
महत्वपूर्ण नोट्स
- गोपनीयता: उपयोगकर्ता को स्पष्ट रूप से सहमति देनी होगी; संवेदनशील डेटा को जिम्मेदारी से संभालें
- प्रदर्शन: कॉलबैक में भारी कार्य न करें; कार्य को वर्कर थ्रेड पर भेजें
- सिस्टम नोटिफिकेशन: सिस्टम UI नोटिफिकेशन (वॉल्यूम, रोटेशन) भी प्राप्त होते हैं - पैकेज नाम द्वारा फ़िल्टर करें
- Android 12+: कुछ नोटिफिकेशन सामग्री के लिए अतिरिक्त अनुमतियों की आवश्यकता हो सकती है
उन्नत: नोटिफिकेशन सामग्री तक पहुँच
// पूर्ण नोटिफिकेशन ऑब्जेक्ट प्राप्त करें Notification notification = sbn.getNotification(); // बड़े टेक्स्ट, चित्र, क्रियाएँ आदि निकालें Bundle extras = notification.extras; String bigText = extras.getString(Notification.EXTRA_BIG_TEXT); CharSequence[] lines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES); Bitmap largeIcon = extras.getParcelable(Notification.EXTRA_LARGE_ICON); // क्रियाएँ जांचें Notification.Action[] actions = notification.actions;