คุณสามารถสมัครใช้บริการแอปไคลเอ็นต์กับหัวข้อได้จากเซิร์ฟเวอร์หรือไคลเอ็นต์ โดยทำดังนี้
ในเซิร์ฟเวอร์ ให้ใช้ FCM Topic Subscription Server API หรือ Firebase Admin SDK
ในไคลเอ็นต์ ให้ใช้ Client-Side API ภายในแอป
จัดการการสมัครใช้บริการหัวข้อโดยใช้ Topic Subscription Server API
หากต้องการสมัครใช้บริการการลงทะเบียน FCM กับหัวข้อ ให้ส่งรหัสการลงทะเบียน (Firebase รหัสการติดตั้ง หรือโทเค็นการลงทะเบียน FCM) ไปยัง FCM Topic Subscription Server API ดังที่แสดง
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 ยังช่วยให้คุณยกเลิกการสมัครใช้บริการโทเค็นจากa หัวข้อผ่านคำขอ 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"
},
...
]
}
จัดการการสมัครใช้บริการหัวข้อโดยใช้ 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().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ข้อผิดพลาด
จัดการการสมัครใช้บริการหัวข้อจากแอปไคลเอ็นต์
นอกจากนี้ คุณยังสมัครใช้บริการหรือยกเลิกการสมัครใช้บริการอินสแตนซ์ของแอปไคลเอ็นต์กับหัวข้อได้โดยตรงจากแอปผ่าน Firebase SDK โปรดทราบว่า 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 และไคลเอ็นต์ใดก็ได้จะสมัครใช้บริการหัวข้อนั้นได้ในภายหลัง
หากต้องการสมัครใช้บริการหัวข้อ ให้เรียกใช้เมธอดการสมัครใช้บริการ จากเทรดหลักของแอปพลิเคชัน (FCM ไม่ปลอดภัยต่อเทรด) หากคำขอสมัครใช้บริการล้มเหลวในครั้งแรก FCM จะลองอีกครั้งโดยอัตโนมัติ ในกรณีที่สมัครใช้บริการไม่สำเร็จ การสมัครใช้บริการจะแสดงข้อผิดพลาดที่คุณสามารถดักจับได้ในตัวแฮนเดิลอร์การดำเนินการเสร็จสมบูรณ์ ดังที่แสดง
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 คืออะไร โปรดไปที่ หน้า Instance ID ดูรายละเอียดเกี่ยวกับ ปลายทางที่เลิกใช้งานแล้วได้ที่ ข้อมูลอ้างอิง Instance ID API
จัดการการสมัครใช้บริการหัวข้อเมื่อมีการเปลี่ยนแปลง FID หรือโทเค็น
เมื่อ FID (รหัสการติดตั้ง Firebase) หรือ FCM โทเค็นของแอปมีการเปลี่ยนแปลง ระบบจะไม่ย้ายข้อมูลการสมัครใช้บริการหัวข้อที่เชื่อมโยงกับ FID หรือโทเค็นเดิม ดังนั้นตัวระบุใหม่จึงไม่มีการสมัครใช้บริการหัวข้อ โดยทั่วไป โทเค็นจะไม่เปลี่ยนแปลงระหว่างการอัปเดตแอปหรือการรีเฟรชโทเค็น สาเหตุที่พบบ่อยที่สุดที่ทำให้โทเค็นเปลี่ยนแปลงคือการลบ/หมุนเวียน FID ซึ่งคุณสามารถตรวจสอบได้โดยทำตามหัวข้อ ตรวจสอบวงจรชีวิตของรหัสการติดตั้ง Firebase
หากต้องการเก็บข้อมูลการสมัครใช้บริการหัวข้อไว้เมื่อมีการเปลี่ยนแปลง FID หรือโทเค็น คุณต้องสำรองข้อมูลการสมัครใช้บริการหัวข้อในไคลเอ็นต์หรือแบ็กเอนด์ แล้วสมัครใช้บริการตัวระบุใหม่กับหัวข้อที่อินสแตนซ์ของแอปเคยสมัครใช้บริการไว้ Firebase ไม่มีฟีเจอร์หรือความสามารถอัตโนมัติในการติดตามหรือย้ายข้อมูลการสมัครใช้บริการเหล่านี้ คุณจึงต้องใช้ฟีเจอร์นี้ด้วยตนเองในแอปและแบ็กเอนด์