एक से ज़्यादा डिवाइसों पर मैसेज भेजें

किसी मैसेज को कई डिवाइसों पर टारगेट करने के लिए, विषय के मैसेज का इस्तेमाल करें. इस सुविधा की मदद से, किसी खास विषय के लिए ऑप्ट-इन किए गए अलग-अलग डिवाइसों पर मैसेज भेजा जा सकता है.

इस ट्यूटोरियल में FCM के लिए एडमिन SDK या REST API का इस्तेमाल करके आपके ऐप्लिकेशन सर्वर से विषय से जुड़े मैसेज भेजने पर फ़ोकस किया गया है. साथ ही, उन्हें Android ऐप्लिकेशन में पाने और मैनेज करने पर फ़ोकस किया गया है. हम बैकग्राउंड और फ़ोरग्राउंड, दोनों तरह के ऐप्लिकेशन से मैसेज मैनेज करने के बारे में बताएंगे. सेटअप करने से लेकर पुष्टि करने तक, इसे हासिल करने के सभी चरणों के बारे में बताया गया है.

SDK टूल सेट अप करें

अगर आपने FCM के लिए Android क्लाइंट ऐप्लिकेशन सेट अप किया है या अपना पहला मैसेज भेजने के चरणों को पूरा कर लिया है, तो इस सेक्शन में वे चरण शामिल हो सकते हैं जिन्हें आपने पहले ही पूरा कर लिया है.

वेब कंटेनर इंस्टॉल करने से पहले

  • Android Studio को इंस्टॉल या अपडेट करें.

  • पक्का करें कि आपका प्रोजेक्ट इन ज़रूरी शर्तों को पूरा करता हो (ध्यान दें कि कुछ प्रॉडक्ट की शर्तें ज़्यादा सख्त हो सकती हैं):

    • टारगेट एपीआई लेवल 19 (KitKat) या उससे ज़्यादा लेवल को टारगेट करता है
    • जो Android 4.4 या इसके बाद वाले वर्शन का इस्तेमाल करते हों
    • Jetpack (AndroidX) का इस्तेमाल करता है. इसमें वर्शन की इन ज़रूरी शर्तों को पूरा करना शामिल है:
      • com.android.tools.build:gradle v7.3.0 या इसके बाद का वर्शन
      • compileSdkVersion 28 या उसके बाद
  • अपने ऐप्लिकेशन को चलाने के लिए, कोई फ़िज़िकल डिवाइस सेट अप करें या एम्युलेटर का इस्तेमाल करें.
    ध्यान दें कि Google Play services पर निर्भर Firebase SDK टूल के लिए, डिवाइस या एम्युलेटर पर Google Play services इंस्टॉल होना ज़रूरी है.

  • अपने Google खाते का इस्तेमाल करके Firebase में साइन इन करें.

अगर आपके पास पहले से कोई Android प्रोजेक्ट नहीं है और आपको सिर्फ़ Firebase प्रॉडक्ट आज़माना है, तो हमारे क्विकस्टार्ट सैंपल में से कोई एक डाउनलोड किया जा सकता है.

Firebase प्रोजेक्ट बनाना

अपने Android ऐप्लिकेशन में Firebase जोड़ने से पहले, आपको अपने Android ऐप्लिकेशन से कनेक्ट करने के लिए एक Firebase प्रोजेक्ट बनाना होगा. Firebase प्रोजेक्ट के बारे में ज़्यादा जानने के लिए, Firebase प्रोजेक्ट के बारे में जानकारी पेज पर जाएं.

अपने ऐप्लिकेशन को Firebase के साथ रजिस्टर करना

अपने Android ऐप्लिकेशन में Firebase का इस्तेमाल करने के लिए, आपको अपने ऐप्लिकेशन को Firebase प्रोजेक्ट के साथ रजिस्टर करना होगा. ऐप्लिकेशन को रजिस्टर करने को अक्सर आपके प्रोजेक्ट में "ऐप्लिकेशन जोड़ना" कहा जाता है.

  1. Firebase कंसोल पर जाएं.

  2. सेटअप वर्कफ़्लो को लॉन्च करने के लिए, प्रोजेक्ट की खास जानकारी देने वाले पेज के बीच में मौजूद, Android आइकॉन () या ऐप्लिकेशन जोड़ें पर क्लिक करें.

  3. Android पैकेज का नाम फ़ील्ड में, अपने ऐप्लिकेशन के पैकेज का नाम डालें.

  4. (ज़रूरी नहीं) ऐप्लिकेशन की अन्य जानकारी डालें: ऐप्लिकेशन का कोई दूसरा नाम और SHA-1 के हस्ताक्षर वाले सर्टिफ़िकेट को डीबग करें.

  5. ऐप्लिकेशन रजिस्टर करें पर क्लिक करें.

