सूचना बनाएं

सूचनाओं से, आपके ऐप्लिकेशन में होने वाले इवेंट के बारे में कम शब्दों में और सही समय पर जानकारी मिलती है वह इस्तेमाल में नहीं है. इस दस्तावेज़ में सूचना बनाने का तरीका बताया गया है सुविधाओं का इस्तेमाल करने के लिए किया जा सकता है. Android पर सूचनाएं कैसे दिखती हैं, यह जानने के लिए, सूचनाओं की खास जानकारी देखें. सूचनाओं का इस्तेमाल करने वाले सैंपल कोड के लिए, People सैंपल GitHub पर.

इस पेज पर मौजूद कोड, NotificationCompat AndroidX लाइब्रेरी से लिए गए एपीआई. ये एपीआई आपको सिर्फ़ उपलब्ध सुविधाओं को जोड़ने देते हैं साथ ही, Android के नए वर्शन पर काम करता है. 9 (एपीआई लेवल 28). हालांकि, कुछ सुविधाएं, जैसे कि इनलाइन जवाब की कार्रवाई, की वजह से पुराने वर्शन पर कोई कार्रवाई नहीं की जाती है.

AndroidX Core Library जोड़ें

हालांकि, Android Studio की मदद से बनाए गए ज़्यादातर प्रोजेक्ट में, NotificationCompat का इस्तेमाल करने के लिए डिपेंडेंसी, पुष्टि करें कि आपका मॉड्यूल-लेवल build.gradle फ़ाइल में यह डिपेंडेंसी शामिल है:

ग्रूवी

dependencies {
    implementation "androidx.core:core:2.2.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:2.2.0")
}

बुनियादी सूचना बनाना

अपने सबसे सामान्य और संक्षिप्त रूप में सूचना—जिसे छोटा किया गया भी कहा जाता है फ़ॉर्म—एक आइकॉन, शीर्षक, और कुछ टेक्स्ट कॉन्टेंट दिखाता है. यह सेक्शन में, ऐसी सूचना बनाने का तरीका बताया गया है जिस पर टैप करके उपयोगकर्ता, गतिविधि भी दिखाई देगी.

पहला डायग्राम. इसके साथ एक सूचना कोई आइकॉन, टाइटल, और कुछ टेक्स्ट.

किसी सूचना के हर हिस्से के बारे में ज़्यादा जानकारी पाने के लिए, सूचना के बारे में पढ़ें. शरीर रचना.

रनटाइम की अनुमति का एलान करना

Android 13 (एपीआई लेवल 33) और उसके बाद के वर्शन, पोस्ट करने के लिए रनटाइम की अनुमति के साथ काम करते हैं किसी ऐप्लिकेशन से मिलने वाली ऐसी सूचनाएं जिन्हें छूट नहीं मिली है (इनमें फ़ोरग्राउंड सेवाएं (एफ़जीएस) भी शामिल हैं).

आपको अपने ऐप्लिकेशन की मेनिफ़ेस्ट फ़ाइल में जिस अनुमति का एलान करना ज़रूरी है कोड स्निपेट में:

<manifest ...>
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
    <application ...>
        ...
    </application>
</manifest>

रनटाइम की अनुमतियों के बारे में ज़्यादा जानने के लिए, यहां जाएं सूचना के लिए रनटाइम की अनुमति.

सूचना का कॉन्टेंट सेट करें

शुरू करने के लिए, सूचना का कॉन्टेंट और चैनल सेट करने के लिए, NotificationCompat.Builder ऑब्जेक्ट है. नीचे दिए गए उदाहरण में, फ़ॉलो किया जा रहा है:

  • छोटा आइकॉन, जिसे इसने सेट किया है setSmallIcon(). इसकी ज़रूरत होती है कि उपयोगकर्ताओं को सिर्फ़ यही कॉन्टेंट दिखेगा.

  • टाइटल: setContentTitle().

  • मुख्य हिस्से के लिए टेक्स्ट setContentText().

  • सूचना की प्राथमिकता, जो इस आधार पर सेट की जाती है setPriority(). प्राथमिकता यह तय करती है कि Android 7.1 पर नोटिफ़िकेशन कितना दखल देने वाला हो और . Android 8.0 और उसके बाद के वर्शन के लिए, चैनल की अहमियत को इस तरह सेट करें अगले सेक्शन में दिखेगा.

Kotlin

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

Java

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationCompat.Builder कंस्ट्रक्टर के लिए, आपको कोई चैनल उपलब्ध कराना होगा आईडी. यह Android 8.0 (एपीआई लेवल 26) और Android 8.0 (एपीआई लेवल 26) के साथ काम करने के लिए ज़रूरी है बाद में, लेकिन पिछले वर्शन में इसे अनदेखा कर दिया जाता है.

