Firebase is back at Google I/O on May 10! Register now

एकाधिक उपकरणों पर संदेश भेजें

संग्रह की मदद से व्यवस्थित रहें अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.

फायरबेस क्लाउड मैसेजिंग एक संदेश को कई उपकरणों पर लक्षित करने के दो तरीके प्रदान करता है:

  • विषय संदेश , जो आपको एक से अधिक उपकरणों पर संदेश भेजने की अनुमति देता है जिन्होंने किसी विशेष विषय को चुना है।
  • उपकरण समूह संदेश सेवा , जो आपको आपके द्वारा परिभाषित समूह से संबंधित अनेक उपकरणों को संदेश भेजने की अनुमति देती है।

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

एसडीके सेट करें

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

शुरू करने से पहले

  • Android Studio को उसके नवीनतम संस्करण में स्थापित या अपडेट करें।

  • सुनिश्चित करें कि आपकी परियोजना इन आवश्यकताओं को पूरा करती है:

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

  • अपने Google खाते का उपयोग करके फायरबेस में साइन इन करें

यदि आपके पास पहले से Android प्रोजेक्ट नहीं है और आप केवल Firebase उत्पाद को आज़माना चाहते हैं, तो आप हमारे क्विकस्टार्ट नमूनों में से एक को डाउनलोड कर सकते हैं।

एक फायरबेस प्रोजेक्ट बनाएं

इससे पहले कि आप अपने Android ऐप में Firebase जोड़ सकें, आपको अपने Android ऐप से कनेक्ट करने के लिए एक Firebase प्रोजेक्ट बनाना होगा। फायरबेस प्रोजेक्ट्स के बारे में अधिक जानने के लिए अंडरस्टैंड फायरबेस प्रोजेक्ट्स पर जाएं।

अपने ऐप को फायरबेस के साथ पंजीकृत करें

अपने Android ऐप में Firebase का उपयोग करने के लिए, आपको अपने ऐप को अपने Firebase प्रोजेक्ट के साथ पंजीकृत करना होगा। अपने ऐप को पंजीकृत करने को अक्सर आपके ऐप को आपके प्रोजेक्ट में "जोड़ना" कहा जाता है।

  1. फायरबेस कंसोल पर जाएं।

  2. प्रोजेक्ट अवलोकन पृष्ठ के केंद्र में, सेटअप वर्कफ़्लो लॉन्च करने के लिए Android आइकन ( ) या ऐप जोड़ें पर क्लिक करें।

  3. Android पैकेज नाम फ़ील्ड में अपने ऐप का पैकेज नाम दर्ज करें।

  4. (वैकल्पिक) अन्य ऐप जानकारी दर्ज करें: ऐप उपनाम और डीबग हस्ताक्षर प्रमाणपत्र SHA-1

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

एक फायरबेस कॉन्फ़िगरेशन फ़ाइल जोड़ें

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

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

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

  2. अपनी google-services.json कॉन्फ़िग फ़ाइल में मानों को Firebase SDKs तक पहुँचाने के लिए, आपको Google services Gradle प्लगइन ( google-services ) की आवश्यकता है।

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

      buildscript {
      
          repositories {
            // Make sure that you have the following two repositories
            google()  // Google's Maven repository
            mavenCentral()  // Maven Central repository
          }
      
          dependencies {
            ...
      
            // Add the dependency for the Google services Gradle plugin
            classpath 'com.google.gms:google-services:4.3.15'
          }
      }
      
      allprojects {
        ...
      
        repositories {
          // Make sure that you have the following two repositories
          google()  // Google's Maven repository
          mavenCentral()  // Maven Central repository
        }
      }
      
    2. अपने मॉड्यूल (ऐप-लेवल) ग्रैडल फ़ाइल (आमतौर पर <project>/<app-module>/build.gradle ) में, Google सर्विसेज प्लगइन जोड़ें:

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

