विषय की सदस्यताएं मैनेज करना

किसी क्लाइंट ऐप्लिकेशन को सर्वर या क्लाइंट से किसी विषय की सदस्यता दिलाई जा सकती है:

विषय की सदस्यता से जुड़े सर्वर एपीआई का इस्तेमाल करके, विषय की सदस्यताएं मैनेज करना

किसी विषय के लिए FCM रजिस्ट्रेशन की सदस्यता लेने के लिए, रजिस्ट्रेशन आईडी (Firebase इंस्टॉलेशन आईडी या FCM रजिस्ट्रेशन टोकन) को FCM विषय की सदस्यता वाले सर्वर एपीआई को पास करें, जैसा कि दिखाया गया है.

REST

POST https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions?topic_name=${TOPIC_NAME}

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{}

curl निर्देश:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{}'
https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions?topic_name=${TOPIC_NAME}

सफलता मिलने पर, एचटीटीपी v1 एपीआई से मिली जानकारी एक JSON ऑब्जेक्ट होती है. इसमें संसाधन का नाम, विषय का नाम, और टाइमस्टैंप शामिल होता है.

    {
      "topic_name": "${TOPIC_NAME}",
      "create_time": "2026-01-15T01:30:15.01Z"
    }

FCM V1 topics subscriptions API की मदद से, DELETE अनुरोध का इस्तेमाल करके किसी विषय से टोकन की सदस्यता छोड़ी जा सकती है:

REST

DELETE https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions/${TOPIC_NAME}

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{}

curl कमांड:

curl -X DELETE -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{}'
https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions/${TOPIC_NAME}

GET अनुरोध का इस्तेमाल करके, किसी रजिस्ट्रेशन आईडी के लिए विषय की सदस्यताओं की सूची भी देखी जा सकती है:

REST

GET https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

curl निर्देश:

curl -X GET -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json"
https://fcm.googleapis.com/v1/projects/${PROJECT_ID}/registrations/${REGISTRATION_ID}/topicSubscriptions

सफलता मिलने पर, एचटीटीपी v1 एपीआई से मिली जानकारी एक JSON ऑब्जेक्ट होती है. इसमें विषय की सदस्यताओं की सूची होती है

    {
        "topic_subscriptions": [
          {
              "topic_name": "${TOPIC_NAME1}",
              "create_time": "2026-01-15T01:30:15.01Z"
          },
          {
              "topic_name": "${TOPIC_NAME2}",
              "create_time": "2026-01-16T02:40:17.01Z"
          },
        ...
        ]
    }

Admin SDK का इस्तेमाल करके, विषय की सदस्यताएं मैनेज करना

Firebase Admin SDK की मदद से, सर्वर साइड से विषय को मैनेज करने के बुनियादी टास्क किए जा सकते हैं. रजिस्ट्रेशन टोकन की मदद से, सर्वर लॉजिक का इस्तेमाल करके क्लाइंट ऐप्लिकेशन के इंस्टेंस की सदस्यता एक साथ ली जा सकती है और छोड़ी जा सकती है.

क्लाइंट ऐप्लिकेशन के इंस्टेंस को किसी भी मौजूदा विषय की सदस्यता दी जा सकती है या नया विषय बनाया जा सकता है. जब किसी क्लाइंट ऐप्लिकेशन को नए विषय (ऐसा विषय जो आपके Firebase प्रोजेक्ट के लिए पहले से मौजूद नहीं है) की सदस्यता दिलाने के लिए एपीआई का इस्तेमाल किया जाता है, तो FCM में उस नाम का एक नया विषय बन जाता है. इसके बाद, कोई भी क्लाइंट उसकी सदस्यता ले सकता है.

किसी विषय की सदस्यता लेने के लिए, रजिस्ट्रेशन टोकन की सूची को Firebase Admin 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);
  });

Java

// 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().subscribeToTopicAsync(
    registrationTokens, topic).get();
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were subscribed successfully");

Python

# 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")

C#

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

Firebase Admin SDK की मदद से, किसी विषय से डिवाइसों की सदस्यता भी छोड़ी जा सकती है. इसके लिए, सही तरीके से रजिस्ट्रेशन टोकन पास करें:

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

Java

// 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().unsubscribeFromTopicAsync(
    registrationTokens, topic).get();
// See the TopicManagementResponse reference documentation
// for the contents of response.
System.out.println(response.getSuccessCount() + " tokens were unsubscribed successfully");

Python

# 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")

C#

