প্রকাশ/সাবস্ক্রাইব মডেলের উপর ভিত্তি করে, FCM টপিক মেসেজিং আপনাকে একাধিক ডিভাইসে একটি বার্তা পাঠাতে দেয় যারা একটি নির্দিষ্ট টপিক বেছে নিয়েছে। আপনি প্রয়োজন অনুসারে টপিক মেসেজ রচনা করেন এবং FCM সঠিক ডিভাইসে নির্ভরযোগ্যভাবে বার্তাটি রাউটিং এবং বিতরণ পরিচালনা করে।
উদাহরণস্বরূপ, স্থানীয় জোয়ারের পূর্বাভাস অ্যাপের ব্যবহারকারীরা "জোয়ারের স্রোতের সতর্কতা" বিষয় বেছে নিতে পারেন এবং নির্দিষ্ট এলাকায় লবণাক্ত জলে মাছ ধরার সর্বোত্তম অবস্থার বিজ্ঞপ্তি পেতে পারেন। একটি স্পোর্টস অ্যাপের ব্যবহারকারীরা তাদের প্রিয় দলের জন্য লাইভ গেম স্কোরের স্বয়ংক্রিয় আপডেটের জন্য সাবস্ক্রাইব করতে পারেন।
বিষয়গুলি সম্পর্কে কিছু বিষয় মনে রাখা উচিত:
- আবহাওয়া, অথবা অন্যান্য সর্বজনীনভাবে উপলব্ধ তথ্যের মতো বিষয়বস্তুর জন্য বিষয়ভিত্তিক বার্তা সবচেয়ে উপযুক্ত।
- টপিক মেসেজগুলি ল্যাটেন্সির পরিবর্তে থ্রুপুটের জন্য অপ্টিমাইজ করা হয়। একক ডিভাইস বা ডিভাইসের ছোট গ্রুপে দ্রুত, নিরাপদ ডেলিভারির জন্য, টপিক নয়, রেজিস্ট্রেশন টোকেনগুলিতে বার্তাগুলিকে লক্ষ্য করুন ।
- যদি আপনার প্রতি ব্যবহারকারীর একাধিক ডিভাইসে বার্তা পাঠাতে হয়, তাহলে সেই ব্যবহারের ক্ষেত্রে ডিভাইস গ্রুপ মেসেজিং বিবেচনা করুন।
- টপিক মেসেজিং প্রতিটি টপিকের জন্য সীমাহীন সাবস্ক্রিপশন সমর্থন করে। তবে, FCM এই ক্ষেত্রগুলিতে সীমা আরোপ করে:
- একটি অ্যাপের ইনস্ট্যান্সে ২০০০টির বেশি বিষয় সাবস্ক্রাইব করা যাবে না।
- আপনি যদি অ্যাপ ইনস্ট্যান্স সাবস্ক্রাইব করার জন্য ব্যাচ ইম্পোর্ট ব্যবহার করেন, তাহলে প্রতিটি অনুরোধ ১০০০টি অ্যাপ ইনস্ট্যান্সের মধ্যে সীমাবদ্ধ।
- প্রতি প্রকল্পে নতুন সাবস্ক্রিপশনের ফ্রিকোয়েন্সি সীমিত হারে। যদি আপনি অল্প সময়ের মধ্যে অনেক বেশি সাবস্ক্রিপশন অনুরোধ পাঠান, তাহলে FCM সার্ভারগুলি
429 RESOURCE_EXHAUSTED("কোটা অতিক্রম করেছে") প্রতিক্রিয়া সহ প্রতিক্রিয়া জানাবে। সূচকীয় ব্যাকঅফ দিয়ে পুনরায় চেষ্টা করুন।
ক্লায়েন্ট অ্যাপটিকে একটি বিষয়ে সাবস্ক্রাইব করুন
আপনি Firebase Admin SDK সাবস্ক্রিপশন পদ্ধতিতে নিবন্ধন টোকেনের একটি তালিকা পাঠাতে পারেন যাতে সংশ্লিষ্ট ডিভাইসগুলিকে একটি বিষয়ে সাবস্ক্রাইব করা যায়:
নোড.জেএস
// 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 এপিআই আপনাকে উপযুক্ত পদ্ধতিতে নিবন্ধন টোকেন পাস করে কোনও বিষয় থেকে ডিভাইসগুলি আনসাবস্ক্রাইব করার অনুমতি দেয়:
নোড.জেএস
// 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 ত্রুটি দেখুন।
বিষয়ভিত্তিক বার্তা গ্রহণ এবং পরিচালনা করা
FCM অন্যান্য ডাউনস্ট্রিম বার্তাগুলির মতোই বিষয়ভিত্তিক বার্তা সরবরাহ করে। ক্লায়েন্টে বার্তাগুলি কীভাবে পরিচালনা করবেন তা ওয়েব পৃষ্ঠার অগ্রভাগ/পটভূমির অবস্থা এবং এই বিভাগে বর্ণিত অন্যান্য বিষয়গুলির উপর নির্ভর করে।
পৃষ্ঠাটি ফোরগ্রাউন্ডে আছে (ফোকাস আছে), নাকি ব্যাকগ্রাউন্ডে আছে, অন্য ট্যাবের পিছনে লুকানো আছে, নাকি সম্পূর্ণ বন্ধ আছে তার উপর নির্ভর করে বার্তাগুলির আচরণ ভিন্ন হয়। সব ক্ষেত্রেই পৃষ্ঠাটিকে onMessage কলব্যাক পরিচালনা করতে হবে, তবে ব্যাকগ্রাউন্ডের ক্ষেত্রে আপনাকে onBackgroundMessage পরিচালনা করতে হতে পারে অথবা ব্যবহারকারীকে আপনার ওয়েব অ্যাপটি ফোরগ্রাউন্ডে আনতে অনুমতি দেওয়ার জন্য ডিসপ্লে বিজ্ঞপ্তি কনফিগার করতে হতে পারে।
| অ্যাপের অবস্থা | বিজ্ঞপ্তি | উপাত্ত | উভয়ই |
|---|---|---|---|
| অগ্রভাগ | onMessage | onMessage | onMessage |
| পটভূমি (সেবা কর্মী) | onBackgroundMessage (প্রদর্শন বিজ্ঞপ্তি স্বয়ংক্রিয়ভাবে প্রদর্শিত হবে) | onBackgroundMessage | onBackgroundMessage (প্রদর্শন বিজ্ঞপ্তি স্বয়ংক্রিয়ভাবে প্রদর্শিত হবে) |
যখন আপনার ওয়েব অ্যাপটি ফোরগ্রাউন্ডে থাকে তখন বার্তাগুলি পরিচালনা করুন
onMessage ইভেন্টটি গ্রহণ করার জন্য, আপনার অ্যাপটিকে firebase-messaging-sw.js এ Firebase মেসেজিং সার্ভিস ওয়ার্কার নির্ধারণ করতে হবে। বিকল্পভাবে, আপনি getToken(): Promise<string> মাধ্যমে SDK-তে একটি বিদ্যমান সার্ভিস ওয়ার্কার সরবরাহ করতে পারেন।
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. // Replace 10.13.2 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.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. // Replace 10.13.2 with latest version of the Firebase JS SDK. importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-app-compat.js'); importScripts('https://www.gstatic.com/firebasejs/10.13.2/firebase-messaging-compat.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); });
পাঠানোর অনুরোধ তৈরি করুন
একটি বিষয় তৈরি করার পরে, ক্লায়েন্ট সাইডে বিষয়ের সাথে ক্লায়েন্ট অ্যাপ ইনস্ট্যান্স সাবস্ক্রাইব করে অথবা সার্ভার API এর মাধ্যমে, আপনি বিষয়টিতে বার্তা পাঠাতে পারেন। যদি এটি আপনার প্রথমবারের মতো 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 কমান্ড:
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 কমান্ড:
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
পরবর্তী পদক্ষেপ
- একাধিক ডিভাইসে পাঠানোর অন্য উপায় সম্পর্কে আরও জানুন — ডিভাইস গ্রুপ মেসেজিং