ข้อความตามหัวข้อใน C++

การรับส่งข้อความหัวข้อ FCM ช่วยให้คุณสามารถส่งข้อความได้ ทั้งนี้ขึ้นอยู่กับรูปแบบการเผยแพร่/ติดตาม สำหรับอุปกรณ์หลายเครื่องที่เลือกเข้าร่วมหัวข้อหนึ่งๆ คุณเขียนข้อความหัวข้อในฐานะ และ FCM จะจัดการเรื่องการกำหนดเส้นทางและการส่งข้อความไปทางด้านขวามือ อุปกรณ์

ตัวอย่างเช่น ผู้ใช้ข่าวในท้องถิ่น แอปการคาดการณ์สามารถเลือกรับ "การแจ้งเตือนกระแสน้ำขึ้น-น้ำลง" หัวข้อและรับ การแจ้งเตือนสภาวะการประมงน้ำเค็มที่เหมาะสมในพื้นที่ที่ระบุ ผู้ใช้แอปกีฬา สามารถสมัครรับการอัปเดตอัตโนมัติสำหรับคะแนนการแข่งขันสดสำหรับ ทีม

สิ่งที่ควรทราบเกี่ยวกับหัวข้อมีดังนี้

  • ข้อความตามหัวข้อเหมาะสมที่สุดสำหรับเนื้อหา เช่น สภาพอากาศ หรือเนื้อหาอื่นๆ แบบสาธารณะ ที่มีอยู่
  • ข้อความหัวข้อจะได้รับการเพิ่มประสิทธิภาพเพื่ออัตราการส่งข้อมูลมากกว่าเวลาในการตอบสนอง เพื่อการจัดส่งที่รวดเร็วและปลอดภัย อุปกรณ์เดียวหรือกลุ่มเล็กๆ ข้อความเป้าหมายไปยังโทเค็นการลงทะเบียน ไม่ใช่หัวข้อ
  • หากต้องการส่งข้อความไปยังอุปกรณ์หลายเครื่องต่อผู้ใช้ โปรดพิจารณา การรับส่งข้อความกลุ่มของอุปกรณ์ สำหรับกรณีการใช้งานเหล่านั้น
  • การรับส่งข้อความตามหัวข้อรองรับการสมัครใช้บริการแบบไม่จำกัดสำหรับแต่ละหัวข้อ อย่างไรก็ตาม FCM บังคับใช้ขีดจำกัดในด้านต่อไปนี้
    • อินสแตนซ์ของแอป 1 รายการจะสมัครรับข้อมูลได้ไม่เกิน 2,000 หัวข้อ
    • หากคุณกำลังใช้ การนำเข้าเป็นกลุ่ม เพื่อสมัครใช้บริการอินสแตนซ์แอป คำขอแต่ละรายการจะจำกัดอินสแตนซ์แอปไว้ที่ 1, 000 รายการ
    • ระบบจะจำกัดอัตราความถี่ของการสมัครใช้บริการใหม่ต่อโปรเจ็กต์ หากส่งอีเมลหลายรายการเกินไป คำขอสมัครสมาชิกในช่วงเวลาสั้นๆ เซิร์ฟเวอร์ FCM จะตอบสนองด้วย การตอบกลับ 429 RESOURCE_EXHAUSTED ("เกินโควต้า") ลองอีกครั้งโดยใช้ Exponential Backoff

สมัครรับข้อมูลหัวข้อในแอปไคลเอ็นต์

หากต้องการสมัครรับข้อมูลหัวข้อ โปรดโทร ::firebase::messaging::Subscribe จากแอปพลิเคชันของคุณ การดำเนินการนี้จะส่งคำขอแบบไม่พร้อมกันไปยัง FCM แบ็กเอนด์ และสมัครรับข้อมูลไคลเอ็นต์สำหรับหัวข้อที่กำหนด

::firebase::messaging::Subscribe("example");

