একাধিক ডিভাইসে বার্তা পাঠান

একাধিক ডিভাইসে একটি বার্তা লক্ষ্য করতে, টপিক মেসেজিং ব্যবহার করুন। এই বৈশিষ্ট্যটি আপনাকে একটি নির্দিষ্ট বিষয় বেছে নেওয়া একাধিক ডিভাইসে একটি বার্তা পাঠাতে দেয়৷

এই টিউটোরিয়ালটি FCM-এর জন্য অ্যাডমিন SDK বা REST API ব্যবহার করে আপনার অ্যাপ সার্ভার থেকে বিষয়ের বার্তা পাঠানো এবং একটি ওয়েব অ্যাপে সেগুলি গ্রহণ ও পরিচালনা করার উপর ফোকাস করে। আমরা ব্যাকগ্রাউন্ডেড এবং ফোরগ্রাউন্ডেড উভয় অ্যাপের জন্য বার্তা হ্যান্ডলিং কভার করব।

SDK সেট আপ করুন

আপনি যদি FCM-এর জন্য একটি JavaScript ক্লায়েন্ট অ্যাপ সেট আপ করে থাকেন বা বার্তাগুলি গ্রহণ করার ধাপগুলির মাধ্যমে কাজ করে থাকেন তবে এই বিভাগে আপনি ইতিমধ্যে সম্পন্ন করা পদক্ষেপগুলি কভার করতে পারে৷

FCM SDK যোগ করুন এবং আরম্ভ করুন

  1. আপনি যদি ইতিমধ্যেই না করে থাকেন তাহলে Firebase JS SDK ইনস্টল করুন এবং Firebase শুরু করুন

  2. Firebase ক্লাউড মেসেজিং JS SDK যোগ করুন এবং Firebase ক্লাউড মেসেজিং শুরু করুন:

ওয়েব মডুলার API

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

ওয়েব নামস্থান API

import firebase from "firebase/compat/app";
import "firebase/compat/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 modular API

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 namespaced API

// 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);
  // ...
});

আপনি টোকেনটি পাওয়ার পরে, এটি আপনার অ্যাপ সার্ভারে পাঠান এবং আপনার পছন্দের পদ্ধতি ব্যবহার করে সংরক্ষণ করুন।

একটি বিষয় ক্লায়েন্ট অ্যাপ্লিকেশন সদস্যতা

আপনি একটি বিষয়ের সাথে সংশ্লিষ্ট ডিভাইসগুলি সাবস্ক্রাইব করতে 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);
  });

জাভা

// 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 আপনাকে উপযুক্ত পদ্ধতিতে নিবন্ধন টোকেন পাস করে একটি বিষয় থেকে ডিভাইসগুলি আনসাবস্ক্রাইব করার অনুমতি দেয়:

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

জাভা

// 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 এ Firebase মেসেজিং পরিষেবা কর্মীকে সংজ্ঞায়িত করতে হবে। বিকল্পভাবে, আপনি getToken(): Promise<string> মাধ্যমে SDK-কে একটি বিদ্যমান পরিষেবা কর্মী প্রদান করতে পারেন।

Web modular API

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 namespaced API

// 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 modular API

// 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 namespaced API

// 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 modular API

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 namespaced API

// 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.jsonBackgroundMessage এ কল করুন। এই উদাহরণে, আমরা শিরোনাম, বডি এবং আইকন ক্ষেত্র সহ একটি বিজ্ঞপ্তি তৈরি করি।

Web modular API

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 namespaced API

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- এর নির্দেশিকা দেখুন।

ব্যাকএন্ডে আপনার পাঠানো যুক্তিতে, দেখানো হিসাবে পছন্দসই বিষয়ের নাম উল্লেখ করুন:

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

জাভা

// 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

আপনি আপনার শর্তাধীন অভিব্যক্তিতে পাঁচটি বিষয় পর্যন্ত অন্তর্ভুক্ত করতে পারেন।

একটি শর্তে পাঠাতে:

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

জাভা

// 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

একটি বিজ্ঞপ্তি পেলোডে ওয়েব পুশ বৈশিষ্ট্য যোগ করুন

HTTP v1 API এর সাহায্যে আপনি ওয়েব বিজ্ঞপ্তি API থেকে যেকোনো বৈধ বৈশিষ্ট্য ধারণকারী JSON অবজেক্ট হিসাবে অতিরিক্ত বিজ্ঞপ্তি বিকল্পগুলি নির্দিষ্ট করতে পারেন। এই বস্তুর title এবং body ক্ষেত্রগুলি, যদি উপস্থিত থাকে, তাহলে সমতুল্য google.firebase.fcm.v1.Notification.title এবং google.firebase.fcm.v1.Notification.body ক্ষেত্রগুলিকে ওভাররাইড করে৷

HTTP POST অনুরোধ

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"
      }
    }
  }
}

এই অনুরোধের সাথে, লক্ষ্যযুক্ত ওয়েব ক্লায়েন্টরা (Android-এ চলমান সমর্থিত ব্রাউজারগুলি সহ) একটি উচ্চ-অগ্রাধিকার বিজ্ঞপ্তি বার্তা পায় যা ব্যবহারকারী এটির সাথে যোগাযোগ না করা পর্যন্ত সক্রিয় থাকে। এতে ক্ষেত্র রয়েছে:

  • শিরোনাম: FCM বার্তা
  • মূল অংশ: এটি FCM থেকে ওয়েবে একটি বার্তা৷
  • ইন্টারঅ্যাকশন প্রয়োজন: সত্য
  • ব্যাজ: /badge-icon.png

অ্যান্ড্রয়েড এবং অ্যাপল নেটিভ অ্যাপ্লিকেশানগুলি (যেগুলিতে ওয়েব ওভাররাইডগুলি প্রযোজ্য নয়) এর সাথে একটি সাধারণ-অগ্রাধিকার বিজ্ঞপ্তি বার্তা পায়:

  • শিরোনাম: FCM বার্তা
  • মূল অংশ: এটি FCM থেকে একটি বার্তা

উল্লেখ্য যে RequireInteraction বর্তমানে ব্রাউজারগুলির মধ্যে শুধুমাত্র আংশিক সমর্থন রয়েছে৷ প্ল্যাটফর্ম এবং ব্রাউজার সমর্থন যাচাই করতে বিকাশকারীদের ওয়েব বিজ্ঞপ্তি API স্পেসিফিকেশন পরীক্ষা করা উচিত।

cURL

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 বার্তাগুলি সম্পর্কে আরও জানতে বিল্ড অ্যাপ সার্ভার অনুরোধ পাঠান দেখুন৷