Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में स्टेटस बार नोटिफिकेशन कैसे इंटरसेप्ट/पढ़ें?

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>

सेटअप और टेस्टिंग

  1. ऐप इंस्टॉल करें
  2. मेनू → सेटिंग्स पर टैप करें → "नोटिफिकेशन एक्सेस" स्क्रीन खुलेगी
  3. अपने ऐप के लिए टॉगल ऑन करें
  4. अब "नोटिफिकेशन बनाएं" बटन दबाएं या अन्य ऐप्स से नोटिफिकेशन प्राप्त करें
  5. लॉगकैट और ऐप UI में पैकेज नाम दिखेंगे

प्राप्त डेटा

  • sbn.getPackageName() - नोटिफिकेशन भेजने वाले ऐप का पैकेज
  • sbn.getId() - नोटिफिकेशन ID
  • sbn.getNotification() - पूरा Notification ऑब्जेक्ट (टाइटल, टेक्स्ट, एक्शन आदि)
  • sbn.getPostTime() - पोस्ट समय

⚠️ गोपनीयता और अनुमतियाँ

  • यूजर को मैन्युअली अनुमति देनी होगी (सेटिंग्स → ऐप्स → विशेष ऐप एक्सेस → नोटिफिकेशन एक्सेस)
  • प्ले स्टोर पॉलिसी: केवल वैध उपयोग के लिए (पैरेंटल कंट्रोल, एक्सेसिबिलिटी, ऑटोमेशन)
  • सेंसिटिव कंटेंट (OTP, पासवर्ड) पढ़ने से बचें
  • Android 11+ में getActiveNotifications() से मौजूदा नोटिफिकेशन भी पढ़ सकते हैं
  1. Android Nougat स्टेटस बार और नोटिफिकेशन कैसे प्राप्त करें Android Nougat स्टेटस बार और नोटिफिकेशन कैसे प्राप्त करें

    Android Nougat किसी समय बाद में इस वर्ष लॉन्च नहीं होगा, और यहां तक कि जब ऐसा होता है, तो यह कई और महीने हो सकते हैं इससे पहले कि यह आपके डिवाइस के लिए एक कस्टम ROM में बनाया जाए, या एक फ्लैगशिप फोन पर शिप किया जाए। तब तक, यदि आप प्रिव्यू इंस्टॉल करने में असमर्थ हैं, तो अगली सबसे अच्छी चीज जो आप कर

  1. Access में Status Bar कैसे दिखाएं या छिपाएं Access में Status Bar कैसे दिखाएं या छिपाएं

    स्टेटस बार एक क्षैतिज विंडो है जो विंडो के नीचे होती है, और यह विभिन्न प्रकार की स्थिति जानकारी प्रदर्शित करती है। Access Status Bar पर, आप स्टेटस बार पर उपलब्ध कंट्रोल का उपयोग करके सक्रिय विंडो को उपलब्ध व्यू में से एक के बीच स्विच कर सकते हैं। Access में Status Bar कैसे दिखाएं या छिपाएं Access म

  1. एंड्रॉइड स्टेटस और नोटिफिकेशन बार को मैन्युअली कस्टमाइज़ करने की चरण-दर-चरण गाइड एंड्रॉइड स्टेटस और नोटिफिकेशन बार को मैन्युअली कस्टमाइज़ करने की चरण-दर-चरण गाइड

    एंड्रॉइड अब तक डिज़ाइन किए गए सबसे अनुकूलन योग्य ऑपरेटिंग सिस्टम में से एक है। यह अन्य मोबाइल OS की तुलना में व्यक्तिगत विकल्प प्रदान करता है। वे आधिकारिक Google Play Store पर जा सकते हैं और Customization खोज सकते हैं। सैकड़ों ऐप्स विभिन्न श्रेणियों में प्रदर्शित होंगे, जिनमें लॉन्चर ऐप्स, आइकन पैक्