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

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

  • सर्वर पर, Firebase Admin SDK का इस्तेमाल करके.

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

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

The 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().subscribeToTopic(
    registrationTokens, topic);
// 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')

Go

// 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().unsubscribeFromTopic(
    registrationTokens, topic);
// 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')

Go

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