डिफ़ॉल्ट रूप से, सूचना के टेक्स्ट कॉन्टेंट में काट-छांट की जाती है, ताकि उसे एक लाइन में फ़िट किया जा सके. आपने लोगों तक पहुंचाया मुफ़्त में विस्तार करने योग्य सूचना बनाकर अतिरिक्त जानकारी दिखा सकते हैं.

दूसरा डायग्राम. बड़ा किया जा सकने वाला सूचना के छोटे और बड़े रूप में.

अगर आप चाहते हैं कि आपकी सूचना ज़्यादा समय तक दिखे, तो विज्ञापन को बड़ा करके देखने की सुविधा चालू की जा सकती है सूचना के लिए स्टाइल टेंप्लेट जोड़ा जा सकता है setStyle(). उदाहरण के लिए, नीचे दिया गया कोड, टेक्स्ट एरिया का बड़ा हिस्सा बनाता है:

Kotlin

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

Java

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

सूचना के दूसरे बड़े स्टाइल के बारे में ज़्यादा जानकारी पाने के लिए, इसमें उन्हें जोड़ने का तरीका भी शामिल है कोई इमेज और मीडिया प्लेबैक कंट्रोल है, तो अपने हिसाब से विज्ञापन को बड़ा करके देखना सूचना पर टैप करें.

चैनल बनाएं और कॉन्टेंट की अहमियत सेट करें

Android 8.0 और उसके बाद के वर्शन पर सूचना डिलीवर करने से पहले, अपना सूचना का चैनल सिस्टम को NotificationChannel से createNotificationChannel(). नीचे दिया गया कोड SDK_INT वर्शन:

Kotlin

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system.
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

Java

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this.
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

क्योंकि कोई भी वीडियो पोस्ट करने से पहले आपको सूचना वाला चैनल बनाना होगा Android 8.0 और उसके बाद के वर्शन पर सूचनाएं पाने के लिए, अपने ऐप्लिकेशन में इस कोड को तुरंत लागू करें शुरू होता है. इसे बार-बार कॉल किया जा सकता है, क्योंकि सूचना चैनल कोई कार्रवाई नहीं करता.

NotificationChannel कंस्ट्रक्टर को importance की ज़रूरत होती है. इसके लिए, नीचे दिए गए विकल्पों में से किसी एक का इस्तेमाल किया जाता है से कॉन्सटेंट NotificationManager क्लास. यह पैरामीटर से यह तय होता है कि उपयोगकर्ता को किसी इस चैनल को. Android 7.1 पर काम करने के लिए setPriority() की प्राथमिकता सेट करें और उससे पहले की है, जैसा कि पिछले उदाहरण में दिखाया गया है.

हालांकि, आपको सूचना की अहमियत या प्राथमिकता सेट करनी होगी, जैसा कि यहां दिखाया गया है नीचे दिया गया उदाहरण देखें, तो सिस्टम आपको मिलने वाले अलर्ट व्यवहार की गारंटी नहीं देता. तय सीमा में कुछ मामलों में, हो सकता है कि सिस्टम अन्य फ़ैक्टर के आधार पर ज़रूरी ईमेल के लेवल में बदलाव करे, साथ ही, उपयोगकर्ता किसी खास विषय के महत्व को फिर से तय कर सकता है चैनल.

अलग-अलग लेवल का मतलब जानने के लिए, सूचना की अहमियत लेवल तय करें.

नोटिफ़िकेशन की टैप करने की कार्रवाई सेट करें

हर सूचना पर एक टैप का जवाब देना ज़रूरी है. ऐसा आम तौर पर, आपके स्टोर में किसी गतिविधि को खोलने के लिए किया जाता है ऐसा ऐप्लिकेशन चुनें जो सूचना से जुड़ा हो. ऐसा करने के लिए, कॉन्टेंट इंटेंट तय करें PendingIntent से तय किया गया ऑब्जेक्ट को भेजना और उसे setContentIntent().

नीचे दिए गए स्निपेट में किसी गतिविधि को खोलने के लिए, बेसिक इंटेंट बनाने का तरीका बताया गया है जब उपयोगकर्ता सूचना पर टैप करता है, तो:

Kotlin

