NotificationListenerService के साथ नोटिफिकेशन मॉनिटरिंग
NotificationListenerService एक सिस्टम सर्विस है जो ऐप को सभी पोस्टेड/रिमूव्ड नोटिफिकेशन पढ़ने देती है। यूजर को सेटिंग्स में अनुमति देनी पड़ती है।
चरण 1: नया प्रोजेक्ट बनाएं
चरण 2: लिस्टनर इंटरफेस (src/MyListener.java)
public interface MyListener {
void setValue(String packageName);
}
चरण 3: नोटिफिकेशन सर्विस (src/NotificationService.java)
package app.tutorialspoint.com.notifyme;
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 = "NotificationService";
private Context context;
private static MyListener myListener;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i(TAG, "onNotificationPosted: " + sbn.getPackageName());
Log.i(TAG, "ID: " + sbn.getId() + " | टिकर: " + sbn.getNotification().tickerText);
if (myListener != null) {
myListener.setValue("पोस्ट: " + sbn.getPackageName());
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i(TAG, "onNotificationRemoved: " + sbn.getPackageName());
if (myListener != null) {
myListener.setValue("हटाया: " + sbn.getPackageName());
}
}
public static void setListener(MyListener listener) {
myListener = listener;
}
}
चरण 4: मेनू (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"
tools:context=".MainActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="सेटिंग्स"
app:showAsAction="never" />
</menu>
चरण 5: लेआउट (res/layout/activity_main.xml)
<?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="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="नोटिफिकेशन बनाएं" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btnCreateNotification">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="NotificationListenerService उदाहरण"
android:textAppearance="?android:attr/textAppearanceMedium" />
</ScrollView>
</RelativeLayout>
चरण 6: MainActivity.java
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
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 NOTIFICATION_CHANNEL_ID = "10001";
private static final String DEFAULT_CHANNEL_ID = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new NotificationService().setListener(this);
txtView = findViewById(R.id.textView);
Button btnCreateNotification = findViewById(R.id.btnCreateNotification);
btnCreateNotification.setOnClickListener(v -> createTestNotification());
}
private void createTestNotification() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
.setContentTitle("मेरा नोटिफिकेशन")
.setContentText("Notification Listener Service Example")
.setTicker("टिकर टेक्स्ट")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
NOTIFICATION_CHANNEL_ID, "चैनल नाम", NotificationManager.IMPORTANCE_HIGH);
builder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert manager != null;
manager.createNotificationChannel(channel);
}
assert manager != null;
manager.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) {
// नोटिफिकेशन एक्सेस सेटिंग्स खोलें
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setValue(String packageName) {
txtView.append("\n" + packageName);
}
}
चरण 7: AndroidManifest.xml - सर्विस डिक्लेरेशन जरूरी
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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>
सेटअप और टेस्टिंग
- ऐप इंस्टॉल करें
- मेनू → सेटिंग्स पर टैप करें → "नोटिफिकेशन एक्सेस" स्क्रीन खुलेगी
- अपने ऐप के लिए टॉगल ऑन करें
- अब "नोटिफिकेशन बनाएं" बटन दबाएं या अन्य ऐप्स से नोटिफिकेशन प्राप्त करें
- लॉगकैट और ऐप UI में पैकेज नाम दिखेंगे
प्राप्त डेटा
sbn.getPackageName()- नोटिफिकेशन भेजने वाले ऐप का पैकेजsbn.getId()- नोटिफिकेशन IDsbn.getNotification()- पूरा Notification ऑब्जेक्ट (टाइटल, टेक्स्ट, एक्शन आदि)sbn.getPostTime()- पोस्ट समय
⚠️ गोपनीयता और अनुमतियाँ
- यूजर को मैन्युअली अनुमति देनी होगी (सेटिंग्स → ऐप्स → विशेष ऐप एक्सेस → नोटिफिकेशन एक्सेस)
- प्ले स्टोर पॉलिसी: केवल वैध उपयोग के लिए (पैरेंटल कंट्रोल, एक्सेसिबिलिटी, ऑटोमेशन)
- सेंसिटिव कंटेंट (OTP, पासवर्ड) पढ़ने से बचें
- Android 11+ में
getActiveNotifications()से मौजूदा नोटिफिकेशन भी पढ़ सकते हैं