एडमिन SDK या FCM v1 HTTP API का इस्तेमाल करके, किसी खास विषय की सदस्यता लेने वाले डिवाइसों को मैसेज भेजे जा सकते हैं.
ज़रूरी शर्तें
चुने गए तरीके का इस्तेमाल करके मैसेज भेजने के लिए, ज़रूरी एनवायरमेंट सेट अप करें. अपना सर्वर एनवायरमेंट सेट अप करना लेख पढ़ें.
क्लाइंट ऐप्लिकेशन को विषय की सदस्यता लेनी होगी. विषय की सदस्यताएं मैनेज करना लेख पढ़ें.
किसी विषय पर मैसेज भेजना
Firebase Admin SDK या FCM HTTP v1 API का इस्तेमाल करके, किसी विषय पर मैसेज भेजे जा सकते हैं.
Admin SDK का इस्तेमाल करना
अपने सर्वर से विषय के मैसेज भेजने के लिए, एडमिन SDK का इस्तेमाल किया जा सकता है. एडमिन एसडीके सेट अप करने के बारे में ज़्यादा जानने के लिए, एडमिन एसडीके का इस्तेमाल करके मैसेज भेजना लेख पढ़ें.
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);
HTTP v1 API का इस्तेमाल करना
HTTP v1 API का इस्तेमाल करके विषय के हिसाब से मैसेज भेजने के लिए, JSON POST अनुरोध बनाएं. एचटीटीपी v1 एपीआई का इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, FCM v1 एपीआई के ज़रिए मैसेज भेजना लेख पढ़ें.
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
विषय से जुड़ी शर्तों के हिसाब से ईमेल भेजना
विषयों के कॉम्बिनेशन को मैसेज भेजने के लिए, शर्त तय करें. यह एक बूलियन एक्सप्रेशन होता है, जो टारगेट किए गए विषयों के बारे में बताता है. उदाहरण के लिए, यहां दी गई शर्त के हिसाब से, मैसेज उन डिवाइसों पर भेजे जाएंगे जिन्होंने TopicA
और TopicB
या TopicC
की सदस्यता ली है:
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
FCM सबसे पहले, ब्रैकेट में दी गई शर्तों का आकलन करता है. इसके बाद, एक्सप्रेशन का आकलन बाएं से दाएं करता है. शर्त के हिसाब से तय किए गए एक्सप्रेशन में, ज़्यादा से ज़्यादा पांच विषय शामिल किए जा सकते हैं.
Admin SDK का इस्तेमाल करना
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);
HTTP v1 API का इस्तेमाल करना
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
ऐप्लिकेशन में विषय के हिसाब से मैसेज मैनेज करना
किसी विषय पर मैसेज भेजने के बाद, FCM उसे सदस्यता लेने वाले सभी क्लाइंट ऐप्लिकेशन इंस्टेंस को डिलीवर करता है.
अपने Android, iOS या वेब ऐप्लिकेशन में मैसेज पाने और उन्हें प्रोसेस करने का तरीका जानने के लिए, मैसेज पाना लेख पढ़ें.