Firebase कॉन्फ़िगरेशन फ़ाइल जोड़ना

  1. डाउनलोड करें और फिर अपने ऐप्लिकेशन में Firebase Android कॉन्फ़िगरेशन फ़ाइल (google-services.json) जोड़ें:

    1. अपनी Firebase Android कॉन्फ़िगरेशन फ़ाइल पाने के लिए, google-services.json डाउनलोड करें पर क्लिक करें.

    2. अपनी कॉन्फ़िगरेशन फ़ाइल को ऐप्लिकेशन के मॉड्यूल (ऐप्लिकेशन-लेवल) रूट डायरेक्ट्री में ले जाएं.

  2. अपनी google-services.json कॉन्फ़िगरेशन फ़ाइल की वैल्यू को Firebase SDK टूल से ऐक्सेस करने के लिए, आपको Google की सेवाओं के Gradle प्लग इन (google-services) की ज़रूरत होगी.

    1. अपनी रूट-लेवल (प्रोजेक्ट-लेवल) की Gradle फ़ाइल (<project>/build.gradle.kts या <project>/build.gradle) में, डिपेंडेंसी के तौर पर Google सेवाएं प्लगिन जोड़ें:

      Kotlin

      plugins {
        id("com.android.application") version "7.3.0" apply false
        // ...
      
        // Add the dependency for the Google services Gradle plugin
        id("com.google.gms.google-services") version "4.4.2" apply false
      }
      

      Groovy

      plugins {
        id 'com.android.application' version '7.3.0' apply false
        // ...
      
        // Add the dependency for the Google services Gradle plugin
        id 'com.google.gms.google-services' version '4.4.2' apply false
      }
      
    2. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) Gradle फ़ाइल (आम तौर पर <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle) में, Google की सेवाएं प्लगिन जोड़ें:

      Kotlin

      plugins {
        id("com.android.application")
      
        // Add the Google services Gradle plugin
        id("com.google.gms.google-services")
        // ...
      }
      

      Groovy

      plugins {
        id 'com.android.application'
      
        // Add the Google services Gradle plugin
        id 'com.google.gms.google-services'
        // ...
      }
      

अपने ऐप्लिकेशन में Firebase SDK टूल जोड़ना

  1. अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर, <project>/<app-module>/build.gradle.kts या <project>/<app-module>/build.gradle) में, Android के लिए Firebase क्लाउड से मैसेज लाइब्रेरी के लिए डिपेंडेंसी जोड़ें. लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, हम Firebase Android BoM का इस्तेमाल करने का सुझाव देते हैं.

    Firebase क्लाउड से मैसेज के साथ बेहतर अनुभव पाने के लिए, हमारा सुझाव है कि आप अपने Firebase प्रोजेक्ट में Google Analytics चालू करें. साथ ही, अपने ऐप्लिकेशन में Google Analytics के लिए Firebase SDK टूल जोड़ें.

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:33.1.1"))
    
        // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-messaging")
        implementation("com.google.firebase:firebase-analytics")
    }
    

    Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase की Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.

    (वैकल्पिक) BoM का इस्तेमाल किए बिना Firebase लाइब्रेरी डिपेंडेंसी जोड़ें

    अगर आप Firebase BoM का इस्तेमाल नहीं करना चाहते हैं, तो आपको उसकी डिपेंडेंसी लाइन में Firebase लाइब्रेरी के हर वर्शन की जानकारी देनी होगी.

    ध्यान दें कि अगर आप अपने ऐप्लिकेशन में कई Firebase लाइब्रेरी का इस्तेमाल करते हैं, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए, BoM का इस्तेमाल करें. इससे यह पक्का होता है कि ऐप्लिकेशन के सभी वर्शन काम करते हैं.

    dependencies {
        // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation("com.google.firebase:firebase-messaging:24.0.0")
        implementation("com.google.firebase:firebase-analytics:22.0.2")
    }
    
    क्या आपको Kotlin से जुड़े लाइब्रेरी मॉड्यूल की तलाश है? अक्टूबर 2023 (Firebase BoM 32.5.0) से, Kotlin और Java डेवलपर, दोनों मुख्य लाइब्रेरी मॉड्यूल पर निर्भर कर सकते हैं. ज़्यादा जानकारी के लिए, इस इनिशिएटिव के बारे में अक्सर पूछे जाने वाले सवाल देखें.

  2. अपने Android प्रोजेक्ट को Gradle फ़ाइलों के साथ सिंक करें.

किसी विषय के लिए, क्लाइंट ऐप्लिकेशन की सदस्यता लेना