// Create an explicit intent for an Activity in your app.
val intent = Intent(this, AlertDetails::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        // Set the intent that fires when the user taps the notification.
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

Java

// Create an explicit intent for an Activity in your app.
Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        // Set the intent that fires when the user taps the notification.
        .setContentIntent(pendingIntent)
        .setAutoCancel(true);

यह कोड, setAutoCancel() ऐसा करने पर, उपयोगकर्ता के उस पर टैप करने पर सूचना को अपने-आप हटा दिया जाता है.

setFlags() तरीका पिछले उदाहरण में दिखाई गई इमेज, उपयोगकर्ता के बताए गए नेविगेशन को सुरक्षित रखती है आपके ऐप्लिकेशन को खोलने के बाद होने वाला अनुभव. आप शायद ये करना चाहें इसे उस गतिविधि के प्रकार के आधार पर इस्तेमाल किया जा सकता है जिसे शुरू किया जा रहा है. यह इनमें से एक हो सकती है: निम्न:

  • ऐसी गतिविधि जो खास तौर पर सूचना के जवाबों के लिए मौजूद होती है. ऐप्लिकेशन के सामान्य इस्तेमाल के दौरान, उपयोगकर्ता इस गतिविधि पर जाने की कोई वजह नहीं है. इससे गतिविधि आपके ऐप्लिकेशन के टास्क में जोड़े जाने के बजाय, नया टास्क शुरू कर देती है मौजूदा टास्क और वापस जाएं स्टैक दबाएं. यह है पिछले सैंपल में बनाए गए इंटेंट का टाइप.

  • ऐसी गतिविधि जो आपके ऐप्लिकेशन के नियमित ऐप्लिकेशन फ़्लो में मौजूद होती है. इस मामले में, गतिविधि शुरू करने से, बैक स्टैक बन जाता है, ताकि उपयोगकर्ता को उम्मीदों पर फ़ोकस हो सके बैक और अप बटन के लिए संरक्षित किया गया.

अपनी सूचना के इंटेंट को कॉन्फ़िगर करने के अलग-अलग तरीकों के बारे में ज़्यादा जानने के लिए, यह देखें सूचना से गतिविधि शुरू करना.

सूचना दिखाएं

सूचना दिखाने के लिए, कॉल करें NotificationManagerCompat.notify() सूचना के लिए एक विशेष आईडी पास करना और NotificationCompat.Builder.build(). यह नीचे दिए गए उदाहरण में दिखाया गया है:

Kotlin

with(NotificationManagerCompat.from(this)) {
    if (ActivityCompat.checkSelfPermission(
            this@MainActivity,
            Manifest.permission.POST_NOTIFICATIONS
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        // TODO: Consider calling
        // ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        // public fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
        //                                        grantResults: IntArray)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.

        return@with
    }
    // notificationId is a unique int for each notification that you must define.
    notify(NOTIFICATION_ID, builder.build())
}

Java

with(NotificationManagerCompat.from(this)) {
   if (ActivityCompat.checkSelfPermission(
           this@MainActivity,
           Manifest.permission.POST_NOTIFICATIONS
       ) != PackageManager.PERMISSION_GRANTED
   ) {
       // TODO: Consider calling
       // ActivityCompat#requestPermissions
       // here to request the missing permissions, and then overriding
       // public void onRequestPermissionsResult(int requestCode, String[] permissions,
       //                                        int[] grantResults)
       // to handle the case where the user grants the permission. See the documentation
       // for ActivityCompat#requestPermissions for more details.

       return
   }
   // notificationId is a unique int for each notification that you must define.
   notify(NOTIFICATION_ID, builder.build())
}

NotificationManagerCompat.notify() को पास किए जाने वाले नोटिफ़िकेशन आईडी को सेव करें, क्योंकि आपको उसकी आवश्यकता तब होती है जब आप अपडेट करना चाहते हैं या हटाना चाहते हैं सूचना.

इसके अलावा, नए वर्शन में चल रहे डिवाइसों पर बुनियादी सूचनाओं की जांच करने के लिए, Android 13 और उसके बाद के वर्शन के लिए, सूचनाएं पाने की सुविधा को मैन्युअल तरीके से चालू करें या इसके लिए डायलॉग बॉक्स बनाएं अनुरोध की सूचनाएँ.

ऐक्शन बटन जोड़ना

किसी सूचना में ज़्यादा से ज़्यादा तीन ऐक्शन बटन हो सकते हैं, जिनकी मदद से उपयोगकर्ता जवाब दे सकता है जैसे, रिमाइंडर स्नूज़ करने या किसी मैसेज का जवाब देने के लिए. हालांकि, ये ऐक्शन बटन को उस कार्रवाई का डुप्लीकेट नहीं बनाना चाहिए जो उपयोगकर्ता के टैप करने पर सूचना पर टैप करें.

तीसरी इमेज. इसके साथ एक सूचना सिर्फ़ एक ऐक्शन बटन का इस्तेमाल करें.

ऐक्शन बटन जोड़ने के लिए, PendingIntent को addAction() तरीका. यह कार्रवाई को छोड़कर, सूचना के लिए डिफ़ॉल्ट रूप से टैप करने की कार्रवाई सेट करने जैसा है कोई गतिविधि शुरू करने के बजाय, अन्य काम किए जा सकते हैं. जैसे BroadcastReceiver कि बैकग्राउंड में काम करता है, ताकि ऐप्लिकेशन के काम में कोई रुकावट न आए कि वह पहले से खुला है.

उदाहरण के लिए, नीचे दिया गया कोड दिखाता है कि किसी खास नेटवर्क पर ब्रॉडकास्ट कैसे भेजा जाता है पाने वाला:

Kotlin

val ACTION_SNOOZE = "snooze"

val snoozeIntent = Intent(this, MyBroadcastReceiver::class.java).apply {
    action = ACTION_SNOOZE
    putExtra(EXTRA_NOTIFICATION_ID, 0)
}
val snoozePendingIntent: PendingIntent =
    PendingIntent.getBroadcast(this, 0, snoozeIntent, 0)</span>
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent)

Java

String ACTION_SNOOZE = "snooze"

Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);</span>

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);

