העברת הודעות בנושא באנדרואיד

בהתבסס על מודל הפרסום/הרשמה, העברת הודעות בנושא FCM מאפשרת לך לשלוח הודעה למספר מכשירים שהצטרפו לנושא מסוים. אתה מחבר הודעות נושא לפי הצורך, ו-FCM מטפל בניתוב ומסירת ההודעה בצורה מהימנה למכשירים הנכונים.

לדוגמה, משתמשים באפליקציית חיזוי גאות ושפל מקומית יכולים להצטרף לנושא "התראות זרמי גאות" ולקבל הודעות על תנאי דיג אופטימליים במים מלוחים באזורים מוגדרים. משתמשים באפליקציית ספורט יכולים להירשם לעדכונים אוטומטיים בתוצאות המשחקים החיים עבור הקבוצות המועדפות עליהם.

כמה דברים שכדאי לזכור לגבי נושאים:

  • העברת הודעות בנושא מתאימה ביותר לתוכן כגון מזג אוויר או מידע זמין לציבור אחר.
  • הודעות נושא מותאמות לתפוקה ולא לאחזור . למסירה מהירה ומאובטחת למכשירים בודדים או לקבוצות קטנות של מכשירים, מקד הודעות לאסימוני רישום , לא לנושאים.
  • אם אתה צריך לשלוח הודעות למספר מכשירים לכל משתמש , שקול לשלוח הודעות קבוצת מכשירים עבור מקרי שימוש אלה.
  • העברת הודעות נושא תומכת בהרשמות ללא הגבלה לכל נושא. עם זאת, FCM אוכפת מגבלות בתחומים הבאים:
    • ניתן להירשם למופע אחד של אפליקציה ללא יותר מ-2000 נושאים.
    • אם אתה משתמש בייבוא ​​אצווה כדי להירשם למופעי אפליקציה, כל בקשה מוגבלת ל-1000 מופעי אפליקציה.
    • תדירות המנויים החדשים מוגבלת בתעריף לפרויקט. אם תשלח יותר מדי בקשות מנוי בפרק זמן קצר, שרתי FCM יגיבו בתגובה של 429 RESOURCE_EXHAUSTED ("חריגה מהמכסה"). נסה שוב עם גיבוי אקספוננציאלי.

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

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

כדי להירשם לנושא, אפליקציית הלקוח קוראת ל-Firebase Cloud Messaging subscribeToTopic() עם שם הנושא של FCM. שיטה זו מחזירה Task , שבה יכול לשמש מאזין השלמה כדי לקבוע אם המנוי הצליח:

Kotlin+KTX

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() עם שם הנושא.

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

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");

פִּיתוֹן

# 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");

ממשק ה-API של Admin FCM מאפשר לך גם לבטל את הרישום של מכשירים לנושא על ידי העברת אסימוני רישום לשיטה המתאימה:

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");

פִּיתוֹן

# 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. לסוג ההחזרה אותו פורמט ללא קשר למספר אסימוני הרישום שצוינו בבקשה.

