คุณสามารถสมัครใช้บริการแอปไคลเอ็นต์กับหัวข้อได้จากเซิร์ฟเวอร์หรือไคลเอ็นต์ โดยทำดังนี้
ในเซิร์ฟเวอร์ ให้ใช้ Firebase Admin SDK
ในไคลเอ็นต์ ให้ใช้ Client-Side 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 และสมัครใช้บริการหัวข้อที่ระบุในไคลเอ็นต์ ก่อนเรียก 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