बैकग्राउंड में चलाने के लिए, BroadcastReceiver बनाने के बारे में ज़्यादा जानकारी के लिए तो ब्रॉडकास्ट की खास जानकारी देखें.

इसके बजाय, अगर आपको मीडिया प्लेबैक बटन का इस्तेमाल करके सूचना बनानी है, तो जैसे कि ट्रैक को रोकना या स्किप करना, मीडिया के साथ सूचना बनाने का तरीका नियंत्रण.

सीधे जवाब देने की कार्रवाई जोड़ें

Android 7.0 (एपीआई लेवल 24) में शुरू की गई, सीधे जवाब देने की कार्रवाई की मदद से, उपयोगकर्ता आसानी से सीधे सूचना में टेक्स्ट डालें. इसके बाद, टेक्स्ट आपके ऐप का इस्तेमाल कर सकते हैं. उदाहरण के लिए, डायरेक्ट जवाब देने की सुविधा का इस्तेमाल किया जा सकता है उपयोगकर्ताओं को टेक्स्ट मैसेज का जवाब देने या टास्क की सूचियां अपडेट करने के लिए सूचना पर टैप करें.

चौथी इमेज. "जवाब दें" पर टैप करें बटन टेक्स्ट इनपुट खोलता है.

सीधे जवाब देने की कार्रवाई, सूचना में एक और बटन के तौर पर दिखती है टेक्स्ट इनपुट खोलता है. जब उपयोगकर्ता टाइप कर लेता है, तब सिस्टम टेक्स्ट को अटैच कर देता है सूचना के लिए आपके तय किए गए इंटेंट पर जवाब देता है और इंटेंट भी करें.

'जवाब दें' बटन जोड़ें

सीधे जवाब देने की सुविधा वाली सूचना कार्रवाई बनाने के लिए, यह तरीका अपनाएं:

  1. का एक इंस्टेंस बनाएं RemoteInput.Builder अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है जिसे सूचना से जुड़ी कार्रवाई में जोड़ा जा सकता है. इस क्लास का कंस्ट्रक्टर स्वीकार करता है एक स्ट्रिंग जिसे सिस्टम टेक्स्ट इनपुट के लिए कुंजी के तौर पर इस्तेमाल करता है. आपका ऐप्लिकेशन बाद में इनपुट के टेक्स्ट को फिर से पाने के लिए उस कुंजी का इस्तेमाल करता है.

    Kotlin

      // Key for the string that's delivered in the action's intent.
      private val KEY_TEXT_REPLY = "key_text_reply"
      var replyLabel: String = resources.getString(R.string.reply_label)
      var remoteInput: RemoteInput = RemoteInput.Builder(KEY_TEXT_REPLY).run {
          setLabel(replyLabel)
          build()
      }
      

    Java

      // Key for the string that's delivered in the action's intent.
      private static final String KEY_TEXT_REPLY = "key_text_reply";
    
      String replyLabel = getResources().getString(R.string.reply_label);
      RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
              .setLabel(replyLabel)
              .build();
      
  2. जवाब देने से जुड़ी कार्रवाई के लिए, PendingIntent बनाएं.

    Kotlin

      // Build a PendingIntent for the reply action to trigger.
      var replyPendingIntent: PendingIntent =
          PendingIntent.getBroadcast(applicationContext,
              conversation.getConversationId(),
              getMessageReplyIntent(conversation.getConversationId()),
              PendingIntent.FLAG_UPDATE_CURRENT)
      

    Java

      // Build a PendingIntent for the reply action to trigger.
      PendingIntent replyPendingIntent =
              PendingIntent.getBroadcast(getApplicationContext(),
                      conversation.getConversationId(),
                      getMessageReplyIntent(conversation.getConversationId()),
                      PendingIntent.FLAG_UPDATE_CURRENT);
      
  3. अपनी फ़ाइल को अटैच करें RemoteInput अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है इसका इस्तेमाल करके किसी कार्रवाई पर आपत्ति जताएं addRemoteInput().

    Kotlin

      // Create the reply action and add the remote input.
      var action: NotificationCompat.Action =
          NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,
              getString(R.string.label), replyPendingIntent)
              .addRemoteInput(remoteInput)
              .build()
      

    Java

      // Create the reply action and add the remote input.
      NotificationCompat.Action action =
              new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon,
                      getString(R.string.label), replyPendingIntent)
                      .addRemoteInput(remoteInput)
                      .build();
      
  4. सूचना पर कार्रवाई लागू करें और सूचना जारी करें.

    Kotlin

      // Build the notification and add the action.
      val newMessageNotification = Notification.Builder(context, CHANNEL_ID)
              .setSmallIcon(R.drawable.ic_message)
              .setContentTitle(getString(R.string.title))
              .setContentText(getString(R.string.content))
              .addAction(action)
              .build()
    
      // Issue the notification.
      with(NotificationManagerCompat.from(this)) {
          notificationManager.notify(notificationId, newMessageNotification)
      }
      

    Java

      // Build the notification and add the action.
      Notification newMessageNotification = new Notification.Builder(context, CHANNEL_ID)
              .setSmallIcon(R.drawable.ic_message)
              .setContentTitle(getString(R.string.title))
              .setContentText(getString(R.string.content))
              .addAction(action)
              .build();
    
      // Issue the notification.
      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
      notificationManager.notify(notificationId, newMessageNotification);
      