// 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 से मिला जवाब शामिल होता है. अनुरोध में रजिस्ट्रेशन टोकन की संख्या चाहे जो भी हो, रिटर्न टाइप का फ़ॉर्मैट एक जैसा होता है.

किसी गड़बड़ी (पुष्टि न हो पाना, अमान्य टोकन या विषय वगैरह) के मामले में, इन तरीकों से गड़बड़ी का मैसेज मिलता है. गड़बड़ी के कोड की पूरी सूची देखने के लिए, Firebase Admin SDK गड़बड़ियां देखें. इसमें गड़बड़ियों की जानकारी और उन्हें ठीक करने का तरीका भी बताया गया है.

अपने क्लाइंट ऐप्लिकेशन से विषय की सदस्यताएं मैनेज करना

क्लाइंट ऐप्लिकेशन इंस्टेंस, Firebase SDK टूल की मदद से सीधे आपके ऐप्लिकेशन से विषयों की सदस्यता ले सकते हैं या उनकी सदस्यता छोड़ सकते हैं. ध्यान दें कि सदस्यता लेने की प्रोसेस पूरी हो जाए, इसके लिए FCM शुरुआती गड़बड़ियों के मामले में फिर से कोशिश करता है.

अपना प्लैटफ़ॉर्म चुनें:

Android

क्लाइंट ऐप्लिकेशन, किसी भी मौजूदा विषय की सदस्यता ले सकते हैं या नया विषय बना सकते हैं. जब कोई क्लाइंट ऐप्लिकेशन किसी नए विषय के नाम की सदस्यता लेता है (ऐसा नाम जो आपके Firebase प्रोजेक्ट के लिए पहले से मौजूद नहीं है), तो FCM में उस नाम का एक नया विषय बनाया जाता है. इसके बाद, कोई भी क्लाइंट उसकी सदस्यता ले सकता है.

किसी विषय की सदस्यता लेने के लिए, क्लाइंट ऐप्लिकेशन Firebase Cloud Messaging subscribeToTopic() को FCM विषय के नाम के साथ कॉल करता है. यह तरीका Task दिखाता है. इसका इस्तेमाल, पूरा होने की जानकारी देने वाला लिसनर यह तय करने के लिए कर सकता है कि सदस्यता ली गई है या नहीं:

Kotlin

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

सदस्यता छोड़ने के लिए, क्लाइंट ऐप्लिकेशन विषय के नाम के साथ Firebase Cloud Messaging unsubscribeFromTopic() को कॉल करता है.

iOS

क्लाइंट ऐप्लिकेशन, किसी भी मौजूदा विषय की सदस्यता ले सकते हैं या नया विषय बना सकते हैं. जब कोई क्लाइंट ऐप्लिकेशन किसी नए विषय के नाम की सदस्यता लेता है (ऐसा नाम जो आपके Firebase प्रोजेक्ट के लिए पहले से मौजूद नहीं है), तो FCM में उस नाम का एक नया विषय बनाया जाता है. इसके बाद, कोई भी क्लाइंट उसकी सदस्यता ले सकता है.

किसी विषय की सदस्यता लेने के लिए, अपने ऐप्लिकेशन के मुख्य थ्रेड से सदस्यता लेने का तरीका कॉल करें (FCM थ्रेड-सुरक्षित नहीं है). अगर सदस्यता का अनुरोध पहली बार में पूरा नहीं हो पाता है, तो FCM अपने-आप फिर से कोशिश करता है. अगर सदस्यता पूरी नहीं की जा सकती, तो सदस्यता में गड़बड़ी का मैसेज दिखता है. इसे पूरा करने वाले हैंडलर में देखा जा सकता है, जैसा कि यहां दिखाया गया है:

Swift

Messaging.messaging().subscribe(toTopic: "weather") { error in
  print("Subscribed to weather topic")
}

Objective-C

[[FIRMessaging messaging] subscribeToTopic:@"weather"
                                completion:^(NSError * _Nullable error) {
  NSLog(@"Subscribed to weather topic");
}];

यह कॉल, FCM बैकएंड को एसिंक्रोनस रिक्वेस्ट भेजता है और क्लाइंट को दिए गए विषय की सदस्यता देता है. subscribeToTopic:topic को कॉल करने से पहले, पक्का करें कि क्लाइंट ऐप्लिकेशन इंस्टेंस को कॉलबैक didReceiveRegistrationToken के ज़रिए रजिस्ट्रेशन टोकन मिल गया हो.

