स्टेटस बार आइकन छुपाना — 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);
यह स्टेटस बार आइकन नहीं दिखाता, लेकिन नोटिफिकेशन शेड में लिस्ट होता है।