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

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

  • בשרת, באמצעות 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);
  });

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')

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().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')

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 שגיאות.

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

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

ניהול נושאים מדור קודם בצד השרת (הוצא משימוש)

כדי להבין מהם מזהי מופעים, אפשר לעיין בדף בנושא מזהי מופעים. פרטים על נקודות הקצה שהוצאו משימוש מופיעים במאמר הפניות ל-API של מזהה מופע.