אפשר לרשום אפליקציית לקוח לנושא מהשרת או מהלקוח:
בשרת, באמצעות FCM topic subscription server API או Firebase Admin SDK.
בצד הלקוח, באמצעות ה-API בצד הלקוח באפליקציה.
ניהול מינויים לנושאים באמצעות topic subscription server API
כדי לרשום את הרישום ב-FCM לנושא, מעבירים את מזהה הרישום (מזהה ההתקנה של Firebase או אסימון הרישום של FCM) אל ה-API של שרת הרישום לנושא ב-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 subscriptions 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"
},
...
]
}
ניהול מינויים לנושאים באמצעות 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);
});
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')
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().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')
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");
השימוש ב-method subscribeToTopic() וב-method unsubscribeFromTopic() יוצר אובייקט שמכיל את התשובה מ-FCM. סוג ההחזרה הוא באותו פורמט, ללא קשר למספר אסימוני הרישום שצוינו בבקשה.
במקרה של שגיאה (כשלים באימות, טוקן או נושא לא תקינים וכו') השיטות האלה יחזירו שגיאה. רשימה מלאה של קודי השגיאה, כולל תיאורים ושלבים לפתרון הבעיה, מופיעה במאמר Firebase Admin SDK שגיאות.
ניהול מינויים לנושאים מאפליקציית הלקוח
בנוסף, מופעים של אפליקציות לקוח יכולים להירשם לנושאים או לבטל את ההרשמה שלהם לנושאים ישירות מהאפליקציה באמצעות Firebase SDKs. שימו לב: FCM מתבצעים ניסיונות חוזרים במקרה של כשלים ראשוניים כדי לוודא שהמינוי יצליח.
בוחרים את הפלטפורמה:
Android
אפליקציות לקוח יכולות להירשם לנושא קיים או ליצור נושא חדש. כשלקוח של אפליקציה נרשם לנושא חדש (שעדיין לא קיים בפרויקט Firebase), נוצר נושא חדש בשם הזה ב-FCM, וכל לקוח יכול להירשם אליו בהמשך.
כדי להירשם למינוי לנושא, אפליקציית הלקוח קוראת ל-Firebase Cloud Messaging
subscribeToTopic() עם שם הנושא FCM. השיטה הזו מחזירה Task, שאפשר להשתמש בו ב-listener של השלמת הפעולה כדי לקבוע אם המינוי הצליח:
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 מנסה שוב באופן אוטומטי. במקרים שבהם אי אפשר להשלים את המינוי, המינוי מחזיר שגיאה שאפשר לטפל בה באמצעות handler להשלמה, כמו שמוצג כאן:
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 References.
ניהול מינויים לנושאים בשינוי של FID או טוקן
כשמזהה ההתקנה (FID) של Firebase או FCMהטוקן של האפליקציה משתנים, המערכת לא מעבירה את המינויים לנושאים שמשויכים למזהה ההתקנה או לטוקן הישנים. כתוצאה מכך, למזהה החדש אין מינוי לנושא. בדרך כלל, האסימון לא משתנה במהלך עדכוני אפליקציה או רענון אסימונים. הסיבה הכי נפוצה לשינוי טוקן היא מחיקה או רוטציה של מזהה התקנה של Firebase (FID). אפשר לעקוב אחרי זה באמצעות מעקב אחרי מחזור החיים של מזהה התקנה של Firebase.
אם רוצים לשמור את המינויים לנושאים במקרה של שינויים ב-FID או בטוקן, צריך לגבות את נתוני המינויים לנושאים בלקוח או בקצה העורפי, ולרשום את המזהה החדש לנושאים שהמופע של האפליקציה היה רשום אליהם קודם. Firebase לא מספקת תכונות או יכולות אוטומטיות למעקב אחרי המינויים האלה או להעברתם. זו משימה שצריך להטמיע באופן ידני באפליקציה ובקצה העורפי.