जब उपयोगकर्ता सूचना कार्रवाई, जैसा कि चौथी इमेज में दिखाया गया है.

जवाब से उपयोगकर्ता का इनपुट फिर से पाएं

सूचना के जवाब देने वाले यूज़र इंटरफ़ेस (यूआई) से उपयोगकर्ता का इनपुट पाने के लिए, कॉल करें RemoteInput.getResultsFromIntent() आपके BroadcastReceiver को मिले Intent के बराबर:

Kotlin

private fun getMessageText(intent: Intent): CharSequence? {
    return RemoteInput.getResultsFromIntent(intent)?.getCharSequence(KEY_TEXT_REPLY)
}

Java

private CharSequence getMessageText(Intent intent) {
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput != null) {
        return remoteInput.getCharSequence(KEY_TEXT_REPLY);
    }
    return null;
 }

मैसेज प्रोसेस करने के बाद, कॉल करके सूचना को अपडेट करें अगर इस्तेमाल किया गया है, तो समान आईडी और टैग वाले NotificationManagerCompat.notify(). यह है सीधे जवाब देने वाले यूज़र इंटरफ़ेस (यूआई) को छिपाने के लिए ज़रूरी है. साथ ही, उपयोगकर्ता से यह पुष्टि की जाती है कि उनका जवाब मिला और सही तरीके से प्रोसेस किया गया.

Kotlin

// Build a new notification, which informs the user that the system
// handled their interaction with the previous notification.
val repliedNotification = Notification.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_message)
        .setContentText(getString(R.string.replied))
        .build()

// Issue the new notification.
NotificationManagerCompat.from(this).apply {
    notificationManager.notify(notificationId, repliedNotification)
}

Java

// Build a new notification, which informs the user that the system
// handled their interaction with the previous notification.
Notification repliedNotification = new Notification.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_message)
        .setContentText(getString(R.string.replied))
        .build();

// Issue the new notification.
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, repliedNotification);

इस नई सूचना का इस्तेमाल करते समय, पाने वाले का onReceive() तरीका.

कॉल करके, सूचना में सबसे नीचे जवाब जोड़ें setRemoteInputHistory(). हालांकि, अगर आप कोई मैसेज सेवा ऐप्लिकेशन बना रहे हैं, तो मैसेज सेवा की शैली बनाएं सूचना पर क्लिक करें और बातचीत में नया मैसेज जोड़ रहे हैं.

मैसेजिंग ऐप्लिकेशन की सूचनाओं के बारे में ज़्यादा सलाह पाने के लिए, मैसेजिंग ऐप्लिकेशन के लिए सबसे सही तरीके.

प्रोग्रेस बार जोड़ें

सूचनाओं में ऐनिमेशन का प्रोग्रेस दिखाने वाला इंडिकेटर शामिल हो सकता है. इससे लोगों को यह पता चलता है कि किसी कार्रवाई की मौजूदा स्थिति.

पांचवी इमेज. के दौरान प्रोग्रेस बार एक कार्रवाई.

अगर किसी भी समय यह अनुमान लगाया जा सकता है कि कितनी कार्रवाई पूरी हो चुकी है, तो "तय करना" इंडिकेटर का रूप—जैसा कि पांचवीं इमेज में दिखाया गया है—कॉल करके setProgress(max, progress, false). पहला पैरामीटर "complete" होता है मान है, जैसे 100. दूसरा है कितना समय पूरा हुआ. आखिरी हिस्सा से पता चलता है कि यह एक तय कदम है बार.

आगे बढ़ने के साथ-साथ, progress के लिए अपडेट की गई वैल्यू के साथ setProgress(max, progress, false) को लगातार कॉल करें और सूचना फिर से जारी करें, जैसे कि नीचे दिए गए उदाहरण में दिखाया गया है.

Kotlin