अपने ऐप में फायरबेस एसडीके जोड़ें

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

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

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.2.2')
    
        // 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 लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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:23.1.1'
        implementation 'com.google.firebase:firebase-analytics:21.2.0'
    }
    

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.2.2')
    
        // 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-ktx'
        implementation 'com.google.firebase:firebase-analytics-ktx'
    }
    

    Firebase Android BoM का उपयोग करके, आपका ऐप हमेशा Firebase Android पुस्तकालयों के संगत संस्करणों का उपयोग करेगा।

    (वैकल्पिक) BoM का उपयोग किए बिना Firebase लाइब्रेरी निर्भरताएँ जोड़ें

    यदि आप Firebase BoM का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक Firebase लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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-ktx:23.1.1'
        implementation 'com.google.firebase:firebase-analytics-ktx:21.2.0'
    }
    

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

किसी विषय के क्लाइंट ऐप को सब्सक्राइब करें

क्लाइंट ऐप्स किसी भी मौजूदा विषय की सदस्यता ले सकते हैं, या वे एक नया विषय बना सकते हैं। जब कोई क्लाइंट ऐप किसी नए विषय के नाम की सदस्यता लेता है (वह जो आपके फायरबेस प्रोजेक्ट के लिए पहले से मौजूद नहीं है), उस नाम का एक नया विषय 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();
            }
        });

सदस्यता समाप्त करने के लिए, क्लाइंट ऐप विषय के नाम के साथ फायरबेस क्लाउड मैसेजिंग unsubscribeFromTopic() को कॉल करता है।

विषय संदेश प्राप्त करें और प्रबंधित करें

FCM अन्य डाउनस्ट्रीम संदेशों की तरह ही विषय संदेशों को डिलीवर करता है।

संदेश प्राप्त करने के लिए, ऐसी सेवा का उपयोग करें जो FirebaseMessagingService का विस्तार करती है। आपकी सेवा को onMessageReceived और onDeletedMessages कॉलबैक को ओवरराइड करना चाहिए। इसे प्राप्ति के 20 सेकंड के भीतर किसी भी संदेश को हैंडल करना चाहिए (एंड्रॉइड मार्शमैलो पर 10 सेकंड)। onMessageReceived को कॉल करने से पहले हुए ओएस विलंब के आधार पर समय विंडो कम हो सकती है। उस समय के बाद, Android O की पृष्ठभूमि निष्पादन सीमा जैसे विभिन्न OS व्यवहार आपके काम को पूरा करने की आपकी क्षमता में हस्तक्षेप कर सकते हैं। अधिक जानकारी के लिए संदेश प्राथमिकता पर हमारा अवलोकन देखें।

निम्न अपवादों के साथ, अधिकांश संदेश प्रकारों के लिए 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 के लिए कस्टम डिफ़ॉल्ट रंग का उपयोग करता है

  • नोटिफिकेशन कंपोजर से भेजे गए सभी नोटिफिकेशन संदेश।
  • कोई सूचना संदेश जो अधिसूचना पेलोड में स्पष्ट रूप से रंग सेट नहीं करता है।

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

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}")

        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.
    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() पर कॉलबैक मिल सकता है। अगर आपने पिछले 4 हफ़्तों में उस डिवाइस पर ऐप को संदेश नहीं भेजा है, तो FCM onDeletedMessages() को कॉल नहीं करेगा।

एक पृष्ठभूमि वाले ऐप में सूचना संदेशों को संभालें

जब आपका ऐप पृष्ठभूमि में होता है, तो Android अधिसूचना संदेशों को सिस्टम ट्रे में निर्देशित करता है। अधिसूचना पर एक उपयोगकर्ता टैप डिफ़ॉल्ट रूप से ऐप लॉन्चर खोलता है।

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

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

पृष्ठभूमि प्रतिबंधित ऐप्स (एंड्रॉइड पी या नया)

