Android पर किसी खास विषय से जुड़े मैसेज की सुविधा

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

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

विषयों के बारे में ध्यान रखने योग्य कुछ चीज़ें:

  • किसी विषय के बारे में मैसेज करने की सुविधा, मौसम या सार्वजनिक तौर पर उपलब्ध अन्य जानकारी जैसे कॉन्टेंट के लिए सबसे सही होती है.
  • विषय से जुड़े मैसेज को प्रोसेस में लगने वाले समय के हिसाब से ऑप्टिमाइज़ किया जाता है, न कि इंतज़ार के समय के हिसाब से. एक डिवाइस या डिवाइसों के छोटे ग्रुप पर तेज़, सुरक्षित डिलीवरी के लिए, मैसेज को रजिस्ट्रेशन टोकन पर टारगेट करें, विषयों के हिसाब से नहीं.
  • अगर आपको हर उपयोगकर्ता के लिए एक से ज़्यादा डिवाइसों पर मैसेज भेजने हैं, तो डिवाइस ग्रुप मैसेज सेवा का इस्तेमाल करें.
  • विषय से जुड़ी मैसेज सेवा, हर विषय के लिए अनलिमिटेड सदस्यता की सुविधा देती है. हालांकि, FCM इन मामलों में सीमाएं लागू करता है:
    • किसी एक ऐप्लिकेशन इंस्टेंस पर 2,000 से ज़्यादा विषयों की सदस्यता नहीं ली जा सकती.
    • अगर ऐप्लिकेशन के इंस्टेंस की सदस्यता लेने के लिए, बैच इंपोर्ट का इस्तेमाल किया जा रहा है, तो हर अनुरोध के लिए, 1,000 ऐप्लिकेशन इंस्टेंस इस्तेमाल किए जा सकते हैं.
    • हर प्रोजेक्ट के लिए, नई सदस्यताओं की फ़्रीक्वेंसी सीमित होती है. अगर कम समय में, सदस्यता के बहुत सारे अनुरोध भेजे जाते हैं, तो FCM सर्वर 429 RESOURCE_EXHAUSTED ("कोटा से ज़्यादा हो गया") जवाब के साथ जवाब देंगे. एक्सपोनेन्शियल बैकऑफ़ के साथ फिर से कोशिश करें.

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

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

सर्वर पर विषय से जुड़ी सदस्यताएं मैनेज करना

Firebase एडमिन SDK की मदद से, सर्वर साइड से विषय को मैनेज करने के बुनियादी काम किए जा सकते हैं. उनके रजिस्ट्रेशन टोकन के आधार पर, सर्वर लॉजिक का इस्तेमाल करके कई क्लाइंट ऐप्लिकेशन इंस्टेंस की एक साथ सदस्यता ली जा सकती है और उनकी सदस्यता छोड़ी जा सकती है.

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

किसी विषय से जुड़े डिवाइसों की सदस्यता लेने के लिए, Firebase एडमिन SDK टूल की सदस्यता के तरीके में रजिस्ट्रेशन टोकन की एक सूची पास की जा सकती है:

Node.js के लिए

// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
  'YOUR_REGISTRATION_TOKEN_1',
  // ...
  'YOUR_REGISTRATION_TOKEN_n'
];

// Subscribe the devices corresponding to the registration tokens to the
// topic.
getMessaging().subscribeToTopic(registrationTokens, topic)
  .then((response) => {
    // See the MessagingTopicManagementResponse reference documentation
    // for the contents of response.
    console.log('Successfully subscribed to topic:', response);
  })
  .catch((error) => {
    console.log('Error subscribing to topic:', error);
  });

Java

// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n"
);