val builder = NotificationCompat.Builder(this, CHANNEL_ID).apply {
    setContentTitle("Picture Download")
    setContentText("Download in progress")
    setSmallIcon(R.drawable.ic_notification)
    setPriority(NotificationCompat.PRIORITY_LOW)
}
val PROGRESS_MAX = 100
val PROGRESS_CURRENT = 0
NotificationManagerCompat.from(this).apply {
    // Issue the initial notification with zero progress.
    builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false)
    notify(notificationId, builder.build())

    // Do the job that tracks the progress here.
    // Usually, this is in a worker thread.
    // To show progress, update PROGRESS_CURRENT and update the notification with:
    // builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
    // notificationManager.notify(notificationId, builder.build());

    // When done, update the notification once more to remove the progress bar.
    builder.setContentText("Download complete")
            .setProgress(0, 0, false)
    notify(notificationId, builder.build())
}

Java

...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentTitle("Picture Download")
        .setContentText("Download in progress")
        .setSmallIcon(R.drawable.ic_notification)
        .setPriority(NotificationCompat.PRIORITY_LOW);

// Issue the initial notification with zero progress.
int PROGRESS_MAX = 100;
int PROGRESS_CURRENT = 0;
builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
notificationManager.notify(notificationId, builder.build());

// Do the job that tracks the progress here.
// Usually, this is in a worker thread.
// To show progress, update PROGRESS_CURRENT and update the notification with:
// builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
// notificationManager.notify(notificationId, builder.build());

// When done, update the notification once more to remove the progress bar.
builder.setContentText("Download complete")
        .setProgress(0,0,false);
notificationManager.notify(notificationId, builder.build());

कार्रवाई के आखिर में, progress का मान max के बराबर होना चाहिए. आप इसे छोड़ सकते हैं प्रोग्रेस बार पर टैप करें. दोनों ही मामलों में, सूचना टेक्स्ट को अपडेट करके दिखाएगा कि कार्रवाई पूरी हो गई है. हटाने के लिए आप प्रोग्रेस बार में हैं, तो setProgress(0, 0, false) पर कॉल करें.

जो प्रोग्रेस बार तय नहीं किया गया है उसे दिखाने के लिए. यह ऐसा बार होता है जो पूरा होने के बारे में नहीं बताता प्रतिशत), setProgress(0, 0, true) पर कॉल करें. नतीजे से पता चलता है कि स्टाइल पिछले प्रोग्रेस बार वाली स्टाइल से मेल खाती है. हालांकि, इसका मतलब है कि पूरा होने का संकेत नहीं देने वाला ऐनिमेशन. प्रोग्रेस ऐनिमेशन इस समय तक चलता रहेगा आपको setProgress(0, 0, false) को कॉल करना होता है. इसके बाद, सूचना को हटाने के लिए सूचना को अपडेट करना होता है गतिविधि दिखाने वाला इंडिकेटर.

यह बताने के लिए सूचना टेक्स्ट को बदलना न भूलें कि कार्रवाई पूरा हुआ.

पूरे सिस्टम के लिए कैटगरी सेट करें

परेशान करना है या नहीं, यह तय करने के लिए Android पूरे सिस्टम पर पहले से तय की गई कैटगरी का इस्तेमाल करता है जब उपयोगकर्ता परेशान न करें सुविधा को चालू करता है, तो उपयोगकर्ता को सूचना दी जाती है मोड है.

अगर आपकी सूचना, NotificationCompat—जैसे CATEGORY_ALARM, CATEGORY_REMINDER, CATEGORY_EVENT, या CATEGORY_CALL—जानकारी देना को उचित श्रेणी पास करके setCategory():

Kotlin

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setCategory(NotificationCompat.CATEGORY_MESSAGE)

Java

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setCategory(NotificationCompat.CATEGORY_MESSAGE);

सिस्टम आपकी सूचना की कैटगरी से जुड़ी इस जानकारी का इस्तेमाल, डिवाइस के 'न करें' मोड में होने पर आपकी सूचना दिखाने के बारे में फ़ैसले परेशान करो. हालांकि, आपको सिस्टम-वाइड कैटगरी सेट करने की ज़रूरत नहीं है. सिर्फ़ ऐसा करें अगर आपकी सूचनाएं NotificationCompat.

कोई ज़रूरी मैसेज दिखाएं

आपके ऐप्लिकेशन में एक ज़रूरी, समय के हिसाब से संवेदनशील मैसेज दिखाने की ज़रूरत पड़ सकती है. उदाहरण के लिए, फ़ोन कॉल आने या अलार्म बजने की आवाज़. इन स्थितियों में, आपके पास आपके नोटिफ़िकेशन के साथ फ़ुल-स्क्रीन पर सूचनाएं दिखाते हैं.