FCM उन ऐप्स को संदेश डिलीवर नहीं कर सकता है जिन्हें उपयोगकर्ता द्वारा पृष्ठभूमि प्रतिबंध में डाल दिया गया था (जैसे: सेटिंग -> ऐप्स और अधिसूचना -> [ऐपनाम] -> बैटरी)। एक बार आपके ऐप को पृष्ठभूमि प्रतिबंध से हटा दिए जाने के बाद, ऐप में नए संदेश पहले की तरह डिलीवर हो जाएंगे। खोए हुए संदेशों और अन्य पृष्ठभूमि प्रतिबंध प्रभावों को रोकने के लिए, सुनिश्चित करें कि Android Vitas प्रयास द्वारा सूचीबद्ध खराब व्यवहारों से बचना है। इन व्यवहारों के कारण Android डिवाइस उपयोगकर्ता को यह अनुशंसा कर सकता है कि आपका ऐप पृष्ठभूमि प्रतिबंधित हो। isBackgroundRestricted() का उपयोग करके आपका ऐप यह जांच सकता है कि यह पृष्ठभूमि प्रतिबंधित है या नहीं।

भेजें अनुरोध बनाएँ

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

बैकएंड पर आपके भेजने के तर्क में, दिखाए गए अनुसार वांछित विषय का नाम निर्दिष्ट करें:

नोड.जेएस

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

जावा

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

अजगर

# 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)

सी#

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

आराम

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 -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

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

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

FCM पहले कोष्ठकों में किसी भी स्थिति का मूल्यांकन करता है, और फिर बाएँ से दाएँ अभिव्यक्ति का मूल्यांकन करता है। उपरोक्त अभिव्यक्ति में, किसी एक विषय की सदस्यता लेने वाले उपयोगकर्ता को संदेश प्राप्त नहीं होता है। इसी तरह, एक उपयोगकर्ता जो TopicA की सदस्यता नहीं लेता है, उसे संदेश प्राप्त नहीं होता है। ये संयोजन इसे प्राप्त करते हैं:

  • TopicA और TopicB
  • TopicA और TopicC

आप अपनी सशर्त अभिव्यक्ति में अधिकतम पाँच विषय शामिल कर सकते हैं।

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

नोड.जेएस

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

जावा

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

अजगर

# 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)

सी#

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

आराम

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 -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

अगले कदम

,

फायरबेस क्लाउड मैसेजिंग एक संदेश को कई उपकरणों पर लक्षित करने के दो तरीके प्रदान करता है:

  • विषय संदेश , जो आपको एक से अधिक उपकरणों पर संदेश भेजने की अनुमति देता है जिन्होंने किसी विशेष विषय को चुना है।
  • उपकरण समूह संदेश सेवा , जो आपको आपके द्वारा परिभाषित समूह से संबंधित अनेक उपकरणों को संदेश भेजने की अनुमति देती है।

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

एसडीके सेट करें

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

शुरू करने से पहले

  • Android Studio को उसके नवीनतम संस्करण में स्थापित या अपडेट करें।

  • सुनिश्चित करें कि आपकी परियोजना इन आवश्यकताओं को पूरा करती है:

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

  • अपने Google खाते का उपयोग करके फायरबेस में साइन इन करें

यदि आपके पास पहले से Android प्रोजेक्ट नहीं है और आप केवल Firebase उत्पाद को आज़माना चाहते हैं, तो आप हमारे क्विकस्टार्ट नमूनों में से एक को डाउनलोड कर सकते हैं।

एक फायरबेस प्रोजेक्ट बनाएं

इससे पहले कि आप अपने Android ऐप में Firebase जोड़ सकें, आपको अपने Android ऐप से कनेक्ट करने के लिए एक Firebase प्रोजेक्ट बनाना होगा। फायरबेस प्रोजेक्ट्स के बारे में अधिक जानने के लिए अंडरस्टैंड फायरबेस प्रोजेक्ट्स पर जाएं।

अपने ऐप को फायरबेस के साथ पंजीकृत करें

