می توانید یک برنامه مشتری را در یک موضوع از سرور یا مشتری مشترک کنید:
در سرور، با استفاده از Firebase Admin SDK .
در سرویس گیرنده، با استفاده از API سمت سرویس گیرنده در برنامه خود.
اشتراکهای موضوع را با استفاده از Admin SDK مدیریت کنید
Firebase Admin SDK به شما این امکان را می دهد که وظایف مدیریت موضوع اصلی را از سمت سرور انجام دهید. با توجه به نشانه(های) ثبت نام آنها، می توانید با استفاده از منطق سرور، نمونه های برنامه مشتری را به صورت انبوه مشترک و لغو اشتراک کنید.
میتوانید نمونههای برنامه مشتری را در هر موضوع موجود مشترک کنید، یا میتوانید موضوع جدیدی ایجاد کنید. هنگامی که از API برای اشتراک برنامه مشتری در یک موضوع جدید استفاده می کنید (موضوعی که قبلاً برای پروژه 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);
});
جاوا
// 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");
پایتون
# 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")
سی شارپ
// 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);
});
جاوا
// 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");
پایتون
# 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")
سی شارپ
// 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 در صورت شکست اولیه برای اطمینان از موفقیت آمیز بودن اشتراک دوباره تلاش می کند.
پلتفرم خود را انتخاب کنید:
اندروید
برنامه های مشتری می توانند در هر موضوع موجود مشترک شوند یا می توانند موضوع جدیدی ایجاد کنند. هنگامی که یک برنامه مشتری در نام موضوع جدیدی مشترک می شود (نامی که قبلاً برای پروژه 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 به طور خودکار دوباره تلاش می کند. برای مواردی که اشتراک نمیتواند تکمیل شود، اشتراک خطایی ایجاد میکند که میتوانید در یک کنترلکننده تکمیل، همانطور که نشان داده شده است، آن را دریافت کنید:
سویفت
Messaging.messaging().subscribe(toTopic: "weather") { error in print("Subscribed to weather topic") }
هدف-C
[[FIRMessaging messaging] subscribeToTopic:@"weather" completion:^(NSError * _Nullable error) { NSLog(@"Subscribed to weather topic"); }];
این تماس یک درخواست ناهمزمان به باطن FCM می دهد و مشتری را در موضوع داده شده مشترک می کند. قبل از فراخوانی subscribeToTopic:topic
، مطمئن شوید که نمونه برنامه مشتری قبلاً یک نشانه ثبت نام از طریق callback didReceiveRegistrationToken
دریافت کرده است.
هر بار که برنامه شروع می شود، FCM مطمئن می شود که همه موضوعات درخواستی مشترک شده اند. برای لغو اشتراک، با unsubscribeFromTopic:topic
تماس بگیرید و FCM اشتراک موضوع را در پسزمینه لغو میکند.
C++
برای اشتراک در یک موضوع، با ::firebase::messaging::Subscribe
در برنامه خود تماس بگیرید. این یک درخواست ناهمزمان به باطن FCM می دهد و مشتری را در موضوع داده شده مشترک می کند.
::firebase::messaging::Subscribe("example");
اگر درخواست اشتراک در ابتدا با شکست مواجه شد، FCM دوباره تلاش میکند تا زمانی که بتواند با موفقیت در موضوع مشترک شود. هر بار که برنامه شروع می شود، FCM مطمئن می شود که همه موضوعات درخواستی مشترک شده اند.
برای لغو اشتراک، با ::firebase::messaging::Unsubscribe
و FCM اشتراک موضوع را در پسزمینه لغو میکند.
وحدت
برای اشتراک در یک موضوع، با Firebase.Messaging.FirebaseMessaging.Subscribe
تماس بگیرید. از برنامه خود مشترک شوید. این یک درخواست ناهمزمان به باطن FCM می دهد و مشتری را در موضوع داده شده مشترک می کند.
Firebase.Messaging.FirebaseMessaging.Subscribe("/topics/example");
اگر درخواست اشتراک در ابتدا با شکست مواجه شد، FCM دوباره تلاش میکند تا زمانی که بتواند با موفقیت در موضوع مشترک شود. هر بار که برنامه شروع می شود، FCM مطمئن می شود که همه موضوعات درخواستی مشترک شده اند.
برای لغو اشتراک، با Firebase.Messaging.FirebaseMessaging.Unsubscribe
تماس بگیرید و FCM اشتراک موضوع را در پسزمینه لغو میکند.
مدیریت موضوع سمت سرور قدیمی (منسوخ شده)
برای درک اینکه شناسههای نمونه چیست، از صفحه شناسه نمونه دیدن کنید. برای جزئیات بیشتر در مورد نقاط پایانی منسوخ شده، به References API ID Instance مراجعه کنید.