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

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

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

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

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

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

किसी विषय की सदस्यता लेने के लिए, Firebase.Messaging.FirebaseMessaging.Subscribe अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है आपके ऐप्लिकेशन से. इससे FCM को एसिंक्रोनस अनुरोध मिलता है बैकएंड और क्लाइंट को दिए गए विषय की सदस्यता देता है.

Firebase.Messaging.FirebaseMessaging.Subscribe("/topics/example");

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

सदस्यता छोड़ने के लिए, कॉल करें Firebase.Messaging.FirebaseMessaging.Unsubscribe, और FCM, बैकग्राउंड में विषय की सदस्यता छोड़ता है.

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

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

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 विषय के मैसेज उसी तरह डिलीवर करता है जैसे अन्य डाउनस्ट्रीम में करता है मैसेज.

Firebase.Messaging.FirebaseMessaging.MessageReceived इवेंट की सदस्यता लेकर आपको मिलने वाले मैसेज के हिसाब से कार्रवाइयां की जा सकती हैं और मैसेज का डेटा मिल सकता है.

Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;

...

public void OnMessageReceived(object sender, Firebase.Messaging.MessageReceivedEventArgs e) {
  UnityEngine.Debug.Log("Received a new message");
  if (e.Message.From.Length > 0)
    UnityEngine.Debug.Log("from: " + e.Message.From);
  if (e.Message.Data.Count > 0) {
    UnityEngine.Debug.Log("data:");
    foreach (System.Collections.Generic.KeyValuePair iter in
             e.Message.Data) {
      UnityEngine.Debug.Log("  " + iter.Key + ": " + iter.Value);
    }
  }
}

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

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