अपने Android ऐप में Firebase का उपयोग करने के लिए, आपको अपने ऐप को अपने Firebase प्रोजेक्ट के साथ पंजीकृत करना होगा। अपने ऐप को पंजीकृत करने को अक्सर आपके ऐप को आपके प्रोजेक्ट में "जोड़ना" कहा जाता है।

  1. फायरबेस कंसोल पर जाएं।

  2. प्रोजेक्ट अवलोकन पृष्ठ के केंद्र में, सेटअप वर्कफ़्लो लॉन्च करने के लिए Android आइकन ( ) या ऐप जोड़ें पर क्लिक करें।

  3. Android पैकेज नाम फ़ील्ड में अपने ऐप का पैकेज नाम दर्ज करें।

  4. (वैकल्पिक) अन्य ऐप जानकारी दर्ज करें: ऐप उपनाम और डीबग हस्ताक्षर प्रमाणपत्र SHA-1

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

एक फायरबेस कॉन्फ़िगरेशन फ़ाइल जोड़ें

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

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

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

  2. अपनी google-services.json कॉन्फ़िग फ़ाइल में मानों को Firebase SDKs तक पहुँचाने के लिए, आपको Google services Gradle प्लगइन ( google-services ) की आवश्यकता है।

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

      buildscript {
      
          repositories {
            // Make sure that you have the following two repositories
            google()  // Google's Maven repository
            mavenCentral()  // Maven Central repository
          }
      
          dependencies {
            ...
      
            // Add the dependency for the Google services Gradle plugin
            classpath 'com.google.gms:google-services:4.3.15'
          }
      }
      
      allprojects {
        ...
      
        repositories {
          // Make sure that you have the following two repositories
          google()  // Google's Maven repository
          mavenCentral()  // Maven Central repository
        }
      }
      
    2. अपने मॉड्यूल (ऐप-लेवल) ग्रैडल फ़ाइल (आमतौर पर <project>/<app-module>/build.gradle ) में, Google सर्विसेज प्लगइन जोड़ें:

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

अपने ऐप में फायरबेस एसडीके जोड़ें

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

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

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.2.2')
    
        // 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 लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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:23.1.1'
        implementation 'com.google.firebase:firebase-analytics:21.2.0'
    }
    

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:31.2.2')
    
        // 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-ktx'
        implementation 'com.google.firebase:firebase-analytics-ktx'
    }
    

    Firebase Android BoM का उपयोग करके, आपका ऐप हमेशा Firebase Android पुस्तकालयों के संगत संस्करणों का उपयोग करेगा।

    (वैकल्पिक) BoM का उपयोग किए बिना Firebase लाइब्रेरी निर्भरताएँ जोड़ें

    यदि आप Firebase BoM का उपयोग नहीं करना चुनते हैं, तो आपको प्रत्येक Firebase लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।

    ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।

    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-ktx:23.1.1'
        implementation 'com.google.firebase:firebase-analytics-ktx:21.2.0'
    }
    

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

किसी विषय के क्लाइंट ऐप को सब्सक्राइब करें

क्लाइंट ऐप्स किसी भी मौजूदा विषय की सदस्यता ले सकते हैं, या वे एक नया विषय बना सकते हैं। जब कोई क्लाइंट ऐप किसी नए विषय के नाम की सदस्यता लेता है (वह जो आपके फायरबेस प्रोजेक्ट के लिए पहले से मौजूद नहीं है), उस नाम का एक नया विषय 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();
            }
        });

सदस्यता समाप्त करने के लिए, क्लाइंट ऐप विषय के नाम के साथ फायरबेस क्लाउड मैसेजिंग unsubscribeFromTopic() को कॉल करता है।

विषय संदेश प्राप्त करें और प्रबंधित करें

FCM अन्य डाउनस्ट्रीम संदेशों की तरह ही विषय संदेशों को डिलीवर करता है।

