অ্যান্ড্রয়েডে টপিক মেসেজিং

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

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

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

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

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

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

একটি বিষয়ের সদস্যতা নিতে, ক্লায়েন্ট অ্যাপটি FCM বিষয়ের নামের সাথে Firebase ক্লাউড মেসেজিং subscribeToTopic() কল করে। এই পদ্ধতিটি একটি Task ফেরত দেয়, যা সদস্যতা সফল হয়েছে কিনা তা নির্ধারণ করতে একটি সম্পূর্ণ শ্রোতা দ্বারা ব্যবহার করা যেতে পারে:

Kotlin+KTX

Firebase.messaging.subscribeToTopic("weather")
    .addOnCompleteListener { task ->
        var msg = "Subscribed"
        if (!task.isSuccessful) {
            msg = "Subscribe failed"
        }
        Log.d(TAG, msg)
        Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
    }

Java

FirebaseMessaging.getInstance().subscribeToTopic("weather")
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                String msg = "Subscribed";
                if (!task.isSuccessful()) {
                    msg = "Subscribe failed";
                }
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });

আনসাবস্ক্রাইব করতে, ক্লায়েন্ট অ্যাপটি ফায়ারবেস ক্লাউড মেসেজিং unsubscribeFromTopic() টপিকের নামের সাথে কল করে।

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

ফায়ারবেস অ্যাডমিন 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 অন্যান্য ডাউনস্ট্রিম বার্তাগুলির মতোই বিষয় বার্তাগুলি সরবরাহ করে৷

বার্তাগুলি পেতে, FirebaseMessagingService প্রসারিত করে এমন একটি পরিষেবা ব্যবহার করুন। আপনার পরিষেবা onMessageReceived এবং onDeletedMessages কলব্যাকগুলিকে ওভাররাইড করা উচিত৷

OS বিলম্ব, অ্যাপ স্টার্টআপের সময়, অন্যান্য ক্রিয়াকলাপ দ্বারা ব্লক করা মূল থ্রেড বা পূর্ববর্তী onMessageReceived কলগুলি খুব বেশি সময় নেওয়া সহ onMessageReceived এ কল করার আগে বিলম্বের উপর নির্ভর করে একটি বার্তা পরিচালনার জন্য সময় উইন্ডো 20 সেকেন্ডের কম হতে পারে। সেই সময়ের পরে, Android এর প্রক্রিয়া হত্যা বা Android O-এর ব্যাকগ্রাউন্ড এক্সিকিউশন সীমার মতো বিভিন্ন OS আচরণ আপনার কাজ সম্পূর্ণ করার ক্ষমতাতে হস্তক্ষেপ করতে পারে।

onMessageReceived নিম্নলিখিত ব্যতিক্রমগুলি সহ বেশিরভাগ বার্তা প্রকারের জন্য প্রদান করা হয়:

  • আপনার অ্যাপ ব্যাকগ্রাউন্ডে থাকলে বিজ্ঞপ্তি বার্তা পাঠানো হয় । এই ক্ষেত্রে, বিজ্ঞপ্তিটি ডিভাইসের সিস্টেম ট্রেতে বিতরণ করা হয়। একটি বিজ্ঞপ্তিতে ব্যবহারকারীর ট্যাপ ডিফল্টরূপে অ্যাপ লঞ্চার খোলে।

  • পটভূমিতে প্রাপ্ত হলে বিজ্ঞপ্তি এবং ডেটা পেলোড উভয় সহ বার্তা। এই ক্ষেত্রে, বিজ্ঞপ্তিটি ডিভাইসের সিস্টেম ট্রেতে বিতরণ করা হয় এবং ডেটা পেলোড আপনার লঞ্চার কার্যকলাপের অভিপ্রায়ের অতিরিক্ত হিসাবে বিতরণ করা হয়।

সংক্ষেপে:

অ্যাপের অবস্থা বিজ্ঞপ্তি ডেটা উভয়
ফোরগ্রাউন্ড onMessageReceived onMessageReceived onMessageReceived
পটভূমি সিস্টেম ট্রে onMessageReceived বিজ্ঞপ্তি: সিস্টেম ট্রে
তথ্য: অভিপ্রায় অতিরিক্ত.
বার্তার ধরন সম্পর্কে আরও তথ্যের জন্য, বিজ্ঞপ্তি এবং ডেটা বার্তা দেখুন।

অ্যাপ ম্যানিফেস্ট সম্পাদনা করুন

FirebaseMessagingService ব্যবহার করতে, আপনাকে আপনার অ্যাপ ম্যানিফেস্টে নিম্নলিখিত যোগ করতে হবে:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

এছাড়াও, বিজ্ঞপ্তিগুলির উপস্থিতি কাস্টমাইজ করতে আপনাকে ডিফল্ট মান সেট করার পরামর্শ দেওয়া হচ্ছে৷ আপনি একটি কাস্টম ডিফল্ট আইকন এবং একটি কাস্টম ডিফল্ট রঙ নির্দিষ্ট করতে পারেন যা যখনই বিজ্ঞপ্তি পেলোডে সমতুল্য মান সেট না করা হয় তখন প্রয়োগ করা হয়৷

