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

लॉक स्क्रीन पर नोटिफिकेशन छुपाएं लेकिन नोटिफिकेशन एरिया में रखें

लॉक स्क्रीन पर नोटिफिकेशन छुपाना (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कुछ न दिखे (नोटिफिकेशन पूरी तरह छुपा)

यूजर कंट्रोल

यूजर सेटिंग्स → ऐप्स → आपका ऐप → नोटिफिकेशन → "लॉक स्क्रीन पर दिखाएं" से ओवरराइड कर सकता है। ऐप केवल डिफॉल्ट सेट कर सकता है।

मैनिफेस्ट

मानक।

  1. मैक को लॉक कैसे करें मैक को लॉक कैसे करें

    सुरक्षा इन दिनों टेक्नोलॉजी का उपयोग करते समय एक दुर्भाग्यपूर्ण आवश्यकता है। हमारे मैक, iPhone, और iPad के माध्यम से भारी मात्रा में व्यक्तिगत जानकारी सुलभ है, इसलिए उन्हें सुरक्षित रखना आवश्यक है भले ही केवल एक या दो मिनट के लिए उन्हें अनअटेंडेड छोड़ रहे हों। ऐसा करने का सबसे आसान तरीका है बस स्क्

  1. Android लॉक स्क्रीन पर नोटिफिकेशन कैसे छिपाएं Android लॉक स्क्रीन पर नोटिफिकेशन कैसे छिपाएं

    अपनी लॉक स्क्रीन पर नोटिफिकेशन देखना सुविधाजनक हो सकता है। इस तरह आप एक नज़र में देख सकते हैं कि यह कुछ महत्वपूर्ण है या नहीं और तय कर सकते हैं कि अपना फोन अनलॉक करना उचित है या नहीं। लेकिन अपनी लॉक स्क्रीन पर यह जानकारी होने का मतलब यह भी है कि आप जितना शेयर करना चाहते हैं उससे अधिक जानकारी शेयर क

  1. अपने Android के लॉक स्क्रीन को कैसे कस्टमाइज़ करें अपने Android के लॉक स्क्रीन को कैसे कस्टमाइज़ करें

    Android फोन के बारे में जो चीज उन्हें उपयोगकर्ताओं के साथ इतना लोकप्रिय बनाती है, वह है कस्टमाइज़ेशन के लिए उपलब्ध विकल्प। Android उपयोगकर्ता अपने ऐप ड्रॉअर, नोटिफिकेशन पैनल और लॉन्चर को बदल सकते हैं यदि वे चुनते हैं। एक चीज जिसके बारे में आप कस्टमाइज़ करने के बारे में नहीं सोच सकते हैं वह है डिवाइस