संदेश प्राप्त करने के लिए, ऐसी सेवा का उपयोग करें जो FirebaseMessagingService का विस्तार करती है। आपकी सेवा को onMessageReceived और onDeletedMessages कॉलबैक को ओवरराइड करना चाहिए। इसे प्राप्ति के 20 सेकंड के भीतर किसी भी संदेश को हैंडल करना चाहिए (एंड्रॉइड मार्शमैलो पर 10 सेकंड)। onMessageReceived को कॉल करने से पहले हुए ओएस विलंब के आधार पर समय विंडो कम हो सकती है। उस समय के बाद, Android O की पृष्ठभूमि निष्पादन सीमा जैसे विभिन्न OS व्यवहार आपके काम को पूरा करने की आपकी क्षमता में हस्तक्षेप कर सकते हैं। अधिक जानकारी के लिए संदेश प्राथमिकता पर हमारा अवलोकन देखें।

निम्न अपवादों के साथ, अधिकांश संदेश प्रकारों के लिए 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 के लिए कस्टम डिफ़ॉल्ट रंग का उपयोग करता है

  • नोटिफिकेशन कंपोजर से भेजे गए सभी नोटिफिकेशन संदेश।
  • कोई सूचना संदेश जो अधिसूचना पेलोड में स्पष्ट रूप से रंग सेट नहीं करता है।

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

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}")

        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.
    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() पर कॉलबैक मिल सकता है। अगर आपने पिछले 4 हफ़्तों में उस डिवाइस पर ऐप को संदेश नहीं भेजा है, तो FCM onDeletedMessages() को कॉल नहीं करेगा।

एक पृष्ठभूमि वाले ऐप में सूचना संदेशों को संभालें

जब आपका ऐप पृष्ठभूमि में होता है, तो Android अधिसूचना संदेशों को सिस्टम ट्रे में निर्देशित करता है। अधिसूचना पर एक उपयोगकर्ता टैप डिफ़ॉल्ट रूप से ऐप लॉन्चर खोलता है।

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

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

पृष्ठभूमि प्रतिबंधित ऐप्स (एंड्रॉइड पी या नया)

FCM उन ऐप्स को संदेश डिलीवर नहीं कर सकता है जिन्हें उपयोगकर्ता द्वारा पृष्ठभूमि प्रतिबंध में डाल दिया गया था (जैसे: सेटिंग -> ऐप्स और अधिसूचना -> [ऐपनाम] -> बैटरी)। एक बार आपके ऐप को पृष्ठभूमि प्रतिबंध से हटा दिए जाने के बाद, ऐप में नए संदेश पहले की तरह डिलीवर हो जाएंगे। खोए हुए संदेशों और अन्य पृष्ठभूमि प्रतिबंध प्रभावों को रोकने के लिए, सुनिश्चित करें कि Android Vitas प्रयास द्वारा सूचीबद्ध खराब व्यवहारों से बचना है। इन व्यवहारों के कारण Android डिवाइस उपयोगकर्ता को यह अनुशंसा कर सकता है कि आपका ऐप पृष्ठभूमि प्रतिबंधित हो। isBackgroundRestricted() का उपयोग करके आपका ऐप यह जांच सकता है कि यह पृष्ठभूमि प्रतिबंधित है या नहीं।

भेजें अनुरोध बनाएँ

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

बैकएंड पर आपके भेजने के तर्क में, दिखाए गए अनुसार वांछित विषय का नाम निर्दिष्ट करें:

नोड.जेएस

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

जावा

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

अजगर

# 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)

सी#

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

आराम

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 -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

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

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

FCM पहले कोष्ठकों में किसी भी स्थिति का मूल्यांकन करता है, और फिर बाएँ से दाएँ अभिव्यक्ति का मूल्यांकन करता है। उपरोक्त अभिव्यक्ति में, किसी एक विषय की सदस्यता लेने वाले उपयोगकर्ता को संदेश प्राप्त नहीं होता है। इसी तरह, एक उपयोगकर्ता जो TopicA की सदस्यता नहीं लेता है, उसे संदेश प्राप्त नहीं होता है। ये संयोजन इसे प्राप्त करते हैं:

  • TopicA और TopicB
  • TopicA और TopicC

आप अपनी सशर्त अभिव्यक्ति में अधिकतम पाँच विषय शामिल कर सकते हैं।

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

नोड.जेएस

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

जावा

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

अजगर

# 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)

सी#

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

आराम

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 -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

अगले कदम