// Subscribe the devices corresponding to the registration tokens to the
// topic.
TopicManagementResponse response = FirebaseMessaging.getInstance().subscribeToTopic(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were subscribed successfully");

Python

# These registration tokens come from the client FCM SDKs.
registration_tokens = [
    'YOUR_REGISTRATION_TOKEN_1',
    # ...
    'YOUR_REGISTRATION_TOKEN_n',
]

# Subscribe the devices corresponding to the registration tokens to the
# topic.
response = messaging.subscribe_to_topic(registration_tokens, topic)
# See the TopicManagementResponse reference documentation
# for the contents of response.
print(response.success_count, 'tokens were subscribed successfully')

शुरू करें

// These registration tokens come from the client FCM SDKs.
registrationTokens := []string{
	"YOUR_REGISTRATION_TOKEN_1",
	// ...
	"YOUR_REGISTRATION_TOKEN_n",
}

// Subscribe the devices corresponding to the registration tokens to the
// topic.
response, err := client.SubscribeToTopic(ctx, registrationTokens, topic)
if err != nil {
	log.Fatalln(err)
}
// See the TopicManagementResponse reference documentation
// for the contents of response.
fmt.Println(response.SuccessCount, "tokens were subscribed successfully")

C#

// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n",
};

// Subscribe the devices corresponding to the registration tokens to the
// topic
var response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were subscribed successfully");

एडमिन FCM एपीआई आपको किसी विषय से जुड़े डिवाइसों की सदस्यता छोड़ने की सुविधा भी देता है. इसके लिए, रजिस्ट्रेशन टोकन को सही तरीके से पास किया जाता है:

Node.js के लिए

// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
  'YOUR_REGISTRATION_TOKEN_1',
  // ...
  'YOUR_REGISTRATION_TOKEN_n'
];

// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
getMessaging().unsubscribeFromTopic(registrationTokens, topic)
  .then((response) => {
    // See the MessagingTopicManagementResponse reference documentation
    // for the contents of response.
    console.log('Successfully unsubscribed from topic:', response);
  })
  .catch((error) => {
    console.log('Error unsubscribing from topic:', error);
  });

Java

// These registration tokens come from the client FCM SDKs.
List<String> registrationTokens = Arrays.asList(
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n"
);

// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
TopicManagementResponse response = FirebaseMessaging.getInstance().unsubscribeFromTopic(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");

Python

# These registration tokens come from the client FCM SDKs.
registration_tokens = [
    'YOUR_REGISTRATION_TOKEN_1',
    # ...
    'YOUR_REGISTRATION_TOKEN_n',
]

# Unubscribe the devices corresponding to the registration tokens from the
# topic.
response = messaging.unsubscribe_from_topic(registration_tokens, topic)
# See the TopicManagementResponse reference documentation
# for the contents of response.
print(response.success_count, 'tokens were unsubscribed successfully')

शुरू करें

// These registration tokens come from the client FCM SDKs.
registrationTokens := []string{
	"YOUR_REGISTRATION_TOKEN_1",
	// ...
	"YOUR_REGISTRATION_TOKEN_n",
}

// Unsubscribe the devices corresponding to the registration tokens from
// the topic.
response, err := client.UnsubscribeFromTopic(ctx, registrationTokens, topic)
if err != nil {
	log.Fatalln(err)
}
// See the TopicManagementResponse reference documentation
// for the contents of response.
fmt.Println(response.SuccessCount, "tokens were unsubscribed successfully")

C#

// These registration tokens come from the client FCM SDKs.
var registrationTokens = new List<string>()
{
    "YOUR_REGISTRATION_TOKEN_1",
    // ...
    "YOUR_REGISTRATION_TOKEN_n",
};

// Unsubscribe the devices corresponding to the registration tokens from the
// topic
var response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync(
    registrationTokens, topic);
// See the TopicManagementResponse reference documentation
// for the contents of response.
Console.WriteLine($"{response.SuccessCount} tokens were unsubscribed successfully");

subscribeToTopic() और unsubscribeFromTopic() तरीके से ऐसा ऑब्जेक्ट मिलता है जिसमें FCM से मिला रिस्पॉन्स शामिल है. अनुरोध में बताए गए रजिस्ट्रेशन टोकन की संख्या चाहे कुछ भी हो, रिटर्न टाइप का फ़ॉर्मैट एक ही होता है.

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

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

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 सूचना वाले मैसेज को सिस्टम ट्रे में भेजता है. जब कोई उपयोगकर्ता सूचना पर टैप करता है, तो ऐप्लिकेशन लॉन्चर डिफ़ॉल्ट रूप से खुल जाता है.

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

अपने ऐप्लिकेशन में मैसेज डिलीवरी के बारे में अहम जानकारी पाने के लिए, 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

अगले चरण