หากคำขอสมัครสมาชิกล้มเหลวในครั้งแรก FCM จะดำเนินการใหม่จนถึง จะสามารถสมัครรับข้อมูล หัวข้อนั้นได้สำเร็จ ทุกครั้งที่แอปเริ่มทำงาน FCM จะตรวจสอบว่าหัวข้อที่ขอทั้งหมดได้สมัครรับข้อมูลแล้ว

หากต้องการยกเลิกการสมัคร โปรดโทรไปที่ ::firebase::messaging::Unsubscribe และ FCM จะยกเลิกการสมัคร เกี่ยวกับหัวข้อนั้นในเบื้องหลัง

จัดการการสมัครรับข้อมูลตามหัวข้อบนเซิร์ฟเวอร์

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

Admin FCM API ยังให้คุณยกเลิกการสมัครอุปกรณ์จากหัวข้อได้ด้วย โดยส่งโทเค็นการลงทะเบียนให้กับ วิธีการ:

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

เมธอด subscribeToTopic() และ unsubscribeFromTopic() จะทําให้เกิด ที่มีการตอบสนองจาก FCM ประเภทการแสดงผล โดยไม่คำนึงถึงจำนวนของโทเค็นการลงทะเบียนที่ระบุไว้ใน อีกครั้ง

ในกรณีที่เกิดข้อผิดพลาด (การตรวจสอบสิทธิ์ล้มเหลว โทเค็นหรือหัวข้อไม่ถูกต้อง ฯลฯ) วิธีการเหล่านี้จะทำให้เกิดข้อผิดพลาด ดูรายการรหัสข้อผิดพลาดทั้งหมดรวมถึงคำอธิบาย และขั้นตอนการแก้ปัญหาได้ที่ ข้อผิดพลาดของ Admin FCM API

รับและจัดการข้อความตามหัวข้อ

FCM ส่งข้อความหัวข้อในลักษณะเดียวกับที่ปลายทางอื่นๆ ข้อความ

ลบล้างเมธอด ::firebase::messaging::Listener::OnMessage, คุณสามารถดำเนินการตามข้อความที่ได้รับและรับข้อมูลข้อความได้ ดังนี้

void OnMessage(const ::firebase::messaging::Message& message) {
  LogMessage(TAG, "From: %s", message.from.c_str());
  LogMessage(TAG, "Message ID: %s", message.message_id.c_str());
}

สร้างคำขอส่งคำขอ

หลังจากสร้างหัวข้อแล้ว ไม่ว่าจะด้วยการสมัครใช้บริการอินสแตนซ์แอปไคลเอ็นต์ หัวข้อในฝั่งไคลเอ็นต์หรือผ่าน 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);

Python

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

Go

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

REST

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

หากต้องการส่งข้อความไปยังกลุ่มหัวข้อ ระบุ condition ซึ่งเป็นนิพจน์บูลีนที่ระบุพารามิเตอร์ หัวข้อเป้าหมาย ตัวอย่างเช่น เงื่อนไขต่อไปนี้จะส่งข้อความถึง อุปกรณ์ที่สมัครใช้บริการ TopicA และ TopicB หรือ TopicC

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

FCM จะประเมินเงื่อนไขใดๆ ในวงเล็บก่อน จากนั้นจึงประเมิน นิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้น ผู้ใช้สมัครรับข้อมูล หัวข้อใดหัวข้อหนึ่งไม่ได้รับข้อความ ในทำนองเดียวกัน ผู้ใช้ที่ไม่ การติดตาม TopicA ไม่ได้รับข้อความ การผสมผสานเหล่านี้ รับเลย

  • TopicA และ TopicB
  • TopicA และ TopicC

คุณสามารถรวมหัวข้อไว้ในนิพจน์แบบมีเงื่อนไขได้สูงสุด 5 หัวข้อ

วิธีส่งไปยังเงื่อนไข

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

Python

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

Go

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

REST

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