C++ এ বিষয় বার্তা

প্রকাশ/সাবস্ক্রাইব মডেলের উপর ভিত্তি করে, FCM টপিক মেসেজিং আপনাকে একাধিক ডিভাইসে একটি বার্তা পাঠাতে দেয় যা একটি নির্দিষ্ট বিষয় বেছে নিয়েছে। আপনি প্রয়োজন অনুযায়ী বিষয়ের বার্তা রচনা করেন এবং FCM সঠিক ডিভাইসে নির্ভরযোগ্যভাবে বার্তাটি রাউটিং এবং বিতরণ পরিচালনা করে।

উদাহরণস্বরূপ, একটি স্থানীয় জোয়ারের পূর্বাভাস অ্যাপের ব্যবহারকারীরা একটি "জোয়ার স্রোত সতর্কতা" বিষয় বেছে নিতে পারে এবং নির্দিষ্ট এলাকায় সর্বোত্তম লবণাক্ত মাছ ধরার অবস্থার বিজ্ঞপ্তি পেতে পারে। একটি স্পোর্টস অ্যাপের ব্যবহারকারীরা তাদের প্রিয় দলের জন্য লাইভ গেম স্কোরের স্বয়ংক্রিয় আপডেটের সদস্যতা নিতে পারে।

টপিক সম্পর্কে কিছু জিনিস মনে রাখতে হবে:

  • আবহাওয়া বা অন্যান্য সর্বজনীনভাবে উপলব্ধ তথ্যের মতো বিষয়বস্তুর জন্য বিষয় বার্তা সবচেয়ে উপযুক্ত।
  • বিষয় বার্তাগুলি লেটেন্সির পরিবর্তে থ্রুপুটের জন্য অপ্টিমাইজ করা হয়৷ একক ডিভাইস বা ডিভাইসের ছোট গোষ্ঠীতে দ্রুত, নিরাপদ বিতরণের জন্য, নিবন্ধকরণ টোকেনগুলিতে লক্ষ্য বার্তাগুলি , বিষয় নয়।
  • আপনি যদি ব্যবহারকারী প্রতি একাধিক ডিভাইসে বার্তা পাঠাতে চান, তাহলে সেই ব্যবহারের ক্ষেত্রে ডিভাইস গ্রুপ মেসেজিং বিবেচনা করুন।
  • টপিক মেসেজিং প্রতিটি বিষয়ের জন্য সীমাহীন সদস্যতা সমর্থন করে। যাইহোক, FCM এই এলাকায় সীমা প্রয়োগ করে:
    • একটি অ্যাপ ইন্সট্যান্স 2000টির বেশি বিষয়ে সাবস্ক্রাইব করা যাবে না।
    • আপনি যদি অ্যাপ দৃষ্টান্ত সদস্যতা নিতে ব্যাচ আমদানি ব্যবহার করেন, প্রতিটি অনুরোধ 1000টি অ্যাপ দৃষ্টান্তের মধ্যে সীমাবদ্ধ।
    • নতুন সদস্যতার ফ্রিকোয়েন্সি প্রতি প্রকল্পের হার-সীমিত। আপনি যদি অল্প সময়ের মধ্যে অনেক বেশি সাবস্ক্রিপশন অনুরোধ পাঠান, তাহলে FCM সার্ভারগুলি 429 RESOURCE_EXHAUSTED ("কোটা অতিক্রম করেছে") প্রতিক্রিয়ার সাথে সাড়া দেবে৷ সূচকীয় ব্যাকঅফ দিয়ে পুনরায় চেষ্টা করুন।

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

একটি বিষয় সাবস্ক্রাইব করতে, কল করুন ::firebase::messaging::Subscribe । এটি FCM ব্যাকএন্ডে একটি অ্যাসিঙ্ক্রোনাস অনুরোধ করে এবং ক্লায়েন্টকে প্রদত্ত বিষয়ে সাবস্ক্রাইব করে।

::firebase::messaging::Subscribe("example");

