ניהול נושאים שביקשתי לקבל באימייל

אפשר לרשום אפליקציית לקוח לנושא מהשרת או מהלקוח:

ניהול מינויים לנושאים באמצעות 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

אם הפעולה בוצעה בהצלחה, תגובה מה-API של HTTP v1 היא אובייקט 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");

התוצאה של השיטות subscribeToTopic() ו-unsubscribeFromTopic() היא אובייקט שמכיל את התשובה מ-FCM. סוג ההחזרה הוא באותו פורמט, בלי קשר למספר הטוקנים לרישום שצוינו בבקשה.

אם יש שגיאה (כשלים באימות, אסימון או נושא לא חוקיים וכו'), השיטות האלה יגרמו לשגיאה. רשימה מלאה של קודי שגיאה, כולל תיאורים ושלבים לפתרון, מופיעה במאמר שגיאות ב-Firebase Admin SDK.

ניהול מינויים לנושאים מאפליקציית הלקוח

אפשר גם להירשם לנושאים או לבטל את ההרשמה אליהם ישירות מהאפליקציה דרך ערכות ה-SDK של Firebase. שימו לב: 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, וכל לקוח יכול להירשם אליו בהמשך.

כדי להירשם לנושא, צריך להפעיל את שיטת ההרשמה מה-thread הראשי של האפליקציה (FCM הוא לא thread-safe). אם בקשת המינוי נכשלת בהתחלה, FCM מנסה שוב באופן אוטומטי. במקרים שבהם לא ניתן להשלים את המינוי, המינוי מחזיר שגיאה שאפשר לתפוס ב-completion 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, צריך לוודא שהמופע של אפליקציית הלקוח כבר קיבל אסימון רישום באמצעות הקריאה החוזרת (callback) 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 או טוקן

כשהמזהה של התקנת Firebase‏ (FID) או הטוקן של FCM משתנים, המינויים לנושאים שמשויכים ל-FID או לטוקן הישנים לא מועברים, ולכן למזהה החדש אין מינוי לנושא. בדרך כלל, האסימון לא משתנה במהלך עדכוני אפליקציה או רענוני אסימונים. הסיבה הכי נפוצה לשינוי טוקן היא מחיקה או רוטציה של מזהה התקנה של Firebase (FID). אפשר לעקוב אחרי זה באמצעות מעקב אחרי מחזור החיים של מזהה התקנה של Firebase.

אם רוצים לשמור את המינויים לנושאים בשינויים של FID או של אסימון, צריך לגבות את נתוני המינויים לנושאים בלקוח או בקצה העורפי, ולרשום את המזהה החדש לנושאים שמופע האפליקציה היה רשום אליהם בעבר. ‫Firebase לא מספקת תכונות או יכולות אוטומטיות למעקב אחרי המינויים האלה או להעברתם. זו משימה שצריך להטמיע באופן ידני באפליקציה ובקצה העורפי.