কাস্টম ডিফল্ট আইকন এবং কাস্টম রঙ সেট করতে application ট্যাগের ভিতরে এই লাইনগুলি যোগ করুন:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

Android এর জন্য কাস্টম ডিফল্ট আইকন প্রদর্শন করে

  • বিজ্ঞপ্তি কম্পোজার থেকে পাঠানো সমস্ত বিজ্ঞপ্তি বার্তা।
  • কোনো বিজ্ঞপ্তি বার্তা যা স্পষ্টভাবে বিজ্ঞপ্তি পেলোডে আইকন সেট করে না।

অ্যান্ড্রয়েড এর জন্য কাস্টম ডিফল্ট রঙ ব্যবহার করে

  • বিজ্ঞপ্তি কম্পোজার থেকে পাঠানো সমস্ত বিজ্ঞপ্তি বার্তা।
  • কোনো বিজ্ঞপ্তি বার্তা যা স্পষ্টভাবে বিজ্ঞপ্তি পেলোডে রঙ সেট করে না।

যদি কোনো কাস্টম ডিফল্ট আইকন সেট করা না থাকে এবং বিজ্ঞপ্তি পেলোডে কোনো আইকন সেট করা না থাকে, তাহলে Android সাদা রঙে রেন্ডার করা অ্যাপ্লিকেশন আইকন প্রদর্শন করে।

ওভাররাইড onMessageReceived

FirebaseMessagingService.onMessageReceived পদ্ধতিটি ওভাররাইড করে, আপনি প্রাপ্ত RemoteMessage অবজেক্টের উপর ভিত্তি করে ক্রিয়া সম্পাদন করতে পারেন এবং বার্তা ডেটা পেতে পারেন:

Kotlin+KTX

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: ${remoteMessage.from}")

    // Check if message contains a data payload.
    if (remoteMessage.data.isNotEmpty()) {
        Log.d(TAG, "Message data payload: ${remoteMessage.data}")

        // Check if data needs to be processed by long running job
        if (needsToBeScheduled()) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

onDeletedMessages ওভাররাইড করুন

কিছু পরিস্থিতিতে, FCM একটি বার্তা প্রদান করতে পারে না। এটি তখন ঘটে যখন একটি নির্দিষ্ট ডিভাইসে সংযোগের সময় আপনার অ্যাপের জন্য অনেকগুলি বার্তা (>100) মুলতুবি থাকে বা যদি ডিভাইসটি এক মাসের বেশি সময় ধরে FCM-এর সাথে সংযুক্ত না থাকে। এই ক্ষেত্রে, আপনি FirebaseMessagingService.onDeletedMessages() এ একটি কলব্যাক পেতে পারেন যখন অ্যাপ ইনস্ট্যান্স এই কলব্যাকটি পায়, তখন এটি আপনার অ্যাপ সার্ভারের সাথে একটি সম্পূর্ণ সিঙ্ক সঞ্চালন করবে। আপনি যদি গত 4 সপ্তাহের মধ্যে সেই ডিভাইসে অ্যাপটিতে একটি বার্তা না পাঠিয়ে থাকেন, তাহলে FCM onDeletedMessages() কল করবে না।

একটি ব্যাকগ্রাউন্ডেড অ্যাপে বিজ্ঞপ্তি বার্তা পরিচালনা করুন

যখন আপনার অ্যাপটি ব্যাকগ্রাউন্ডে থাকে, তখন Android সিস্টেম ট্রেতে বিজ্ঞপ্তি বার্তা পাঠায়। বিজ্ঞপ্তিতে একটি ব্যবহারকারীর ট্যাপ ডিফল্টরূপে অ্যাপ লঞ্চারটি খোলে।

এতে বিজ্ঞপ্তি এবং ডেটা পেলোড (এবং বিজ্ঞপ্তি কনসোল থেকে পাঠানো সমস্ত বার্তা) উভয়ই রয়েছে এমন বার্তাগুলি অন্তর্ভুক্ত করে৷ এই ক্ষেত্রে, বিজ্ঞপ্তিটি ডিভাইসের সিস্টেম ট্রেতে বিতরণ করা হয়, এবং ডেটা পেলোড আপনার লঞ্চার কার্যকলাপের অভিপ্রায়ের অতিরিক্ত হিসাবে বিতরণ করা হয়।

আপনার অ্যাপে বার্তা বিতরণের অন্তর্দৃষ্টির জন্য, এফসিএম রিপোর্টিং ড্যাশবোর্ড দেখুন, যা অ্যাপল এবং অ্যান্ড্রয়েড ডিভাইসে পাঠানো এবং খোলা বার্তাগুলির সংখ্যা রেকর্ড করে, সাথে Android অ্যাপগুলির জন্য "ইম্প্রেশন" (ব্যবহারকারীরা দেখেছে বিজ্ঞপ্তিগুলি) ডেটা সহ।

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

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

পরবর্তী পদক্ষেপ