जब किसी सूचना को शुरू किया जाता है, तो उपयोगकर्ताओं को इनमें से कोई एक सूचना दिखती है. हालांकि, यह कार्रवाई इन पर निर्भर करती है डिवाइस का लॉक स्थिति:

  • अगर उपयोगकर्ता का डिवाइस लॉक है, तो फ़ुल-स्क्रीन पर एक गतिविधि दिखेगी. इसमें, लॉकस्क्रीन.
  • अगर उपयोगकर्ता का डिवाइस अनलॉक है, तो सूचना बड़े साइज़ में दिखेगी फ़ॉर्म में, सूचना को मैनेज या खारिज करने के विकल्प दिए गए हैं.

नीचे दिया गया कोड स्निपेट आपकी सूचना को फ़ुल-स्क्रीन पर सूचनाएं दिखाने के लिए:

Kotlin

val fullScreenIntent = Intent(this, ImportantActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setFullScreenIntent(fullScreenPendingIntent, true)

Java

Intent fullScreenIntent = new Intent(this, ImportantActivity.class);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
        fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setFullScreenIntent(fullScreenPendingIntent, true);

लॉक स्क्रीन किसको दिखे, यह सेट करें

लॉक स्क्रीन से सूचना में दिखने वाली जानकारी के लेवल को कंट्रोल करने के लिए, कॉल करें setVisibility() और इनमें से कोई एक वैल्यू बताएं:

  • VISIBILITY_PUBLIC: सूचना का पूरा कॉन्टेंट लॉक स्क्रीन पर दिखता है.

  • VISIBILITY_SECRET: लॉक स्क्रीन पर सूचना का कोई हिस्सा नहीं दिखता.

  • VISIBILITY_PRIVATE: सिर्फ़ बुनियादी जानकारी, जैसे कि सूचना का आइकॉन और कॉन्टेंट टाइटल, लॉक स्क्रीन पर दिखता है. सूचना का पूरा कॉन्टेंट काम नहीं करता दिखाएं.

VISIBILITY_PRIVATE को सेट करने पर, आपके पास इसका दूसरा वर्शन भी उपलब्ध कराने का विकल्प होता है कुछ जानकारी छिपाने वाली सूचना. उदाहरण के लिए, मैसेज (एसएमएस) ऐप्लिकेशन "आपको तीन नए मैसेज मिले हैं" वाली सूचना दिख सकती है. लेकिन यह मैसेज के कॉन्टेंट और उसे भेजने वाले लोगों को छिपा देता है. यह विकल्प देने के लिए सूचना, पहले एक अन्य विकल्प के रूप में हमेशा की तरह NotificationCompat.Builder. इसके बाद, सूचना देने का दूसरा विकल्प अटैच करें के साथ सामान्य सूचना पर setPublicVersion().

ध्यान रखें कि उपयोगकर्ता के पास हमेशा इस बात का पूरा कंट्रोल रहता है कि वह सूचनाएं लॉक स्क्रीन पर देखी जा सकती हैं. साथ ही, इन्हें आपके हिसाब से कंट्रोल किया जा सकता है सूचना के चैनल पर टैप करें.

सूचना अपडेट करना

सूचना जारी करने के बाद उसे अपडेट करने के लिए, कॉल करें NotificationManagerCompat.notify() फिर से इस्तेमाल करके, उसे वही आईडी दें जिससे आपने इस्तेमाल किया था से पहले. अगर पिछली सूचना खारिज कर दी जाती है, तो एक नई सूचना बन जाती है आज़माएं.

वैकल्पिक रूप से कॉल किया जा सकता है setOnlyAlertOnce() ताकि उपयोगकर्ता आवाज़, वाइब्रेशन या विज़ुअल की मदद से सूचना पाने की प्रक्रिया में रुकावट डाल सकता है सुराग—सिर्फ़ पहली बार सूचना दिखने पर और बाद में नहीं अपडेट.

सूचना हटाना

सूचनाएं तब तक दिखती हैं, जब तक इनमें से कोई एक स्थिति न हो:

  • उपयोगकर्ता सूचना को खारिज करता है.
  • अगर आपने setAutoCancel() को कॉल करते समय सूचना पर टैप किया सूचना बनाएं.
  • आपने कॉल किया cancel() किसी खास सूचना आईडी के लिए. इस तरीके से, मौजूदा डेटा भी मिट जाता है नोटिफ़िकेशन.
  • आपने कॉल किया cancelAll() इससे, पहले जारी की गई सभी सूचनाएं हट जाएंगी.
  • अगर आपने टाइम आउट सेट किया है, तो तय समय खत्म हो जाता है सूचना का इस्तेमाल करके setTimeoutAfter(). अगर ज़रूरी हो, तो तय किए गए टाइम आउट से पहले सूचना को रद्द किया जा सकता है समय बीत जाता है.

मैसेजिंग ऐप्लिकेशन इस्तेमाल करने के सबसे सही तरीके

अपने मैसेजिंग और चैट ऐप्लिकेशन.

मैसेज सेवा का इस्तेमाल करें

Android 7.0 (एपीआई लेवल 24) के बाद वाले वर्शन में, सूचना पाने की सुविधा मिलती है टेंप्लेट बनाया जा सकता है. इसका उपयोग करके NotificationCompat.MessagingStyle क्लास के बाद, सूचना पर दिखाए गए कई लेबल में बदलाव किया जा सकता है, जिसमें बातचीत का टाइटल, अतिरिक्त मैसेज, और सूचना पर टैप करें.

नीचे दिया गया कोड स्निपेट, सूचना की शैली को पसंद के मुताबिक बनाने का तरीका बताता है MessagingStyle क्लास का इस्तेमाल करके.

Kotlin

val user = Person.Builder()
    .setIcon(userIcon)
    .setName(userName)
    .build()

val notification = NotificationCompat.Builder(this, CHANNEL_ID)
    .setContentTitle("2 new messages with $sender")
    .setContentText(subject)
    .setSmallIcon(R.drawable.new_message)
    .setStyle(NotificationCompat.MessagingStyle(user)
        .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
        .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
    )
    .build()

Java

Person user = new Person.Builder()
    .setIcon(userIcon)
    .setName(userName)
    .build();

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
    .setContentTitle("2 new messages with " + sender)
    .setContentText(subject)
    .setSmallIcon(R.drawable.new_message)
    .setStyle(new NotificationCompat.MessagingStyle(user)
        .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getPerson())
        .addMessage(messages[2].getText(), messages[2].getTime(), messages[2].getPerson())
    )
    .build();

