सूचना बनाएं

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

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

AndroidX Core लाइब्रेरी जोड़ना

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

ग्रूवी

dependencies {
    implementation "androidx.core:core-ktx:1.16.0"
}

Kotlin

dependencies {
    implementation("androidx.core:core-ktx:1.16.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) और उसके बाद के वर्शन के साथ काम करने के लिए ज़रूरी है. हालांकि, इससे पहले के वर्शन इसे अनदेखा कर देते हैं.

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

दूसरी इमेज. बड़ी की जा सकने वाली सूचना को छोटा और बड़ा करके दिखाया गया है.

अगर आपको लंबी सूचना दिखानी है, तो 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...")
        <b>.setStyle(NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))</b>
        .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...")
        <b>.setStyle(new NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))</b>
        .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 क्लास के किसी एक कॉन्स्टेंट का इस्तेमाल किया जाता है. इस पैरामीटर से यह तय होता है कि इस चैनल से जुड़ी किसी भी सूचना के लिए, उपयोगकर्ता को कैसे इंटरप्ट किया जाए. priority को setPriority() के साथ सेट करें, ताकि Android 7.1 और इससे पहले के वर्शन पर काम किया जा सके. जैसा कि ऊपर दिए गए उदाहरण में दिखाया गया है.

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

अलग-अलग लेवल का क्या मतलब है, इस बारे में ज़्यादा जानने के लिए, सूचना के अहम होने के लेवल के बारे में पढ़ें.

सूचना पर टैप करने की कार्रवाई सेट करना

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

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

Kotlin

// Create an explicit intent for an Activity in your app.
<b>val intent = Intent(this, AlertDetails::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}</b>
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.
        <b>.setContentIntent(pendingIntent)</b>
        .setAutoCancel(true)

Java

// Create an explicit intent for an Activity in your app.
<b>Intent intent = new Intent(this, AlertDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);</b>
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.
        <b>.setContentIntent(pendingIntent)</b>
        .setAutoCancel(true);

यह कोड, setAutoCancel() को कॉल करता है. इससे, जब उपयोगकर्ता सूचना पर टैप करता है, तो वह अपने-आप हट जाती है.

ऊपर दिए गए उदाहरण में, इंटेंट फ़्लैग की मदद से यह पक्का किया जाता है कि उपयोगकर्ता को नोटिफ़िकेशन का इस्तेमाल करके आपका ऐप्लिकेशन खोलने के बाद, नेविगेशन का वैसा ही अनुभव मिले जैसा उसे उम्मीद थी. शुरू की जा रही गतिविधि के टाइप के आधार पर, इसका इस्तेमाल किया जा सकता है. यह गतिविधि इनमें से कोई एक हो सकती है:

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

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

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

सूचना दिखाओ

सूचना दिखाने के लिए, 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&lt;out String&gt;,
        //                                        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"

<b>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)</b>
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        <b>.addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent)</b>

Java

String ACTION_SNOOZE = "snooze"

<b>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);</b>

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)
        <b>.addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);</b>

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

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

सीधे तौर पर जवाब देने की सुविधा जोड़ना

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

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

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

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

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

  1. RemoteInput.Builder का एक इंस्टेंस बनाएं, जिसे सूचना की कार्रवाई में जोड़ा जा सकता है. इस क्लास का कंस्ट्रक्टर, ऐसी स्ट्रिंग स्वीकार करता है जिसका इस्तेमाल सिस्टम, टेक्स्ट इनपुट के लिए कुंजी के तौर पर करता है. इसके बाद, आपका ऐप्लिकेशन उस कुंजी का इस्तेमाल करके, इनपुट किए गए टेक्स्ट को वापस पाता है. * {Kotlin} ```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} ```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} ```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} ```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 का इस्तेमाल करके, RemoteInput ऑब्जेक्ट को किसी कार्रवाई से अटैच करें.addRemoteInput() * {Kotlin} ```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} ```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} ```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} ```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);

अन्य डेटा वापस पाना

RemoteInput के साथ अन्य डेटा टाइप को मैनेज करने का तरीका भी ऐसा ही होता है. इस उदाहरण में, इमेज को इनपुट के तौर पर इस्तेमाल किया गया है.

Kotlin

  // Key for the data that's delivered in the action's intent.
  private val KEY_REPLY = "key_reply"
  var replyLabel: String = resources.getString(R.string.reply_label)
  var remoteInput: RemoteInput = RemoteInput.Builder(KEY_REPLY).run {
      setLabel(replyLabel)
      // Allow for image data types in the input
  // This method can be used again to
  // allow for other data types
      setAllowDataType("image/*", true)
      build()
}

RemoteInput#getDataResultsFromIntent को कॉल करें और उससे जुड़ा डेटा एक्सट्रैक्ट करें.

Kotlin

  import android.app.RemoteInput;
  import android.content.Intent;
  import android.os.Bundle;

  class ReplyReceiver: BroadcastReceiver()  {

      public static final String KEY_DATA = "key_data";

      public static void handleRemoteInput(Intent intent) {
          Bundle dataResults = RemoteInput.getDataResultsFromIntent(intent, KEY_DATA);
          val imageUri: Uri? = dataResults.values.firstOrNull()
          if (imageUri != null) {
              // Extract the image
          try {
                  val inputStream = context.contentResolver.openInputStream(imageUri)
                  val bitmap = BitmapFactory.decodeStream(inputStream)
                  // Display the image
                  // ...
              } catch (e: Exception) {
                  Log.e("ReplyReceiver", "Failed to process image URI", e)
              }
      }
  }

इस नई सूचना के साथ काम करते समय, उस कॉन्टेक्स्ट का इस्तेमाल करें जिसे रिसीवर के onReceive() तरीके में पास किया गया है.

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

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

बहुत ज़रूरी मैसेज दिखाना

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

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

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

नीचे दिए गए कोड स्निपेट में, सूचना को फ़ुल-स्क्रीन इंटेंट से जोड़ने का तरीका दिखाया गया है:

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)
        <b>.setFullScreenIntent(fullScreenPendingIntent, true)</b>

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)
        <b>.setFullScreenIntent(fullScreenPendingIntent, true);</b>

लॉक स्क्रीन पर दिखने वाली सूचनाओं को सेट करना

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

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

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

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

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

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

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

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

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

सूचना हटाना

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

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

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

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

MessagingStyle का इस्तेमाल करना

Android 7.0 (एपीआई लेवल 24) से, Android ने सूचना के लिए एक खास स्टाइल वाला टेंप्लेट उपलब्ध कराया है. यह टेंप्लेट, मैसेजिंग कॉन्टेंट के लिए है. 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() तरीके का इस्तेमाल करें. पैटर्न इमेज/* के एमआईएमई टाइप इस्तेमाल किए जा सकते हैं.

सीधे नोटिफ़िकेशन से ही जवाब देने की सुविधा का इस्तेमाल करना

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

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

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

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

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

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