คุณสามารถสมัครรับข้อมูลหัวข้อจากแอปไคลเอ็นต์ได้จากเซิร์ฟเวอร์หรือไคลเอ็นต์
ในเซิร์ฟเวอร์โดยใช้ 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");
เมธอด 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 และสมัครรับข้อมูลไคลเอ็นต์ในหัวข้อที่ระบุ โปรดตรวจสอบว่าอินสแตนซ์ของแอปไคลเอ็นต์ได้รับโทเค็นการลงทะเบียนผ่านการเรียกกลับ didReceiveRegistrationToken แล้วก่อนที่จะเรียก subscribeToTopic:topic
FCM จะตรวจสอบว่ามีการสมัครรับข้อมูลหัวข้อที่ขอทั้งหมดแล้วทุกครั้งที่แอปเริ่มต้น
FCM หากต้องการยกเลิกการสมัครรับข้อมูล ให้เรียก unsubscribeFromTopic:topic แล้ว FCM จะยกเลิกการสมัครรับข้อมูลหัวข้อในเบื้องหลัง
C++
หากต้องการสมัครรับข้อมูลหัวข้อ ให้เรียก ::firebase::messaging::Subscribe
จากแอปพลิเคชัน การเรียกนี้จะส่งคำขอแบบไม่พร้อมกันไปยังแบ็กเอนด์ของ FCM
และสมัครรับข้อมูลไคลเอ็นต์ในหัวข้อที่ระบุ
::firebase::messaging::Subscribe("example");
หากคำขอสมัครรับข้อมูลล้มเหลวในครั้งแรก FCM จะลองอีกครั้งจนกว่า จะสมัครรับข้อมูลหัวข้อได้สำเร็จ FCM จะตรวจสอบว่ามีการสมัครรับข้อมูลหัวข้อที่ขอทั้งหมดแล้วทุกครั้งที่แอปเริ่มต้น FCM
หากต้องการยกเลิกการสมัครรับข้อมูล ให้เรียก ::firebase::messaging::Unsubscribe,
แล้ว FCM จะยกเลิกการสมัครรับข้อมูลหัวข้อในเบื้องหลัง
Unity
หากต้องการสมัครรับข้อมูลหัวข้อ ให้เรียก
Firebase.Messaging.FirebaseMessaging.Subscribe
จากแอปพลิเคชัน การเรียกนี้จะส่งคำขอแบบไม่พร้อมกันไปยังแบ็กเอนด์ของ FCM
และสมัครรับข้อมูลไคลเอ็นต์ในหัวข้อที่ระบุ
Firebase.Messaging.FirebaseMessaging.Subscribe("/topics/example");
หากคำขอสมัครรับข้อมูลล้มเหลวในครั้งแรก FCM จะลองอีกครั้งจนกว่า จะสมัครรับข้อมูลหัวข้อได้สำเร็จ FCM จะตรวจสอบว่ามีการสมัครรับข้อมูลหัวข้อที่ขอทั้งหมดแล้วทุกครั้งที่แอปเริ่มต้น FCM
หากต้องการยกเลิกการสมัครรับข้อมูล ให้เรียก
Firebase.Messaging.FirebaseMessaging.Unsubscribe,
และ FCM จะยกเลิกการสมัครรับข้อมูลหัวข้อในเบื้องหลัง
การจัดการหัวข้อฝั่งเซิร์ฟเวอร์แบบเดิม (เลิกใช้งานแล้ว)
หากต้องการทำความเข้าใจว่า Instance ID คืออะไร โปรดไปที่ หน้า Instance ID ดูรายละเอียดเกี่ยวกับ ปลายทางที่เลิกใช้งานแล้วได้ที่ ข้อมูลอ้างอิง Instance ID API