जब भी ऐप्लिकेशन शुरू होता है, FCM यह पक्का करता है कि अनुरोध किए गए सभी विषयों की सदस्यता ली गई हो. सदस्यता छोड़ने के लिए, unsubscribeFromTopic:topic को कॉल करें. इसके बाद, FCM बैकग्राउंड में विषय की सदस्यता छोड़ देता है.

C++

किसी विषय की सदस्यता लेने के लिए, अपने ऐप्लिकेशन से ::firebase::messaging::Subscribe को कॉल करें. यह FCM बैकएंड को एसिंक्रोनस रिक्वेस्ट भेजता है और क्लाइंट को दिए गए विषय की सदस्यता देता है.

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

अगर सदस्यता का अनुरोध शुरू में पूरा नहीं हो पाता है, तो FCM तब तक कोशिश करता है, जब तक कि वह विषय की सदस्यता नहीं ले लेता. जब भी ऐप्लिकेशन शुरू होता है, FCM यह पक्का करता है कि अनुरोध किए गए सभी विषयों की सदस्यता ली गई हो.

सदस्यता छोड़ने के लिए, ::firebase::messaging::Unsubscribe को कॉल करें. इसके बाद, FCM बैकग्राउंड में उस विषय की सदस्यता छोड़ देता है.

Unity

किसी विषय की सदस्यता लेने के लिए, अपने ऐप्लिकेशन से Firebase.Messaging.FirebaseMessaging.Subscribe को कॉल करें. यह FCM बैकएंड को एसिंक्रोनस रिक्वेस्ट भेजता है और क्लाइंट को दिए गए विषय की सदस्यता देता है.

Firebase.Messaging.FirebaseMessaging.Subscribe("/topics/example");

अगर सदस्यता का अनुरोध शुरू में पूरा नहीं हो पाता है, तो FCM तब तक कोशिश करता है, जब तक कि वह विषय की सदस्यता नहीं ले लेता. जब भी ऐप्लिकेशन शुरू होता है, FCM यह पक्का करता है कि अनुरोध किए गए सभी विषयों की सदस्यता ली गई हो.

सदस्यता छोड़ने के लिए, Firebase.Messaging.FirebaseMessaging.Unsubscribe को कॉल करें. इसके बाद, FCM बैकग्राउंड में विषय की सदस्यता छोड़ देता है.

लेगसी सर्वर-साइड टॉपिक मैनेजमेंट (अब काम नहीं करता)

इंस्टेंस आईडी क्या होते हैं, यह जानने के लिए इंस्टेंस आईडी पेज पर जाएं. अब काम न करने वाले एंडपॉइंट के बारे में ज़्यादा जानने के लिए, इंस्टेंस आईडी एपीआई के संदर्भ देखें.

एफ़आईडी या टोकन में बदलाव होने पर, विषय की सदस्यताएं मैनेज करना

जब किसी ऐप्लिकेशन का एफ़आईडी (Firebase इंस्टॉलेशन आईडी) या FCM टोकन बदलता है, तो पुराने एफ़आईडी या टोकन से जुड़ी टॉपिक की सदस्यताएं माइग्रेट नहीं होती हैं. इस वजह से, नए आइडेंटिफ़ायर के पास टॉपिक की कोई सदस्यता नहीं होती है. आम तौर पर, ऐप्लिकेशन के अपडेट या टोकन रीफ़्रेश होने के दौरान, टोकन नहीं बदलता. टोकन में बदलाव होने की सबसे आम वजह, एफ़आईडी को मिटाना/बदलना है. इसे Firebase इंस्टॉलेशन आईडी की लाइफ़साइकल को मॉनिटर करें पर जाकर मॉनिटर किया जा सकता है.

अगर आपको एफ़आईडी या टोकन में बदलाव होने पर भी, विषय की सदस्यताओं को बनाए रखना है, तो आपको क्लाइंट या बैकएंड पर विषय की सदस्यता के डेटा का बैक अप लेना होगा. साथ ही, नए आइडेंटिफ़ायर को उन विषयों की सदस्यता लेनी होगी जिनकी सदस्यता ऐप्लिकेशन इंस्टेंस ने पहले ली थी. Firebase, इन सदस्यताओं को ट्रैक या माइग्रेट करने के लिए, अपने-आप काम करने वाली कोई सुविधा या क्षमता उपलब्ध नहीं कराता. यह एक ऐसा टास्क है जिसे आपको अपने ऐप्लिकेशन और बैकएंड में मैन्युअल तरीके से लागू करना होगा.