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

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

स्टेटस बार आइकन छुपाना — VISIBILITY_SECRET

एंड्रॉइड में नोटिफिकेशन का स्टेटस बार आइकन छुपाने के लिए NotificationCompat.VISIBILITY_SECRET उपयोग करें। यह नोटिफिकेशन शेड में तो दिखेगा, लेकिन स्टेटस बार में आइकन नहीं आएगा।

चरण 1: प्रोजेक्ट और मेन लेआउट

सामान्य बटन वाला लेआउट।

चरण 2: कस्टम नोटिफिकेशन लेआउट (आइकन के बिना)

res/layout/custom_notification_layout.xml — इसमें कोई ImageView नहीं:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:padding="10dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:text="Testing"
        android:textColor="#000"
        android:textSize="18sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_marginTop="8dp"
        android:layout_toEndOf="@+id/image"
        android:hint="We are testing without icon"
        android:inputType="text"
        android:textSize="14sp" />
</RelativeLayout>

चरण 3: MainActivity — VISIBILITY_SECRET सेट करें

package app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";

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

    public void createNotification(View view) {
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);

        mBuilder.setContent(contentView);
        mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground); // फिर भी देना पड़ता है
        mBuilder.setAutoCancel(true);
        
        // स्टेटस बार में आइकन न दिखाने की कुंजी
        mBuilder.setVisibility(NotificationCompat.VISIBILITY_SECRET);

        long[] VIBRATE_PATTERN = {0, 500};

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            assert mNotificationManager != null;
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
    }
}

चरण 4: मैनिफेस्ट

मानक।

Visibility लेवल समझें

कॉन्स्टेंटबिहेवियर
VISIBILITY_PUBLICपूरा कंटेंट लॉक स्क्रीन और स्टेटस बार में दिखे (डिफॉल्ट)
VISIBILITY_PRIVATEलॉक स्क्रीन पर केवल ऐप नाम/आइकन दिखे, कंटेंट छुपा
VISIBILITY_SECRETलॉक स्क्रीन पर बिल्कुल न दिखे, स्टेटस बार आइकन भी छुपे

⚠️ महत्वपूर्ण नोट

  • setSmallIcon() फिर भी कॉल करना पड़ता है (API अनिवार्य), लेकिन VISIBILITY_SECRET से स्टेटस बार में नहीं दिखता
  • यह लॉक स्क्रीन पर भी नोटिफिकेशन छुपा देता है
  • एंड्रॉइड 5.0+ (API 21) से सपोर्टेड
  • चैनल importance LOW या MIN होने पर भी स्टेटस बार आइकन नहीं दिखता

बेहतर विकल्प: चैनल Importance LOW

अगर केवल स्टेटस बार आइकन छुपाना है (लॉक स्क्रीन पर दिखे), तो चैनल बनाते समय:

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Name", NotificationManager.IMPORTANCE_LOW);
// या IMPORTANCE_MIN
channel.setShowBadge(false);

यह स्टेटस बार आइकन नहीं दिखाता, लेकिन नोटिफिकेशन शेड में लिस्ट होता है।

  1. एंड्रॉइड में स्टेटस बार की ऊंचाई कैसे प्राप्त करें एंड्रॉइड में स्टेटस बार की ऊंचाई कैसे प्राप्त करें

    एंड्रॉइड में स्टेटस बार की ऊंचाई जानने का तरीका एंड्रॉइड ऐप विकास में कई बार हमें स्टेटस बार की सटीक ऊंचाई जानने की आवश्यकता होती है, खासकर जब हम फुल-स्क्रीन लेआउट या कस्टम UI बना रहे हों। यह उदाहरण दर्शाता है कि प्रोग्रामेटिक रूप से स्टेटस बार की ऊंचाई कैसे प्राप्त की जाए। चरण 1: नया प्रोजेक्ट बना

  1. एंड्रॉइड स्टेटस बार और नोटिफिकेशन आइकन्स समझें – पूरी गाइड एंड्रॉइड स्टेटस बार और नोटिफिकेशन आइकन्स समझें – पूरी गाइड

    क्या आपने कभी एंड्रॉइड स्टेटस बार और नोटिफिकेशन में मौजूद अनजान आइकन्स के बारे में सोचा है? चिंता न करें! हम आपकी मदद के लिए यहां हैं। एंड्रॉइड स्टेटस बार असल में आपके एंड्रॉइड डिवाइस का एक नोटिस बोर्ड है। यह आइकन आपको जिंदगी में होने वाली सभी घटनाओं से अपडेट रखने में मदद करता है। यह नए टेक्स्ट मैस

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

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