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

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

  • सर्वर पर, FCM topic subscription server API या Firebase Admin SDK का इस्तेमाल करके.

  • क्लाइंट पर, अपने ऐप्लिकेशन में क्लाइंट-साइड एपीआई का इस्तेमाल करके.

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

किसी विषय के लिए, 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}

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

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

FCM V1 के Topics 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

कार्रवाई पूरी होने पर, HTTP v1 API का रिस्पॉन्स एक 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 subscription तरीके में रजिस्ट्रेशन टोकन की सूची पास करके, डिवाइसों को किसी विषय की सदस्यता के लिए रजिस्टर किया जा सकता है:

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 बैकग्राउंड में उस विषय की सदस्यता छोड़ देगा.

लेगसी सर्वर-साइड विषय मैनेजमेंट (बंद कर दिया गया है)

इंस्टेंस आईडी के बारे में जानने के लिए, इंस्टेंस आईडी पेज पर जाएं. बंद किए गए एंडपॉइंट के बारे में जानकारी के लिए, Instance ID API के रेफ़रंस देखें.

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

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

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