หากต้องการกำหนดเป้าหมายข้อความไปยังอุปกรณ์หลายเครื่อง ให้ใช้การส่งข้อความตามหัวข้อ ฟีเจอร์นี้ช่วยให้คุณส่งข้อความไปยังอุปกรณ์หลายเครื่องที่เลือกรับหัวข้อใดหัวข้อหนึ่งได้
บทแนะนำนี้จะเน้นที่การส่งข้อความหัวข้อจากเซิร์ฟเวอร์แอปโดยใช้ SDK ผู้ดูแลระบบหรือ REST API สำหรับ FCM รวมถึงการรับและจัดการในแอป Android เราจะพูดถึงการจัดการข้อความสำหรับทั้งแอปในเบื้องหลังและแอปที่ทำงานอยู่เบื้องหน้า ครอบคลุมทุกขั้นตอนในการบรรลุเป้าหมายนี้ ตั้งแต่การตั้งค่าไปจนถึงการยืนยัน
ตั้งค่า SDK
ส่วนนี้อาจพูดถึงขั้นตอนที่คุณได้ทำไปแล้วหากคุณตั้งค่าแอปไคลเอ็นต์ Android สำหรับ FCM หรือทำตามขั้นตอนการส่งข้อความแรก
ก่อนเริ่มต้น
ติดตั้งหรืออัปเดต Android Studio เป็นเวอร์ชันล่าสุด
ตรวจสอบว่าโปรเจ็กต์ของคุณเป็นไปตามข้อกำหนดเหล่านี้ (โปรดทราบว่าผลิตภัณฑ์บางอย่างอาจมีข้อกำหนดที่เข้มงวดกว่า)
- กำหนดเป้าหมาย API ระดับ 19 (KitKat) ขึ้นไป
- ใช้ Android 4.4 ขึ้นไป
- ใช้
Jetpack (AndroidX)
ซึ่งมีคุณสมบัติตรงตามข้อกำหนดของเวอร์ชันดังต่อไปนี้
com.android.tools.build:gradle
v7.3.0 ขึ้นไปcompileSdkVersion
28 ขึ้นไป
ตั้งค่าอุปกรณ์จริงหรือใช้โปรแกรมจำลองเพื่อเรียกใช้แอป
โปรดทราบว่า Firebase SDK ที่มีการพึ่งพาบริการ Google Play กำหนดให้อุปกรณ์หรือโปรแกรมจำลองต้องติดตั้งบริการ Google Playลงชื่อเข้าใช้ Firebase โดยใช้บัญชี Google
หากยังไม่มีโปรเจ็กต์ Android และต้องการลองใช้ผลิตภัณฑ์ Firebase คุณดาวน์โหลดตัวอย่างการเริ่มต้นอย่างรวดเร็วได้
สร้างโปรเจ็กต์ Firebase
ก่อนที่จะเพิ่ม Firebase ลงในแอป Android คุณต้องสร้างโปรเจ็กต์ Firebase เพื่อเชื่อมต่อกับแอป Android ก่อน โปรดไปที่ทำความเข้าใจโปรเจ็กต์ Firebase เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับโปรเจ็กต์ Firebase
ลงทะเบียนแอปกับ Firebase
หากต้องการใช้ Firebase ในแอป Android คุณต้องลงทะเบียนแอปกับโปรเจ็กต์ Firebase การลงทะเบียนแอปมักเรียกว่า "การเพิ่ม" แอปลงในโปรเจ็กต์
ไปที่คอนโซล Firebase
ตรงกลางหน้าภาพรวมโปรเจ็กต์ ให้คลิกไอคอน Android (
) หรือเพิ่มแอปเพื่อเปิดเวิร์กโฟลว์การตั้งค่าป้อนชื่อแพ็กเกจของแอปในช่องชื่อแพ็กเกจ Android
(ไม่บังคับ) ป้อนข้อมูลอื่นๆ ของแอป ได้แก่ ชื่อเล่นแอปและแก้ไขข้อบกพร่องการลงชื่อในใบรับรอง SHA-1
คลิกลงทะเบียนแอป
เพิ่มไฟล์การกำหนดค่า Firebase
ดาวน์โหลดแล้วเพิ่มไฟล์การกำหนดค่า Firebase Android (
) ลงในแอปgoogle-services.json คลิก Download google-services.json เพื่อรับไฟล์การกำหนดค่า Firebase Android
ย้ายไฟล์การกำหนดค่าไปไว้ในไดเรกทอรีรูทโมดูล (ระดับแอป) ของแอป
หากต้องการทำให้ Firebase SDK เข้าถึงค่าในไฟล์การกำหนดค่า
ได้ คุณต้องมีปลั๊กอิน Gradle สำหรับบริการของ Google (google-services.json google-services
)ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (
<project>/build.gradle.kts
หรือ<project>/build.gradle
) ให้เพิ่มปลั๊กอินบริการของ Google เป็นทรัพยากร DependencyKotlin
plugins { id("com.android.application") version "7.3.0" apply false // ... // Add the dependency for the Google services Gradle plugin id("com.google.gms.google-services") version "4.4.1" apply false }
Groovy
plugins { id 'com.android.application' version '7.3.0' apply false // ... // Add the dependency for the Google services Gradle plugin id 'com.google.gms.google-services' version '4.4.1' apply false }
ในโมดูล (ระดับแอป) ไฟล์ Gradle (โดยปกติจะเป็น
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) ให้เพิ่มปลั๊กอินบริการของ Google ดังนี้Kotlin
plugins { id("com.android.application") // Add the Google services Gradle plugin id("com.google.gms.google-services") // ... }
Groovy
plugins { id 'com.android.application' // Add the Google services Gradle plugin id 'com.google.gms.google-services' // ... }
เพิ่ม Firebase SDK ลงในแอป
ในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติจะเป็น
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) ให้เพิ่มทรัพยากร Dependency สำหรับไลบรารีการรับส่งข้อความในระบบคลาวด์ของ Firebase สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมเวอร์ชันไลบรารีเพื่อรับประสบการณ์การใช้งาน Firebase Cloud Messaging ที่ดีที่สุด เราขอแนะนำให้เปิดใช้ Google Analytics ในโปรเจ็กต์ Firebase และเพิ่ม Firebase SDK สำหรับ Google Analytics ลงในแอป
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.0.0")) // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-messaging") implementation("com.google.firebase:firebase-analytics") }
การใช้ Firebase Android BoM จะทำให้แอปใช้ไลบรารี Firebase Android เวอร์ชันที่เข้ากันได้เสมอ
(ทางเลือก) เพิ่มทรัพยากร Dependency ของไลบรารี Firebase โดยไม่ใช้ BoM
หากเลือกไม่ใช้ Firebase BoM คุณต้องระบุเวอร์ชันไลบรารี Firebase แต่ละเวอร์ชันในบรรทัดทรัพยากร Dependency
โปรดทราบว่าหากคุณใช้ไลบรารี Firebase หลายรายการในแอป เราขอแนะนำอย่างยิ่งให้ใช้ BoM เพื่อจัดการเวอร์ชันไลบรารี ซึ่งจะทำให้มั่นใจได้ว่าทุกเวอร์ชันจะใช้งานร่วมกันได้
dependencies { // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-messaging:24.0.0") implementation("com.google.firebase:firebase-analytics:22.0.0") }
ซิงค์โปรเจ็กต์ Android กับไฟล์ Gradle
สมัครใช้บริการแอปไคลเอ็นต์ตามหัวข้อ
แอปไคลเอ็นต์จะสมัครรับหัวข้อที่มีอยู่หรือสร้างหัวข้อใหม่ก็ได้ เมื่อแอปไคลเอ็นต์สมัครใช้บริการชื่อหัวข้อใหม่ (ชื่อหัวข้อใหม่ที่ยังไม่มีสำหรับโปรเจ็กต์ Firebase) ระบบจะสร้างหัวข้อใหม่ของชื่อนั้นใน FCM และไคลเอ็นต์จะสมัครใช้บริการดังกล่าวในภายหลังได้
หากต้องการสมัครรับข้อมูลหัวข้อ แอปไคลเอ็นต์จะเรียก Firebase Cloud Messaging
subscribeToTopic()
ด้วยชื่อหัวข้อ FCM เมธอดนี้แสดงผล Task
ซึ่ง Listener ที่ดำเนินการเสร็จสมบูรณ์สามารถใช้เพื่อระบุว่าการสมัครใช้บริการนั้นสำเร็จหรือไม่
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()
ด้วยชื่อหัวข้อ
รับและจัดการข้อความตามหัวข้อ
FCM ส่งข้อความตามหัวข้อในลักษณะเดียวกับข้อความปลายทางอื่นๆ
หากต้องการรับข้อความ โปรดใช้บริการที่ขยาย
FirebaseMessagingService
บริการของคุณควรลบล้างโค้ดเรียกกลับ onMessageReceived
และ onDeletedMessages
กรอบเวลาในการจัดการข้อความอาจสั้นกว่า 20 วินาที ทั้งนี้ขึ้นอยู่กับความล่าช้าที่เกิดขึ้นก่อนการโทร onMessageReceived
รวมถึงความล่าช้าของระบบปฏิบัติการ เวลาเริ่มต้นของแอป เทรดหลักที่ถูกบล็อกโดยการดำเนินการอื่นๆ หรือการโทร onMessageReceived
ครั้งก่อนใช้เวลานานเกินไป หลังจากนั้น ลักษณะการทำงานต่างๆ ของระบบปฏิบัติการ เช่น การทำลายกระบวนการของ Android หรือ
ขีดจำกัดการดำเนินการในเบื้องหลังของ Android O อาจรบกวนความสามารถในการทำงานให้เสร็จสิ้น
onMessageReceived
มีไว้สำหรับข้อความส่วนใหญ่ โดยมีข้อยกเว้นต่อไปนี้
-
ข้อความแจ้งเตือนที่ส่งเมื่อแอปทำงานในเบื้องหลัง ในกรณีนี้ การแจ้งเตือนจะส่งไปยังถาดระบบของอุปกรณ์ ผู้ใช้แตะการแจ้งเตือนจะเปิดตัวเปิดแอปโดยค่าเริ่มต้น
-
ข้อความที่มีทั้งการแจ้งเตือนและเพย์โหลดข้อมูล เมื่อได้รับข้อความในเบื้องหลัง ในกรณีนี้ การแจ้งเตือนจะส่งไปยังถาดระบบของอุปกรณ์ และเพย์โหลดข้อมูลจะส่งไปยังส่วนเพิ่มเติมของ Intent ของกิจกรรม Launcher
บทสรุปมีดังนี้:
สถานะแอป | การแจ้งเตือน | ข้อมูล | ทั้งสอง |
---|---|---|---|
พื้นหน้า | onMessageReceived |
onMessageReceived |
onMessageReceived |
ความเป็นมา | ถาดระบบ | onMessageReceived |
การแจ้งเตือน: ถาดระบบ ข้อมูล: ใน Intent เพิ่มเติม |
แก้ไขไฟล์ Manifest ของแอป
หากต้องการใช้ FirebaseMessagingService
คุณต้องเพิ่มค่าต่อไปนี้ในไฟล์ Manifest ของแอป
<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" />
Android จะแสดงไอคอนเริ่มต้นที่กำหนดเองสำหรับ
- ข้อความแจ้งเตือนทั้งหมดที่ส่งจาก การเขียนการแจ้งเตือน
- ข้อความแจ้งเตือนที่ไม่ได้ตั้งค่าไอคอนอย่างชัดเจนในเพย์โหลดการแจ้งเตือน
Android ใช้สีเริ่มต้นที่กำหนดเองสำหรับ
- ข้อความแจ้งเตือนทั้งหมดที่ส่งจาก การเขียนการแจ้งเตือน
- ข้อความแจ้งเตือนที่ไม่ได้ตั้งค่าสีในเพย์โหลดการแจ้งเตือนอย่างชัดเจน
หากไม่มีการตั้งค่าไอคอนเริ่มต้นที่กำหนดเองและไม่มีการตั้งค่าไอคอนในเพย์โหลดการแจ้งเตือน Android จะแสดงไอคอนแอปพลิเคชันที่แสดงผลเป็นสีขาว
ลบล้าง 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 เป็นเวลานานกว่า 1 เดือน ในกรณีเหล่านี้
คุณอาจได้รับการเรียกกลับไปยัง FirebaseMessagingService.onDeletedMessages()
เมื่ออินสแตนซ์ของแอปได้รับโค้ดเรียกกลับนี้
ควรมีการซิงค์เต็มรูปแบบกับเซิร์ฟเวอร์แอปของคุณ หากคุณไม่ได้ส่งข้อความไปยังแอปในอุปกรณ์นั้นภายใน 4 สัปดาห์ที่ผ่านมา FCM จะไม่โทรหา onDeletedMessages()
จัดการข้อความการแจ้งเตือนในแอปเบื้องหลัง
เมื่อแอปทำงานในเบื้องหลัง Android จะส่งข้อความแจ้งเตือนไปยังถาดระบบ ผู้ใช้แตะการแจ้งเตือนจะเปิดตัวเปิดแอปโดยค่าเริ่มต้น
ซึ่งจะรวมถึงข้อความที่มีทั้งเพย์โหลดการแจ้งเตือนและ ข้อมูล (และข้อความทั้งหมดที่ส่งจากคอนโซลการแจ้งเตือน) ในกรณีเหล่านี้ การแจ้งเตือนจะส่งไปยังถาดระบบของอุปกรณ์ และเพย์โหลดข้อมูลจะส่งไปยังส่วนเพิ่มเติมของ Intent ของกิจกรรม Launcher
สำหรับข้อมูลเชิงลึกเกี่ยวกับการส่งข้อความไปยังแอปของคุณ โปรดดูที่ แดชบอร์ดการรายงาน FCM ซึ่งจะบันทึกจำนวนข้อความที่ส่งและเปิดในอุปกรณ์ Apple และ 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);
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
ขั้นตอนถัดไป
- คุณสามารถใช้เซิร์ฟเวอร์ของคุณสมัครรับข้อมูลอินสแตนซ์ของแอปไคลเอ็นต์กับหัวข้อและดำเนินงานด้านการจัดการอื่นๆ ได้ โปรดดูหัวข้อจัดการการสมัครรับข้อมูลตามหัวข้อในเซิร์ฟเวอร์