क्लाइंट ऐप्लिकेशन को किसी विषय की सदस्यता देने के लिए, सर्वर या क्लाइंट में से किसी एक का इस्तेमाल किया जा सकता है:
सर्वर पर, Firebase Admin SDK का इस्तेमाल करके.
क्लाइंट पर, अपने ऐप्लिकेशन में क्लाइंट-साइड एपीआई का इस्तेमाल करके.
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().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')
शुरू करें
// 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')
शुरू करें
// 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 के रेफ़रंस देखें.