يمكنك اشتراك تطبيق عميل في موضوع من الخادم أو العميل:
على الخادم، باستخدام Firebase Admin SDK
على العميل، باستخدام واجهة برمجة التطبيقات من جهة العميل داخل تطبيقك
إدارة الاشتراكات في المواضيع باستخدام حزمة Admin SDK
تتيح لك Firebase Admin SDK تنفيذ مهام أساسية لإدارة المواضيع من جهة الخادم. باستخدام رموز التسجيل، يمكنك الاشتراك في مثيلات تطبيقات العميل وإلغاء الاشتراك فيها بشكل مجمّع باستخدام منطق الخادم.
يمكنك اشتراك مثيلات تطبيقات العميل في أي موضوع حالي، أو يمكنك إنشاء موضوع جديد. عند استخدام واجهة برمجة التطبيقات لاشتراك تطبيق عميل في موضوع جديد (لم يسبق أن كان موجودًا في مشروع Firebase)، يتم إنشاء موضوع جديد بهذا الاسم في مراسلة Firebase السحابية ويمكن لأي عميل الاشتراك فيه لاحقًا.
يمكنك تمرير قائمة برموز التسجيل إلى طريقة 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`، التي يمكن أن يستخدمها مستمع الإكمال لتحديد ما إذا نجح الاشتراك: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 الاشتراك في الموضوع في الخلفية.
إدارة المواضيع من جهة الخادم (قديمة ومتوقفة)
لفهم أرقام تعريف المثيل، يُرجى الانتقال إلى صفحة رقم تعريف المثيل. للاطّلاع على تفاصيل نقاط النهاية المتوقفة، يُرجى الرجوع إلى مراجع واجهة برمجة تطبيقات "رقم تعريف المثيل".