वेब/JavaScript पर विषयों के लिए मैसेज भेजें

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

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

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

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

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

आपके पास रजिस्ट्रेशन टोकन की एक सूची, Firebase Admin 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");

Admin FCM API की मदद से, डिवाइसों को किसी विषय से सदस्यता रद्द करने के लिए, रजिस्टर करने के टोकन को सही तरीके से पास किया जा सकता है:

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, विषय के मैसेज को उसी तरह डिलीवर करता है जिस तरह अन्य डाउनस्ट्रीम मैसेज डिलीवर किए जाते हैं. क्लाइंट पर मैसेज मैनेज करने का तरीका, वेब पेज के फ़ोरग्राउंड/बैकग्राउंड स्टेटस और इस सेक्शन में बताए गए अन्य फ़ैक्टर पर निर्भर करता है.

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

ऐप्लिकेशन की स्थिति सूचना डेटा दोनों
फ़ोरग्राउंड onMessage onMessage onMessage
बैकग्राउंड (सर्विस वर्कर) onBackgroundMessage (डिसप्ले की सूचना अपने-आप दिखने लगेगी) onBackgroundMessage onBackgroundMessage (डिसप्ले सूचना अपने-आप दिखती है)

वेब ऐप्लिकेशन के फ़ोरग्राउंड में होने पर मैसेज मैनेज करना

onMessage इवेंट पाने के लिए, आपके ऐप्लिकेशन को firebase-messaging-sw.js में Firebase मैसेजिंग सेवा वर्कर्स तय करना होगा. इसके अलावा, SDK टूल को किसी मौजूदा सर्विस वर्कर के ज़रिए भी जोड़ा जा सकता है. इसके लिए: getToken(): Promise<string>.

Web

import { initializeApp } from "firebase/app";
import { getMessaging } from "firebase/messaging/sw";

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
const firebaseApp = initializeApp({
  apiKey: 'api-key',
  authDomain: 'project-id.firebaseapp.com',
  databaseURL: 'https://project-id.firebaseio.com',
  projectId: 'project-id',
  storageBucket: 'project-id.appspot.com',
  messagingSenderId: 'sender-id',
  appId: 'app-id',
  measurementId: 'G-measurement-id',
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = getMessaging(firebaseApp);

Web

// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
  apiKey: 'api-key',
  authDomain: 'project-id.firebaseapp.com',
  databaseURL: 'https://project-id.firebaseio.com',
  projectId: 'project-id',
  storageBucket: 'project-id.appspot.com',
  messagingSenderId: 'sender-id',
  appId: 'app-id',
  measurementId: 'G-measurement-id',
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

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

Web

// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
//   `messaging.onBackgroundMessage` handler.
import { getMessaging, onMessage } from "firebase/messaging";

const messaging = getMessaging();
onMessage(messaging, (payload) => {
  console.log('Message received. ', payload);
  // ...
});

Web

// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
//   `messaging.onBackgroundMessage` handler.
messaging.onMessage((payload) => {
  console.log('Message received. ', payload);
  // ...
});

वेब ऐप्लिकेशन के बैकग्राउंड में होने पर भी मैसेज मैनेज करना

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

भेजने के अनुरोध में सूचना के विकल्प सेट करना

ऐप्लिकेशन सर्वर से भेजे गए सूचना मैसेज के लिए, FCM JavaScript API में fcm_options.link की काम करती है. आम तौर पर, यह आपके वेब ऐप्लिकेशन के किसी पेज पर सेट होता है:

https://fcm.googleapis.com//v1/projects/<YOUR-PROJECT-ID>/messages:send
Content-Type: application/json
Authorization: bearer <YOUR-ACCESS-TOKEN>

{
  "message": {
    "topic": "matchday",
    "notification": {
      "title": "Background Message Title",
      "body": "Background message body"
    },
    "webpush": {
      "fcm_options": {
        "link": "https://dummypage.com"
      }
    }
  }
}

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

डेटा मैसेज के साथ fcm_options.link काम नहीं करता. इसलिए, आपको ऐसा करने का सुझाव दिया जाता है सभी डेटा मैसेज में सूचना पेलोड जोड़ना होगा. इसके अलावा, सेवा वर्कर का इस्तेमाल करके भी सूचनाएं मैनेज की जा सकती हैं.

सूचना और डेटा मैसेज के बीच के अंतर के बारे में जानने के लिए, देखें मैसेज के टाइप.

सर्विस वर्कर में सूचना विकल्प सेट करना

डेटा मैसेज के लिए, सर्विस वर्कर में सूचना के विकल्प सेट किए जा सकते हैं. सबसे पहले, सेवा वर्कर में अपना ऐप्लिकेशन शुरू करें:

Web

import { initializeApp } from "firebase/app";
import { getMessaging } from "firebase/messaging/sw";

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
const firebaseApp = initializeApp({
  apiKey: 'api-key',
  authDomain: 'project-id.firebaseapp.com',
  databaseURL: 'https://project-id.firebaseio.com',
  projectId: 'project-id',
  storageBucket: 'project-id.appspot.com',
  messagingSenderId: 'sender-id',
  appId: 'app-id',
  measurementId: 'G-measurement-id',
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = getMessaging(firebaseApp);

Web

// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
  apiKey: 'api-key',
  authDomain: 'project-id.firebaseapp.com',
  databaseURL: 'https://project-id.firebaseio.com',
  projectId: 'project-id',
  storageBucket: 'project-id.appspot.com',
  messagingSenderId: 'sender-id',
  appId: 'app-id',
  measurementId: 'G-measurement-id',
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

विकल्प सेट करने के लिए, firebase-messaging-sw.js में onBackgroundMessage को कॉल करें. इस उदाहरण में, हमने टाइटल, मुख्य हिस्से, और आइकॉन फ़ील्ड के साथ सूचना बनाई है.

Web

import { getMessaging } from "firebase/messaging/sw";
import { onBackgroundMessage } from "firebase/messaging/sw";

const messaging = getMessaging();
onBackgroundMessage(messaging, (payload) => {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png'
  };

  self.registration.showNotification(notificationTitle,
    notificationOptions);
});

Web

messaging.onBackgroundMessage((payload) => {
  console.log(
    '[firebase-messaging-sw.js] Received background message ',
    payload
  );
  // Customize notification here
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png'
  };

  self.registration.showNotification(notificationTitle, notificationOptions);
});

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

विषय बनाने के बाद, क्लाइंट ऐप्लिकेशन के इंस्टेंस की सदस्यता लेकर क्लाइंट-साइड पर या server API, तो आप विषय. अगर आपने पहली बार 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

अगले चरण