במקרה של שגיאה (כשלי אימות, אסימון או נושא לא חוקי וכו') שיטות אלו גורמות לשגיאה. לרשימה מלאה של קודי שגיאה, כולל תיאורים ושלבי פתרון, ראה Admin FCM API Errors .

קבלה וטיפול בהודעות בנושא

FCM מעביר הודעות נושא באותו אופן כמו הודעות אחרות במורד הזרם.

כדי לקבל הודעות, השתמש בשירות שמרחיב את FirebaseMessagingService . השירות שלך צריך לעקוף את ההתקשרות חוזרת של onMessageReceived ו- onDeletedMessages .

חלון הזמן לטיפול בהודעה עשוי להיות קצר מ-20 שניות, בהתאם לעיכובים שנגרמו לפני הקריאה onMessageReceived , כולל עיכובים של מערכת ההפעלה, זמן הפעלת האפליקציה, שהשרשור הראשי נחסם על ידי פעולות אחרות, או שיחות onMessageReceived קודמות שנמשכות זמן רב מדי. לאחר זמן זה, התנהגויות שונות של מערכת ההפעלה, כגון הרג תהליך של אנדרואיד או מגבלות ביצוע ברקע של Android O עלולות להפריע ליכולת שלך להשלים את עבודתך.

onMessageReceived מסופק עבור רוב סוגי ההודעות, למעט החריגים הבאים:

  • הודעות התראה נשלחות כאשר האפליקציה שלך ברקע . במקרה זה, ההודעה מועברת למגש המערכת של המכשיר. הקשה של משתמש על הודעה פותחת את משגר האפליקציות כברירת מחדל.

  • הודעות עם התראה וגם מטען נתונים, כשהן מתקבלות ברקע . במקרה זה, ההודעה נמסרת למגש המערכת של המכשיר, ומטען הנתונים נמסר בתוספות של כוונת פעילות המשגר ​​שלך.

לסיכום:

מצב האפליקציה הוֹדָעָה נתונים שניהם
חֲזִית onMessageReceived onMessageReceived onMessageReceived
רקע כללי מגש מערכת onMessageReceived הודעה: מגש מערכת
נתונים: בתוספות של הכוונה.
למידע נוסף על סוגי הודעות, ראה התראות והודעות נתונים .

ערוך את מניפסט האפליקציה

כדי להשתמש FirebaseMessagingService , עליך להוסיף את הדברים הבאים למניפסט האפליקציה שלך:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

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

הוסף שורות אלה בתוך תג application כדי להגדיר את סמל ברירת המחדל המותאם אישית ואת הצבע המותאם אישית:

<!-- Set custom default icon. This is used when no icon is set for incoming notification messages.
     See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />
<!-- Set color used with incoming notification messages. This is used when no color is set for the incoming
     notification message. See README(https://goo.gl/6BKBk7) for more. -->
<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

אנדרואיד מציג את סמל ברירת המחדל המותאם אישית עבור

  • כל הודעות ההתראות שנשלחו ממחבר ההתראות .
  • כל הודעת הודעה שאינה מגדירה במפורש את הסמל במטען ההודעות.

אנדרואיד משתמשת בצבע ברירת המחדל המותאם אישית עבור

  • כל הודעות ההתראות שנשלחו ממחבר ההתראות .
  • כל הודעת הודעה שאינה מגדירה במפורש את הצבע במטען ההודעות.

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

עוקף על onMessageReceived

על ידי ביטול השיטה FirebaseMessagingService.onMessageReceived , תוכל לבצע פעולות על סמך אובייקט RemoteMessage שהתקבל ולקבל את נתוני ההודעה:

Kotlin+KTX

override fun onMessageReceived(remoteMessage: RemoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: ${remoteMessage.from}")

    // Check if message contains a data payload.
    if (remoteMessage.data.isNotEmpty()) {
        Log.d(TAG, "Message data payload: ${remoteMessage.data}")

        // Check if data needs to be processed by long running job
        if (needsToBeScheduled()) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob()
        } else {
            // Handle message within 10 seconds
            handleNow()
        }
    }

    // Check if message contains a notification payload.
    remoteMessage.notification?.let {
        Log.d(TAG, "Message Notification Body: ${it.body}")
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());

        if (/* Check if data needs to be processed by long running job */ true) {
            // For long-running tasks (10 seconds or more) use WorkManager.
            scheduleJob();
        } else {
            // Handle message within 10 seconds
            handleNow();
        }

    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

עוקף על onDeletedMessages

במצבים מסוימים, ייתכן ש-FCM לא יעביר הודעה. זה מתרחש כאשר יש יותר מדי הודעות (>100) ממתינות עבור האפליקציה שלך במכשיר מסוים בזמן שהוא מתחבר או אם המכשיר לא התחבר ל-FCM במשך יותר מחודש אחד. במקרים אלה, ייתכן שתקבל התקשרות חוזרת אל FirebaseMessagingService.onDeletedMessages() כאשר מופע האפליקציה מקבל התקשרות חוזרת זו, עליו לבצע סנכרון מלא עם שרת האפליקציה שלך. אם לא שלחת הודעה לאפליקציה במכשיר זה במהלך 4 השבועות האחרונים, FCM לא יתקשר ל- onDeletedMessages() .

טפל בהודעות התראות באפליקציה ברקע

כאשר האפליקציה שלך ברקע, אנדרואיד מפנה הודעות התראה למגש המערכת. הקשה של משתמש על ההודעה פותחת את משגר האפליקציות כברירת מחדל.

זה כולל הודעות שמכילות הן הודעות והן עומס נתונים (וכל ההודעות שנשלחו ממסוף ההודעות). במקרים אלה, ההודעה נמסרת למגש המערכת של המכשיר, ומטען הנתונים נמסר בתוספות של כוונת פעילות המשגר ​​שלך.

לקבלת תובנות לגבי מסירת הודעות לאפליקציה שלך, ראה את לוח המחוונים לדיווח של FCM , שמתעד את מספר ההודעות שנשלחו ונפתחו במכשירי אפל ו-Android, יחד עם נתונים עבור "הופעות" (התראות שנראו על ידי משתמשים) עבור אפליקציות Android.

בנה בקשות לשלוח

לאחר שיצרת נושא, על ידי הרשמה למופעי אפליקציית לקוח לנושא בצד הלקוח או באמצעות ממשק API של השרת , תוכל לשלוח הודעות לנושא. אם זו הפעם הראשונה שאתה בונה בקשות שליחה עבור FCM, עיין במדריך לסביבת השרת שלך ול-FCM לקבלת מידע חשוב על רקע והגדרה.

בלוגיקת השליחה שלך בקצה העורפי, ציין את שם הנושא הרצוי כפי שמוצג:

Node.js

// The topic name can be optionally prefixed with "/topics/".
const topic = 'highScores';

const message = {
  data: {
    score: '850',
    time: '2:45'
  },
  topic: topic
};

// Send a message to devices subscribed to the provided topic.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Java

// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";

// See documentation on defining a message payload.
Message message = Message.builder()
    .putData("score", "850")
    .putData("time", "2:45")
    .setTopic(topic)
    .build();

// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

פִּיתוֹן

# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'

# See documentation on defining a message payload.
message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    topic=topic,
)

# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

ללכת

// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Topic: topic,
}

// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

C#

// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";

// See documentation on defining a message payload.
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        { "score", "850" },
        { "time", "2:45" },
    },
    Topic = topic,
};

// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

מנוחה

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
  "message":{
    "topic" : "foo-bar",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message"
      }
   }
}

פקודת cURL:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "message": {
    "topic" : "foo-bar",
    "notification": {
      "body": "This is a Firebase Cloud Messaging Topic Message!",
      "title": "FCM Message"
    }
  }
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

כדי לשלוח הודעה לשילוב של נושאים, ציין תנאי , שהוא ביטוי בוליאני המציין את נושאי היעד. לדוגמה, התנאי הבא ישלח הודעות למכשירים המנויים על TopicA ועל TopicB או TopicC :

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

FCM מעריך תחילה תנאים בסוגריים, ולאחר מכן מעריך את הביטוי משמאל לימין. בביטוי שלמעלה, משתמש שנרשם לאף נושא בודד אינו מקבל את ההודעה. כמו כן, משתמש שאינו מנוי ל- TopicA אינו מקבל את ההודעה. השילובים האלה אכן מקבלים את זה:

  • TopicA ונושא TopicB
  • TopicA ו- TopicC

אתה יכול לכלול עד חמישה נושאים בביטוי המותנה שלך.

כדי לשלוח לתנאי:

Node.js

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';

// See documentation on defining a message payload.
const message = {
  notification: {
    title: '$FooCorp up 1.43% on the day',
    body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
  },
  condition: condition
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
getMessaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });

Java

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
Message message = Message.builder()
    .setNotification(Notification.builder()
        .setTitle("$GOOG up 1.43% on the day")
        .setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
        .build())
    .setCondition(condition)
    .build();

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);

פִּיתוֹן

# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"

# See documentation on defining a message payload.
message = messaging.Message(
    notification=messaging.Notification(
        title='$GOOG up 1.43% on the day',
        body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
    ),
    condition=condition,
)

# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)

ללכת

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"

// See documentation on defining a message payload.
message := &messaging.Message{
	Data: map[string]string{
		"score": "850",
		"time":  "2:45",
	},
	Condition: condition,
}

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
	log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)

C#

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";

// See documentation on defining a message payload.
var message = new Message()
{
    Notification = new Notification()
    {
        Title = "$GOOG up 1.43% on the day",
        Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
    },
    Condition = condition,
};

// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);

מנוחה

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
   "message":{
    "condition": "'dogs' in topics || 'cats' in topics",
    "notification" : {
      "body" : "This is a Firebase Cloud Messaging Topic Message!",
      "title" : "FCM Message",
    }
  }
}

פקודת cURL:

curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
  "notification": {
    "title": "FCM Message",
    "body": "This is a Firebase Cloud Messaging Topic Message!",
  },
  "condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

הצעדים הבאים