लॉक स्क्रीन पर नोटिफिकेशन छुपाना (VISIBILITY_SECRET + KeyguardManager)
नोटिफिकेशन नोटिफिकेशन शेड में दिखे लेकिन लॉक स्क्रीन पर न दिखे, इसके लिए दो तरीके हैं: चैनल importance LOW/MIN, या VISIBILITY_SECRET + रनटाइम चेक।
चरण 1-2: प्रोजेक्ट, मेन लेआउट, कस्टम लेआउट
पिछले आर्टिकल्स जैसे — कस्टम RemoteViews लेआउट।
चरण 3: MainActivity — KeyguardManager से लॉक चेक
package app.tutorialspoint.com.notifyme;
import android.app.KeyguardManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
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);
// KeyguardManager से चेक करें कि डिवाइस लॉक है या नहीं
KeyguardManager keyguardManager = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardLocked()) {
// लॉक होने पर प्रायोरिटी MIN कर दें (स्टेटस बार आइकन भी छुपेगा)
mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
}
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());
}
}
बेहतर और आधुनिक तरीका: चैनल Importance LOW
कोड में रनटाइम चेक के बजाय चैनल बनाते समय importance सेट करें:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// IMPORTANCE_LOW — स्टेटस बार में आइकन नहीं, लॉक स्क्रीन पर नहीं, शेड में नीचे
// IMPORTANCE_MIN — केवल शेड में, कोई आवाज/वाइब्रेशन/पीक नहीं
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Silent Channel", importance);
channel.setShowBadge(false);
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); // लॉक स्क्रीन पर छुपाएं
mNotificationManager.createNotificationChannel(channel);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
}
Lockscreen Visibility ऑप्शन
| कॉन्स्टेंट | लॉक स्क्रीन पर |
|---|---|
VISIBILITY_PUBLIC | पूरा कंटेंट दिखे |
VISIBILITY_PRIVATE | केवल ऐप नाम/आइकन, कंटेंट छुपा |
VISIBILITY_SECRET | कुछ न दिखे (नोटिफिकेशन पूरी तरह छुपा) |
यूजर कंट्रोल
यूजर सेटिंग्स → ऐप्स → आपका ऐप → नोटिफिकेशन → "लॉक स्क्रीन पर दिखाएं" से ओवरराइड कर सकता है। ऐप केवल डिफॉल्ट सेट कर सकता है।
मैनिफेस्ट
मानक।