যদি সাবস্ক্রিপশন অনুরোধ প্রাথমিকভাবে ব্যর্থ হয়, FCM পুনরায় চেষ্টা করে যতক্ষণ না এটি সফলভাবে বিষয়ের সদস্যতা নিতে পারে। প্রতিবার অ্যাপটি শুরু হলে, FCM নিশ্চিত করে যে সমস্ত অনুরোধ করা বিষয় সাবস্ক্রাইব করা হয়েছে।

আনসাবস্ক্রাইব করতে, কল করুন ::firebase::messaging::Unsubscribe , এবং FCM পটভূমিতে বিষয় থেকে আনসাবস্ক্রাইব করে।

সার্ভারে বিষয় সদস্যতা পরিচালনা করুন

ফায়ারবেস অ্যাডমিন SDK আপনাকে সার্ভারের দিক থেকে প্রাথমিক বিষয় পরিচালনার কাজগুলি সম্পাদন করতে দেয়৷ তাদের রেজিস্ট্রেশন টোকেন(গুলি) দেওয়া, আপনি সার্ভার লজিক ব্যবহার করে বাল্ক ক্লায়েন্ট অ্যাপ ইন্সট্যান্স সাবস্ক্রাইব এবং আনসাবস্ক্রাইব করতে পারেন।

আপনি যেকোন বিদ্যমান বিষয়ের ক্লায়েন্ট অ্যাপ ইনস্ট্যান্স সাবস্ক্রাইব করতে পারেন, অথবা আপনি একটি নতুন বিষয় তৈরি করতে পারেন। আপনি যখন একটি নতুন বিষয়ে একটি ক্লায়েন্ট অ্যাপ সাবস্ক্রাইব করার জন্য API ব্যবহার করেন (যেটি আপনার ফায়ারবেস প্রকল্পের জন্য ইতিমধ্যেই বিদ্যমান নেই), তখন সেই নামের একটি নতুন বিষয় FCM-এ তৈরি হয় এবং যেকোনো ক্লায়েন্ট পরবর্তীতে এটিতে সদস্যতা নিতে পারে।

আপনি একটি বিষয়ের সাথে সংশ্লিষ্ট ডিভাইসগুলি সাবস্ক্রাইব করতে Firebase অ্যাডমিন SDK সদস্যতা পদ্ধতিতে নিবন্ধন টোকেনগুলির একটি তালিকা পাঠাতে পারেন:

Node.js

// These registration tokens come from the client FCM SDKs.
const registrationTokens = [
  'YOUR_REGISTRATION_TOKEN_1',
  // ...
  'YOUR_REGISTRATION_TOKEN_n'
];

// Subscribe the devices corresponding to the registration tokens to the
// topic.
getMessaging().subscribeToTopic(registrationTokens, topic)
  .then((response) => {
    // See the MessagingTopicManagementResponse reference documentation
    // for the contents of response.
    console.log('Successfully subscribed to topic:', response);
  })
  .catch((error) => {
    console.log('Error subscribing to topic:', error);
  });

জাভা

// 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 ত্রুটিগুলি দেখুন৷

বিষয় বার্তা গ্রহণ এবং পরিচালনা

FCM অন্যান্য ডাউনস্ট্রিম বার্তাগুলির মতোই বিষয় বার্তাগুলি সরবরাহ করে৷

পদ্ধতিটি ওভাররাইড করে ::firebase::messaging::Listener::OnMessage , আপনি প্রাপ্ত বার্তার উপর ভিত্তি করে ক্রিয়া সম্পাদন করতে পারেন এবং বার্তা ডেটা পেতে পারেন:

void OnMessage(const ::firebase::messaging::Message& message) {
  LogMessage(TAG, "From: %s", message.from.c_str());
  LogMessage(TAG, "Message ID: %s", message.message_id.c_str());
}

অনুরোধ পাঠান তৈরি করুন

আপনি একটি বিষয় তৈরি করার পরে, হয় ক্লায়েন্ট সাইডের বিষয়ে বা সার্ভার 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