क्लाइंट ऐप्लिकेशन किसी भी मौजूदा विषय की सदस्यता ले सकते हैं या वे एक नया विषय बना सकते हैं. जब कोई क्लाइंट ऐप्लिकेशन किसी नए विषय नाम (जो आपके Firebase प्रोजेक्ट के लिए पहले से मौजूद नहीं है) की सदस्यता लेता है, तो FCM में उस नाम का एक नया विषय बनाया जाता है और कोई भी क्लाइंट बाद में इसकी सदस्यता ले सकता है.

किसी विषय की सदस्यता लेने के लिए, क्लाइंट ऐप्लिकेशन FCM विषय के नाम के साथ Firebase क्लाउड से मैसेज subscribeToTopic() को कॉल करता है. इस तरीके से Task मिलता है, जिसका इस्तेमाल करके वह व्यक्ति यह पता लगा सकता है कि सदस्यता पूरी हुई है या नहीं:

Kotlin+KTX

Firebase.messaging.subscribeToTopic("weather")
    .addOnCompleteListener { task ->
        var msg = "Subscribed"
        if (!task.isSuccessful) {
            msg = "Subscribe failed"
        }
        Log.d(TAG, msg)
        Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
    }

Java

FirebaseMessaging.getInstance().subscribeToTopic("weather")
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                String msg = "Subscribed";
                if (!task.isSuccessful()) {
                    msg = "Subscribe failed";
                }
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

सदस्यता छोड़ने के लिए, क्लाइंट ऐप्लिकेशन, विषय के नाम के साथ Firebase क्लाउड से मैसेज unsubscribeFromTopic() को कॉल करता है.

विषय के मैसेज पाएं और मैनेज करें

FCM विषय से जुड़े मैसेज उसी तरह डिलीवर करता है जैसे दूसरे डाउनस्ट्रीम मैसेज डिलीवर करता है.

मैसेज पाने के लिए, FirebaseMessagingService तक इस्तेमाल करने वाली सेवा का इस्तेमाल करें. आपकी सेवा, onMessageReceived और onDeletedMessages कॉलबैक को बदल देगी.

किसी मैसेज को हैंडल करने में 20 सेकंड से कम समय लग सकता है. यह इस बात पर निर्भर करता है कि onMessageReceived को कॉल करने में कितनी देरी हुई है. इनमें ओएस की देरी, ऐप्लिकेशन के शुरू होने में लगने वाला समय, दूसरी कार्रवाइयों की वजह से मुख्य थ्रेड का ब्लॉक होना या onMessageReceived पर किए गए पिछले कॉल की वजह से ज़्यादा समय लग सकता है. इसके बाद, ओएस से जुड़ी अलग-अलग तरह की गतिविधियां, जैसे कि Android की प्रोसेस किलिंग या Android O की बैकग्राउंड में एक्ज़ीक्यूशन करने की सीमाएं हो सकती हैं. इस वजह से हो सकता है कि आप अपना काम पूरा न कर पाएं.

onMessageReceived का इस्तेमाल ज़्यादातर मैसेज टाइप के लिए किया जाता है. हालांकि, इनमें ये अपवाद शामिल हैं:

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

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

संक्षेप में:

ऐप्लिकेशन की स्थिति सूचना डेटा दोनों
फ़ोरग्राउंड onMessageReceived onMessageReceived onMessageReceived
बैकग्राउंड सिस्टम ट्रे onMessageReceived सूचना: सिस्टम ट्रे
डेटा: इंटेंट की दूसरी चीज़ों में.
मैसेज के टाइप के बारे में ज़्यादा जानने के लिए, सूचनाएं और डेटा मैसेज देखें.

ऐप्लिकेशन मेनिफ़ेस्ट में बदलाव करें

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

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

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

कस्टम डिफ़ॉल्ट आइकॉन और पसंद के मुताबिक रंग सेट करने के लिए, इन लाइनों को application टैग में जोड़ें:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

Android, इसके लिए कस्टम डिफ़ॉल्ट आइकॉन दिखाता है

Android इसके लिए कस्टम डिफ़ॉल्ट रंग का इस्तेमाल करता है

  • सूचनाओं लिखने वाले व्यक्ति से मिले सूचनाओं वाले सभी मैसेज.
  • ऐसा कोई भी सूचना मैसेज जिसमें सूचना पेलोड में रंग को साफ़ तौर पर सेट न किया गया हो.

अगर कोई कस्टम डिफ़ॉल्ट आइकॉन सेट नहीं है और सूचना पेलोड में कोई आइकॉन सेट नहीं है, तो Android, रेंडर किए गए ऐप्लिकेशन आइकॉन को सफ़ेद रंग में दिखाता है.

onMessageReceived को बदलें

FirebaseMessagingService.onMessageReceived तरीके को बदलकर, उस remoteMessage ऑब्जेक्ट के हिसाब से कार्रवाइयां की जा सकती हैं और मैसेज का डेटा पाया जा सकता है:

Kotlin+KTX

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: ${remoteMessage.from}")

    // Check if message contains a data payload.
    if (remoteMessage.data.isNotEmpty()) {
        Log.d(TAG, "Message data payload: ${remoteMessage.data}")

        // Check if data needs to be processed by long running job
        if (needsToBeScheduled()) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

onDeletedMessages को बदलें

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

बैकग्राउंड में चलने वाले ऐप्लिकेशन में सूचना वाले मैसेज मैनेज करना

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

इसमें ऐसे मैसेज शामिल हैं जिनमें सूचना और डेटा पेलोड, दोनों शामिल होते हैं. साथ ही, इसमें Notifications कंसोल से भेजे गए सभी मैसेज भी शामिल होते हैं. इन मामलों में, सूचना को डिवाइस की सिस्टम ट्रे में भेजा जाता है. साथ ही, डेटा पेलोड को लॉन्चर की गतिविधि के इंटेंट के अतिरिक्त हिस्से में डिलीवर किया जाता है.

अपने ऐप्लिकेशन पर मैसेज डिलीवरी के बारे में अहम जानकारी के लिए, FCM रिपोर्टिंग डैशबोर्ड देखें. यह Apple और Android डिवाइस पर भेजे गए और खोले गए मैसेज की संख्या रिकॉर्ड करता है. साथ ही, इसमें Android ऐप्लिकेशन के "इंप्रेशन" (उपयोगकर्ताओं को दिखने वाली सूचनाएं) का डेटा भी रिकॉर्ड होता है.

मैसेज भेजने के अनुरोध बनाएं

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

बैकएंड पर, भेजने के तरीके में, विषय का पसंदीदा नाम बताएं, जैसा कि यहां दिखाया गया है:

Node.js के लिए

// The topic name can be optionally prefixed with "/topics/".
const topic = 'highScores';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Java

// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";

// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

Python

# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'

# See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    topic=topic,
)

# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

शुरू करें

// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Topic: topic,
}

// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

C#

// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";

// See documentation on defining a message payload.
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "score", "850" },
        { "time", "2:45" },
    },
    Topic = topic,
};

// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

REST

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}

cURL निर्देश:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "message": {
    "topic" : "foo-bar",
    "notification": {
      "body": "This is a Firebase Cloud Messaging Topic Message!",
      "title": "FCM Message"
    }
  }
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

विषयों के कॉम्बिनेशन को मैसेज भेजने के लिए, condition की जानकारी दें. यह एक बूलियन एक्सप्रेशन है, जो टारगेट किए गए विषयों के बारे में बताता है. उदाहरण के लिए, इस शर्त से उन डिवाइसों पर मैसेज भेजे जाएंगे जिन्होंने TopicA और TopicB या TopicC की सदस्यता ली है:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

FCM सबसे पहले ब्रैकेट में मौजूद किसी भी शर्त का आकलन करता है. इसके बाद, बाईं से दाईं ओर एक्सप्रेशन का आकलन करता है. ऊपर दिए गए एक्सप्रेशन में, किसी एक विषय की सदस्यता लेने वाले उपयोगकर्ता को मैसेज नहीं मिलता. इसी तरह, TopicA की सदस्यता न लेने वाले लोगों को भी मैसेज नहीं मिलता. इन कॉम्बिनेशन को यह कॉन्टेंट मिलता है:

  • TopicA और TopicB
  • TopicA और TopicC

कंडिशनल एक्सप्रेशन में ज़्यादा से ज़्यादा पांच विषय शामिल किए जा सकते हैं.

किसी शर्त पर भेजने के लिए:

Node.js के लिए

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';

// See documentation on defining a message payload.
const message = {
  notification: {
    title: '$FooCorp up 1.43% on the day',
    body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
  },
  condition: condition
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Java

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
Message message = Message.builder()
    .setNotification(Notification.builder()
        .setTitle("$GOOG up 1.43% on the day")
        .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
        .build())
    .setCondition(condition)
    .build();

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

Python

# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"

# See documentation on defining a message payload.
message = messaging.Message(
    notification=messaging.Notification(
        title='$GOOG up 1.43% on the day',
        body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
    ),
    condition=condition,
)

# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

शुरू करें

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Condition: condition,
}

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

C#

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
var message = new Message()
{
    Notification = new Notification()
    {
        Title = "$GOOG up 1.43% on the day",
        Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
    },
    Condition = condition,
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

REST

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
   "message":{
    "condition": "'dogs' in topics || 'cats' in topics",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
    }
  }
}

cURL निर्देश:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "notification": {
    "title": "FCM Message",
    "body": "This is a Firebase Cloud Messaging Topic Message!",
  },
  "condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

अगले चरण