Android 9.0 (एपीआई लेवल 28) से शुरू होने वाले वर्शन के लिए, यह ज़रूरी है कि Person क्लास पाने के लिए सूचना और उसके अवतार की बेहतर रेंडरिंग.

NotificationCompat.MessagingStyle का इस्तेमाल करते समय, ये काम करें:

  • कॉल करें MessagingStyle.setConversationTitle() का उपयोग करें. बहुत अच्छा बातचीत का शीर्षक, ग्रुप चैट का नाम हो सकता है. अगर ऐसा नहीं है, तो हो सकता है कि उसका नाम एक नाम होगा, बातचीत में हिस्सा लेने वाले लोगों की सूची होगी. इसके बिना, ऐसा हो सकता है कि कोई मैसेज, बातचीत के दौरान आपसे जुड़ी हो बातचीत में सबसे हाल के मैसेज को भेजने वाला व्यक्ति.
  • इसका इस्तेमाल करें MessagingStyle.setData() तरीका है. पैटर्न के MIME टाइप इमेज/* इस्तेमाल किए जा सकते हैं.

सीधे तौर पर जवाब देने की सुविधा का इस्तेमाल करना

सीधे तौर पर जवाब देने की सुविधा से, उपयोगकर्ता किसी मैसेज का इनलाइन जवाब दे सकता है.

  • जब कोई उपयोगकर्ता इनलाइन जवाब की कार्रवाई के साथ जवाब देता है, तो इसका इस्तेमाल करें MessagingStyle.addMessage() सूचना MessagingStyle को अपडेट करें और सूचना पर टैप करें. सूचना को रद्द नहीं करने पर, उपयोगकर्ता कई ईमेल पते पर पैसे भेज सकता है नोटिफ़िकेशन से जवाब मिलते हैं.
  • इनलाइन जवाब की कार्रवाई को Wear OS के साथ काम करने लायक बनाने के लिए, कॉल करें Action.WearableExtender.setHintDisplayInlineAction(true).
  • इसका इस्तेमाल करें addHistoricMessage() बातचीत के इतिहास को जोड़कर, सीधे जवाब में बातचीत का संदर्भ देने का तरीका नोटिफ़िकेशन पर संदेश.

स्मार्ट जवाब की सुविधा चालू करें

  • स्मार्ट जवाब की सुविधा चालू करने के लिए, कॉल करें setAllowGeneratedResponses(true) को ध्यान में रखें. इससे स्मार्ट जवाब की सुविधा इन डिवाइसों पर मिलती है उपयोगकर्ताओं को तब सूचना मिलती है, जब सूचना किसी Wear OS डिवाइस पर भेजी जाती है. स्मार्ट जवाब ये सभी प्रतिक्रियाएं, स्मार्टवॉच पर मौजूद मशीन लर्निंग मॉडल की मदद से जनरेट की जाती हैं. NotificationCompat.MessagingStyle की ओर से दिया गया संदर्भ सूचना मिलती है और नई जानकारी पाने के लिए इंटरनेट पर कोई डेटा अपलोड नहीं किया जाता जवाब.

सूचना का मेटाडेटा जोड़ना

  • सिस्टम को यह बताने के लिए सूचना मेटाडेटा असाइन करें कि आपके ऐप्लिकेशन को कैसे मैनेज करना है डिवाइस Do Not Disturb mode में होने पर सूचनाएं. उदाहरण के लिए, का इस्तेमाल करें addPerson() या setCategory(Notification.CATEGORY_MESSAGE) 'परेशान न करें' को ओवरराइड करने का तरीका.