फायरबेस क्लाउड मैसेजिंग एक संदेश को कई उपकरणों पर लक्षित करने के लिए ये दो तरीके प्रदान करता है:
- विषय संदेश , जो आपको एक से अधिक उपकरणों पर संदेश भेजने की अनुमति देता है जिन्होंने किसी विशेष विषय को चुना है।
- उपकरण समूह संदेश सेवा , जो आपको आपके द्वारा परिभाषित समूह से संबंधित अनेक उपकरणों को संदेश भेजने की अनुमति देती है।
यह ट्यूटोरियल FCM के लिए एडमिन SDK या REST API का उपयोग करके आपके ऐप सर्वर से विषय संदेश भेजने और उन्हें वेब ऐप में प्राप्त करने और प्रबंधित करने पर केंद्रित है। हम पृष्ठभूमि वाले और अग्रभूमि वाले दोनों प्रकार के ऐप्स के लिए संदेश प्रबंधन को कवर करेंगे।
एसडीके सेट करें
यदि आपने FCM के लिए JavaScript क्लाइंट ऐप सेट अप किया है या संदेश प्राप्त करने के चरणों के माध्यम से काम किया है तो इस अनुभाग में आपके द्वारा पहले ही पूरे किए गए चरण शामिल हो सकते हैं।
FCM SDK को जोड़ें और आरंभ करें
यदि आपके पास पहले से नहीं है, तो Firebase JS SDK इंस्टॉल करें और Firebase को इनिशियलाइज़ करें ।
फायरबेस क्लाउड मैसेजिंग जेएस एसडीके जोड़ें और फायरबेस क्लाउड मैसेजिंग को इनिशियलाइज़ करें:
Web version 9
import { initializeApp } from "firebase/app"; import { getMessaging } from "firebase/messaging"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase const app = initializeApp(firebaseConfig); // Initialize Firebase Cloud Messaging and get a reference to the service const messaging = getMessaging(app);
Web version 8
import firebase from "firebase/app"; import "firebase/messaging"; // TODO: Replace the following with your app's Firebase project configuration // See: https://firebase.google.com/docs/web/learn-more#config-object const firebaseConfig = { // ... }; // Initialize Firebase firebase.initializeApp(firebaseConfig); // Initialize Firebase Cloud Messaging and get a reference to the service const messaging = firebase.messaging();
पंजीकरण टोकन तक पहुंचें
जब आपको किसी ऐप इंस्टेंस के लिए वर्तमान पंजीकरण टोकन को पुनः प्राप्त करने की आवश्यकता होती है, तो पहले Notification.requestPermission()
वाले उपयोगकर्ता से अधिसूचना अनुमतियों का अनुरोध करें। दिखाए गए अनुसार बुलाए जाने पर, यदि अनुमति दी जाती है तो यह एक टोकन लौटाता है या इनकार किए जाने पर वादे को अस्वीकार करता है:
function requestPermission() { console.log('Requesting permission...'); Notification.requestPermission().then((permission) => { if (permission === 'granted') { console.log('Notification permission granted.');
FCM को एक firebase-messaging-sw.js
फ़ाइल की आवश्यकता होती है। जब तक आपके पास पहले से कोई firebase-messaging-sw.js
फ़ाइल न हो, उस नाम से एक खाली फ़ाइल बनाएं और टोकन प्राप्त करने से पहले इसे अपने डोमेन के रूट में रखें। आप बाद में क्लाइंट सेटअप प्रक्रिया में फ़ाइल में अर्थपूर्ण सामग्री जोड़ सकते हैं।
वर्तमान टोकन पुनः प्राप्त करने के लिए:
Web version 9
import { getMessaging, getToken } from "firebase/messaging"; // Get registration token. Initially this makes a network call, once retrieved // subsequent calls to getToken will return from cache. const messaging = getMessaging(); getToken(messaging, { vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => { if (currentToken) { // Send the token to your server and update the UI if necessary // ... } else { // Show permission request UI console.log('No registration token available. Request permission to generate one.'); // ... } }).catch((err) => { console.log('An error occurred while retrieving token. ', err); // ... });
Web version 8
// Get registration token. Initially this makes a network call, once retrieved // subsequent calls to getToken will return from cache. messaging.getToken({ vapidKey: '<YOUR_PUBLIC_VAPID_KEY_HERE>' }).then((currentToken) => { if (currentToken) { // Send the token to your server and update the UI if necessary // ... } else { // Show permission request UI console.log('No registration token available. Request permission to generate one.'); // ... } }).catch((err) => { console.log('An error occurred while retrieving token. ', err); // ... });
आपके द्वारा टोकन प्राप्त करने के बाद, इसे अपने ऐप सर्वर पर भेजें और अपनी पसंदीदा विधि का उपयोग करके इसे संगृहीत करें।
किसी विषय के क्लाइंट ऐप को सब्सक्राइब करें
आप किसी विषय से संबंधित उपकरणों की सदस्यता लेने के लिए फायरबेस एडमिन एसडीके सब्सक्रिप्शन विधि में पंजीकरण टोकन की एक सूची पास कर सकते हैं:
नोड.जेएस
// 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);
});
जावा
// 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");
अजगर
# 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")
सी#
// 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 API आपको उचित तरीके से पंजीकरण टोकन पास करके किसी विषय से उपकरणों की सदस्यता समाप्त करने की अनुमति भी देता है:
नोड.जेएस
// 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);
});
जावा
// 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");
अजगर
# 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")
सी#
// 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 API त्रुटियाँ देखें.
विषय संदेश प्राप्त करें और प्रबंधित करें
संदेशों का व्यवहार इस बात पर निर्भर करता है कि पृष्ठ अग्रभूमि में है (फोकस है), या पृष्ठभूमि में, अन्य टैब के पीछे छिपा हुआ है, या पूरी तरह से बंद है। सभी मामलों में पृष्ठ को onMessage
कॉलबैक को संभालना चाहिए, लेकिन पृष्ठभूमि के मामलों में आपको onBackgroundMessage
संभालने या उपयोगकर्ता को अपने वेब ऐप को अग्रभूमि में लाने की अनुमति देने के लिए प्रदर्शन सूचना को कॉन्फ़िगर करने की आवश्यकता हो सकती है।
ऐप राज्य | अधिसूचना | आंकड़े | दोनों |
---|---|---|---|
अग्रभूमि | onMessage | onMessage | onMessage |
पृष्ठभूमि (सेवा कार्यकर्ता) | onBackgroundMessage (प्रदर्शन अधिसूचना स्वचालित रूप से दिखाया गया है) | onBackgroundMessage | onBackgroundMessage (प्रदर्शन अधिसूचना स्वचालित रूप से दिखाया गया है) |
संदेशों को संभालें जब आपका वेब ऐप अग्रभूमि में हो
onMessage
ईवेंट प्राप्त करने के लिए, आपके ऐप को firebase-messaging-sw.js
में फायरबेस मैसेजिंग सर्विस वर्कर को परिभाषित करना होगा। वैकल्पिक रूप से, आप getToken(): Promise<string>
के माध्यम से SDK को एक मौजूदा सेवा कार्यकर्ता प्रदान कर सकते हैं।
Web version 9
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 version 8
// 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 version 9
// 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 version 8
// 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 version 9
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 version 8
// 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();
विकल्प सेट करने के लिए, onBackgroundMessage
firebase-messaging-sw.js
में कॉल करें। इस उदाहरण में, हम शीर्षक, बॉडी और आइकन फ़ील्ड के साथ एक सूचना बनाते हैं।
Web version 9
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 version 8
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); });
भेजें अनुरोध बनाएँ
आपके द्वारा एक विषय बनाने के बाद, या तो क्लाइंट साइड पर या सर्वर एपीआई के माध्यम से क्लाइंट ऐप इंस्टेंस की सदस्यता लेकर, आप विषय पर संदेश भेज सकते हैं। यदि आप पहली बार 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
अधिसूचना पेलोड में वेब पुश गुण जोड़ें
HTTP v1 API के साथ आप वेब सूचना API से किसी भी मान्य गुण वाले JSON ऑब्जेक्ट के रूप में अतिरिक्त सूचना विकल्प निर्दिष्ट कर सकते हैं। इस ऑब्जेक्ट में title
और body
फ़ील्ड, यदि मौजूद हैं, तो google.firebase.fcm.v1.Notification.title
और google.firebase.fcm.v1.Notification.body
फ़ील्ड को ओवरराइड करें।
HTTP पोस्ट अनुरोध
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...PbJ_uNasm
{
"message": {
"token" : <token of destination app>,
"notification": {
"title": "FCM Message",
"body": "This is a message from FCM"
},
"webpush": {
"headers": {
"Urgency": "high"
},
"notification": {
"body": "This is a message from FCM to web",
"requireInteraction": "true",
"badge": "/badge-icon.png"
}
}
}
}
इस अनुरोध के साथ, लक्षित वेब क्लाइंट (एंड्रॉइड पर चलने वाले समर्थित ब्राउज़र सहित) एक उच्च-प्राथमिकता वाला सूचना संदेश प्राप्त करते हैं जो तब तक सक्रिय रहता है जब तक कि उपयोगकर्ता इसके साथ इंटरैक्ट नहीं करता। इसमें फ़ील्ड शामिल हैं:
- शीर्षक: FCM संदेश
- मुख्य भाग: यह FCM की ओर से वेब के लिए एक संदेश है
- इंटरेक्शन की आवश्यकता: सच
- बिल्ला: /बैज-icon.png
Android और Apple नेटिव ऐप्स (जिन पर वेब ओवरराइड लागू नहीं होता) सामान्य-प्राथमिकता वाला सूचना संदेश प्राप्त करते हैं:
- शीर्षक: FCM संदेश
- मुख्य भाग: यह FCM का संदेश है
ध्यान दें कि वर्तमान में RequireInteraction
ब्राउज़रों के बीच केवल आंशिक समर्थन प्राप्त है। प्लेटफ़ॉर्म और ब्राउज़र समर्थन को सत्यापित करने के लिए डेवलपर्स को वेब अधिसूचना एपीआई विनिर्देश की जांच करनी चाहिए।
कर्ल
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...PbJ_uNasm" -H "Content-Type: application/json" -d '{
"message": {
"token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
"notification": {
"title": "FCM Message",
"body": "This is a message from FCM"
},
"webpush": {
"headers": {
"Urgency": "high"
},
"notification": {
"body": "This is a message from FCM to web",
"requireInteraction": "true",
"badge": "/badge-icon.png"
}
}
}
}' "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send"
HTTP प्रतिक्रिया
{
"name": "projects/myproject-b5ae1/messages/0:1500415314455276%31bd1c9631bd1c98"
}
FCM संदेशों के बारे में अधिक जानने के लिए ऐप सर्वर भेजें अनुरोध देखें।