หากต้องการกำหนดเป้าหมายข้อความไปยังอุปกรณ์หลายเครื่อง ให้ใช้การรับส่งข้อความตามหัวข้อ ฟีเจอร์นี้ช่วยให้คุณส่งข้อความไปยังอุปกรณ์หลายเครื่องที่เลือกใช้หัวข้อหนึ่งๆ ได้
บทแนะนำนี้มุ่งเน้นที่การส่งข้อความหัวข้อจากเซิร์ฟเวอร์แอปโดยใช้ Admin SDK หรือ REST API สำหรับ FCM ตลอดจนการรับและจัดการข้อความในแอป Android เราจะกล่าวถึงการจัดการข้อความสำหรับทั้งแอปที่ทำงานอยู่เบื้องหลังและแอปที่ทำงานอยู่เบื้องหน้า ขั้นตอนทั้งหมดในการดำเนินการนี้ครอบคลุมตั้งแต่การตั้งค่าไปจนถึงการยืนยัน
ตั้งค่า SDK
ส่วนนี้อาจครอบคลุมขั้นตอนที่คุณทำไปแล้วหากคุณตั้งค่าแอปไคลเอ็นต์ Android สำหรับ FCM หรือทำตามขั้นตอนเพื่อส่งข้อความแรก
ก่อนเริ่มต้น
ติดตั้งหรืออัปเดต Android Studio เป็นเวอร์ชันล่าสุด
ตรวจสอบว่าโปรเจ็กต์ของคุณเป็นไปตามข้อกำหนดต่อไปนี้ (โปรดทราบว่าผลิตภัณฑ์บางรายการอาจมีข้อกำหนดที่เข้มงวดกว่า)
- กำหนดเป้าหมายเป็น API ระดับ 21 (Lollipop) ขึ้นไป
- ใช้ Android 5.0 ขึ้นไป
- ใช้ Jetpack (AndroidX) ซึ่งรวมถึงการปฏิบัติตามข้อกำหนดเวอร์ชันต่อไปนี้
com.android.tools.build:gradle
v7.3.0 ขึ้นไปcompileSdkVersion
28 ขึ้นไป
ตั้งค่าอุปกรณ์จริงหรือใช้โปรแกรมจำลองเพื่อเรียกใช้แอป
โปรดทราบว่า SDK ของ Firebase ที่ต้องอาศัยบริการ Google Play กำหนดให้อุปกรณ์หรือโปรแกรมจำลองต้องติดตั้งบริการ Google Playลงชื่อเข้าใช้ Firebase ด้วยบัญชี Google
หากยังไม่มีโปรเจ็กต์ Android และต้องการลองใช้ผลิตภัณฑ์ Firebase เพียงอย่างเดียว คุณสามารถดาวน์โหลดตัวอย่างการเริ่มต้นใช้งานอย่างรวดเร็วของเรา
สร้างโปรเจ็กต์ Firebase
คุณต้องสร้างโปรเจ็กต์ Firebase เพื่อเชื่อมต่อกับแอป Android ก่อนจึงจะเพิ่ม Firebase ในแอปได้ โปรดไปที่ทําความเข้าใจโปรเจ็กต์ Firebase เพื่อดูข้อมูลเพิ่มเติมเกี่ยวกับโปรเจ็กต์ Firebase
สร้างโปรเจ็กต์ Firebase
-
ในคอนโซล Firebase ให้คลิกเพิ่มโปรเจ็กต์
-
หากต้องการเพิ่มทรัพยากร Firebase ลงในโปรเจ็กต์ Google Cloud ที่มีอยู่ ให้ป้อนชื่อโปรเจ็กต์หรือเลือกจากเมนูแบบเลื่อนลง
-
หากต้องการสร้างโปรเจ็กต์ใหม่ ให้ป้อนชื่อโปรเจ็กต์ที่ต้องการ นอกจากนี้ คุณยังแก้ไขรหัสโปรเจ็กต์ที่แสดงใต้ชื่อโปรเจ็กต์ได้ด้วย
-
-
เมื่อได้รับข้อความแจ้ง ให้อ่านและยอมรับข้อกำหนดของ Firebase
-
คลิกต่อไป
-
(ไม่บังคับ) ตั้งค่า Google Analytics สําหรับโปรเจ็กต์ ซึ่งจะช่วยให้คุณได้รับประสบการณ์การใช้งานผลิตภัณฑ์ Firebase ต่อไปนี้อย่างเต็มประสิทธิภาพ
เลือกบัญชี Google Analytics ที่มีอยู่หรือสร้างบัญชีใหม่
หากคุณสร้างบัญชีใหม่ ให้เลือกตำแหน่งการรายงาน Analytics แล้วยอมรับการตั้งค่าการแชร์ข้อมูลและข้อกำหนด Google Analytics สำหรับโปรเจ็กต์
-
คลิกสร้างโปรเจ็กต์ (หรือเพิ่ม Firebase หากคุณใช้โปรเจ็กต์ Google Cloud ที่มีอยู่)
Firebase จะจัดสรรทรัพยากรสําหรับโปรเจ็กต์ Firebase โดยอัตโนมัติ เมื่อดำเนินการเสร็จแล้ว ระบบจะนำคุณไปยังหน้าภาพรวมของโปรเจ็กต์ Firebase ในคอนโซล Firebase
ลงทะเบียนแอปกับ Firebase
หากต้องการใช้ Firebase ในแอป Android คุณต้องลงทะเบียนแอปกับโปรเจ็กต์ Firebase การลงทะเบียนแอปมักเรียกว่า "การเพิ่ม" แอปลงในโปรเจ็กต์
ไปที่คอนโซล Firebase
ตรงกลางหน้าภาพรวมโปรเจ็กต์ ให้คลิกไอคอน Android (
) หรือเพิ่มแอปเพื่อเปิดเวิร์กโฟลว์การตั้งค่าป้อนชื่อแพ็กเกจของแอปในช่องชื่อแพ็กเกจ Android
ชื่อแพ็กเกจคืออะไรและจะดูได้จากที่ใด
ชื่อแพ็กเกจเป็นตัวระบุที่ไม่ซ้ำของแอปในอุปกรณ์และใน Google Play Store
ชื่อแพ็กเกจมักเรียกว่ารหัสแอปพลิเคชัน
ค้นหาชื่อแพ็กเกจของแอปในไฟล์ Gradle ของโมดูล (ระดับแอป) ซึ่งมักจะเป็น
app/build.gradle
(ตัวอย่างชื่อแพ็กเกจ:com.yourcompany.yourproject
)โปรดทราบว่าค่าชื่อแพ็กเกจจะคำนึงถึงตัวพิมพ์เล็กและตัวพิมพ์ใหญ่ และคุณจะเปลี่ยนแปลงค่าสำหรับแอป Firebase ของ Android นี้ไม่ได้หลังจากลงทะเบียนกับโปรเจ็กต์ Firebase แล้ว
(ไม่บังคับ) ป้อนข้อมูลอื่นๆ ของแอป ได้แก่ ชื่อเล่นของแอปและใบรับรองการแก้ไขข้อบกพร่อง SHA-1
ชื่อเล่นแอปและใบรับรองการลงนาม SHA-1 สำหรับการแก้ไขข้อบกพร่องใช้ใน Firebase อย่างไร
ชื่อเล่นของแอป: ตัวระบุภายในเพื่อความสะดวกที่คุณจะมองเห็นได้ในคอนโซล Firebase เท่านั้น
ใบรับรองการแก้ไขข้อบกพร่อง SHA-1: Firebase Authentication (เมื่อใช้Google Sign In หรือการลงชื่อเข้าใช้ด้วยหมายเลขโทรศัพท์) และ Firebase Dynamic Links กำหนดให้ต้องมีแฮช SHA-1
คลิกลงทะเบียนแอป
เพิ่มไฟล์การกําหนดค่า Firebase
ดาวน์โหลดแล้วเพิ่มไฟล์การกําหนดค่า Firebase สำหรับ Android (
) ลงในแอปgoogle-services.json คลิกดาวน์โหลด google-services.json เพื่อรับไฟล์การกําหนดค่า Firebase สำหรับ Android
ย้ายไฟล์การกําหนดค่าไปยังไดเรกทอรีรากโมดูล (ระดับแอป) ของแอป
สิ่งที่คุณจำเป็นต้องทราบเกี่ยวกับไฟล์การกําหนดค่านี้
ไฟล์การกําหนดค่า Firebase จะมีตัวระบุที่ไม่ซ้ำกันและไม่เป็นความลับสําหรับโปรเจ็กต์ ดูข้อมูลเพิ่มเติมเกี่ยวกับไฟล์การกําหนดค่านี้ได้ที่หัวข้อทําความเข้าใจโปรเจ็กต์ Firebase
คุณดาวน์โหลดไฟล์กำหนดค่า Firebase อีกครั้งได้ทุกเมื่อ
โปรดตรวจสอบว่าชื่อไฟล์การกําหนดค่าไม่มีอักขระต่อท้ายเกิน เช่น
(2)
หากต้องการให้ Firebase SDK เข้าถึงค่าใน
ไฟล์การกําหนดค่าได้ คุณต้องมีปลั๊กอิน Gradle ของบริการ Google (google-services.json google-services
)ในไฟล์ Gradle ระดับรูท (ระดับโปรเจ็กต์) (
<project>/build.gradle.kts
หรือ<project>/build.gradle
) ให้เพิ่มปลั๊กอินบริการของ Google เป็น Dependency ดังนี้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.2" apply false }
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.2' apply false }
ในไฟล์ Gradle ของโมดูล (ระดับแอป) (โดยปกติจะเป็น
<project>/<app-module>/build.gradle.kts
หรือ<project>/<app-module>/build.gradle
) ให้เพิ่มปลั๊กอินบริการของ Google ดังนี้plugins { id("com.android.application") // Add the Google services Gradle plugin id("com.google.gms.google-services") // ... }
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 Cloud Messaging สำหรับ Android เราขอแนะนำให้ใช้ Firebase Android BoM เพื่อควบคุมการกำหนดเวอร์ชันของไลบรารีเราขอแนะนําให้เปิดใช้ Google Analytics ในโปรเจ็กต์ Firebase และเพิ่ม Firebase SDK สําหรับ Google Analytics ลงในแอปเพื่อให้ได้รับประสบการณ์การใช้งาน Firebase Cloud Messaging ที่ดีที่สุด
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:33.9.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 เวอร์ชันที่เข้ากันได้อยู่เสมอ
หากกำลังมองหาโมดูลไลบรารีสำหรับ Kotlin โดยเฉพาะ ตั้งแต่เดือนตุลาคม 2023 (Firebase BoM 32.5.0) เป็นต้นไป นักพัฒนาซอฟต์แวร์ทั้ง Kotlin และ Java จะใช้โมดูลไลบรารีหลักได้ (ดูรายละเอียดได้ในคําถามที่พบบ่อยเกี่ยวกับโครงการริเริ่มนี้)(วิธีอื่น) เพิ่มไลบรารี 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.1.0") implementation("com.google.firebase:firebase-analytics:22.2.0") }
ซิงค์โปรเจ็กต์ Android กับไฟล์ Gradle
คุณพบปัญหาการสร้างไม่สำเร็จเกี่ยวกับการสนับสนุนการเรียกใช้แบบกำหนดเองและการเปิดใช้การกรองน้ำตาลออกหรือไม่ มาดูวิธีแก้ไขกัน
บิลด์ Gradle ที่ใช้ปลั๊กอิน Android Gradle (AGP) v4.2 หรือเวอร์ชันก่อนหน้าต้องเปิดใช้การรองรับ Java 8 ไม่เช่นนั้นโปรเจ็กต์ Android เหล่านี้จะสร้างไม่สําเร็จเมื่อเพิ่ม Firebase SDK
หากต้องการแก้ไขการสร้างที่ไม่สําเร็จนี้ ให้ทําตามตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้
- เพิ่ม
compileOptions
ที่ระบุจากข้อความแสดงข้อผิดพลาดลงในไฟล์build.gradle.kts
หรือbuild.gradle
ระดับแอป - เพิ่ม
minSdk
สำหรับโปรเจ็กต์ Android เป็น 26 ขึ้นไป
ดูข้อมูลเพิ่มเติมเกี่ยวกับการบิลด์ที่ล้มเหลวนี้ในคำถามที่พบบ่อยนี้
- เพิ่ม
สมัครใช้บริการแอปไคลเอ็นต์ในหัวข้อ
แอปไคลเอ็นต์สามารถสมัครรับหัวข้อที่มีอยู่หรือสร้างหัวข้อใหม่ก็ได้ เมื่อแอปไคลเอ็นต์ติดตามชื่อหัวข้อใหม่ (หัวข้อที่ไม่มีอยู่ในโปรเจ็กต์ Firebase ของคุณ) ระบบจะสร้างหัวข้อใหม่ชื่อนั้นใน FCM และไคลเอ็นต์ทุกรายจะติดตามหัวข้อดังกล่าวได้ในภายหลัง
หากต้องการสมัครรับข้อมูลหัวข้อ แอปไคลเอ็นต์จะเรียกใช้ Firebase Cloud Messaging
subscribeToTopic()
พร้อมชื่อหัวข้อ FCM เมธอดนี้จะแสดงผล Task
ซึ่ง Listener ที่ดำเนินการเสร็จสมบูรณ์สามารถใช้เพื่อระบุว่าการสมัครใช้บริการสําเร็จหรือไม่
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() }
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 ของกิจกรรมตัวเปิด
บทสรุปมีดังนี้:
สถานะแอป | การแจ้งเตือน | ข้อมูล | ทั้งสอง |
---|---|---|---|
พื้นหน้า | 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 ที่ได้รับและรับข้อมูลข้อความได้ ดังนี้
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. }
@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 ของกิจกรรมตัวเปิด
ดูข้อมูลเชิงลึกเกี่ยวกับการนำส่งข้อความไปยังแอปของคุณได้ที่ หน้าแดชบอร์ดการรายงาน FCM ซึ่งบันทึกจำนวนข้อความที่ส่งและเปิดในอุปกรณ์ Apple และ Android รวมถึงข้อมูล "การแสดงผล" (การแจ้งเตือนที่ผู้ใช้เห็น) สําหรับแอป Android
สร้างคำขอส่ง
หลังจากสร้างหัวข้อแล้ว คุณสามารถส่งข้อความไปยังหัวข้อได้ด้วยการสมัครใช้บริการอินสแตนซ์แอปไคลเอ็นต์เพื่อรับหัวข้อที่ฝั่งไคลเอ็นต์ หรือผ่าน server API หากนี่เป็นการสร้างคำขอส่งสำหรับ FCM เป็นครั้งแรก ให้ดูคำแนะนำเกี่ยวกับสภาพแวดล้อมเซิร์ฟเวอร์และ FCM เพื่อดูข้อมูลเบื้องต้นและข้อมูลการตั้งค่าที่สำคัญ
ในตรรกะการส่งในแบ็กเอนด์ ให้ระบุชื่อหัวข้อที่ต้องการดังที่แสดง
// 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);
});
// 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);
# 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)
// 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)
// 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);
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
หากต้องการส่งข้อความไปยังชุดค่าผสมของหัวข้อ ให้ระบุเงื่อนไข ซึ่งเป็นนิพจน์บูลีนที่กำหนดหัวข้อเป้าหมาย ตัวอย่างเช่น เงื่อนไขต่อไปนี้จะส่งข้อความไปยังอุปกรณ์ที่สมัครใช้บริการ TopicA
และ TopicB
หรือ TopicC
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
FCM จะประเมินเงื่อนไขในวงเล็บก่อน แล้วจึงประเมินนิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้น ผู้ใช้ที่สมัครรับข้อมูลหัวข้อเดียวจะไม่ได้รับการแจ้งเตือน ในทำนองเดียวกัน ผู้ใช้ที่ไม่ได้ติดตาม TopicA
จะไม่ได้รับการแจ้งเตือน ชุดค่าผสมต่อไปนี้จะได้รับ
TopicA
และTopicB
TopicA
และTopicC
คุณสามารถใส่หัวข้อได้สูงสุด 5 หัวข้อในนิพจน์เงื่อนไข
หากต้องการส่งไปยังเงื่อนไข
// 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);
});
// 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);
# 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)
// 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)
// 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);
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
ขั้นตอนถัดไป
- คุณสามารถใช้เซิร์ฟเวอร์เพื่อสมัครใช้บริการอินสแตนซ์แอปไคลเอ็นต์กับหัวข้อและดำเนินการจัดการอื่นๆ ได้ ดูหัวข้อจัดการการติดตามหัวข้อในเซิร์ฟเวอร์