การเริ่มต้นอย่างรวดเร็วนี้อธิบายวิธีตั้งค่า Firebase Cloud Messaging ในแอปไคลเอ็นต์บนอุปกรณ์เคลื่อนที่และเว็บ เพื่อให้คุณส่งข้อความได้อย่างน่าเชื่อถือ สำหรับสภาพแวดล้อมของเซิร์ฟเวอร์ โปรดดูสภาพแวดล้อมของเซิร์ฟเวอร์และ
FCM
เริ่มต้นใช้งาน Firebase Cloud Messaging และ Android
FCMไคลเอ็นต์ต้องใช้อุปกรณ์ที่ใช้ Android 5.0 ขึ้นไป ซึ่งติดตั้งแอป Google Play Store ไว้ด้วย หรือโปรแกรมจำลองที่ใช้ Android 5.0 ที่มี Google API โปรดทราบว่าคุณไม่ได้ถูกจำกัดให้ใช้งานแอป Android ผ่าน Google Play Store เท่านั้น
ตั้งค่า SDK
เพิ่ม Firebase ลงในโปรเจ็กต์ Android หากยังไม่ได้เพิ่ม
เราขอแนะนำอย่างยิ่งให้เปิดใช้ Google Analytics ในโปรเจ็กต์ของคุณเพื่อให้ได้รับประสบการณ์การใช้งาน FCM ที่ดีที่สุด Google Analytics เป็นข้อกำหนดสำหรับFCM การรายงานการนำส่งข้อความ
แก้ไขไฟล์ Manifest ของแอป
เพิ่มข้อมูลต่อไปนี้ลงในไฟล์ Manifest ของแอป
- บริการที่ขยาย
FirebaseMessagingService
จำเป็นต้องระบุ หากคุณต้องการจัดการข้อความนอกเหนือจากการรับการแจ้งเตือนในแอปในเบื้องหลัง หากต้องการรับการแจ้งเตือนในแอปที่ทำงานอยู่เบื้องหน้า รับเพย์โหลดข้อมูล และอื่นๆ คุณต้องขยายบริการนี้ - (ไม่บังคับ) ภายในคอมโพเนนต์แอปพลิเคชัน องค์ประกอบข้อมูลเมตาเพื่อตั้งค่าการแจ้งเตือนเริ่มต้น ไอคอนและสี Android จะใช้ค่าเหล่านี้เมื่อใดก็ตามที่ข้อความขาเข้าไม่ได้ตั้งค่าไอคอนหรือสีอย่างชัดเจน
- (ไม่บังคับ) ตั้งแต่ Android 8.0 (API ระดับ 26) ขึ้นไป
ระบบรองรับและแนะนำให้ใช้
ช่องทางการแจ้งเตือน FCM มีช่องทางการแจ้งเตือนเริ่มต้น
พร้อมการตั้งค่าพื้นฐาน หากต้องการ
สร้างและใช้แชแนลเริ่มต้นของคุณเอง
ให้ตั้งค่า
default_notification_channel_id
เป็นรหัสของออบเจ็กต์แชแนลการแจ้งเตือน ตามที่แสดง FCM จะใช้ค่านี้ เมื่อใดก็ตามที่ข้อความขาเข้าไม่ได้ตั้งค่าการแจ้งเตือน แชแนลอย่างชัดเจน ดูข้อมูลเพิ่มเติมได้ที่ จัดการช่องทางการแจ้งเตือน
<service android:name=".java.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
<!-- 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" />
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
ขอสิทธิ์การแจ้งเตือนรันไทม์ใน Android 13 ขึ้นไป
Android 13 เปิดตัวสิทธิ์รันไทม์ใหม่สำหรับการแสดงการแจ้งเตือน การเปลี่ยนแปลงนี้ จะส่งผลต่อแอปทั้งหมดที่ทำงานบน Android 13 ขึ้นไปซึ่งใช้FCM การแจ้งเตือน
โดยค่าเริ่มต้น FCM SDK (เวอร์ชัน 23.0.6 ขึ้นไป) จะมีสิทธิ์
POST_NOTIFICATIONS
ที่กำหนดไว้ในไฟล์ Manifest
อย่างไรก็ตาม แอปของคุณจะต้องขอสิทธิ์เวอร์ชันรันไทม์นี้ด้วยโดยใช้ค่าคงที่ android.permission.POST_NOTIFICATIONS
แอปของคุณจะแสดงการแจ้งเตือนไม่ได้จนกว่า
ผู้ใช้จะให้สิทธิ์นี้
วิธีขอสิทธิ์รันไทม์ใหม่มีดังนี้
Kotlin
// Declare the launcher at the top of your Activity/Fragment: private val requestPermissionLauncher = registerForActivityResult( ActivityResultContracts.RequestPermission(), ) { isGranted: Boolean -> if (isGranted) { // FCM SDK (and your app) can post notifications. } else { // TODO: Inform user that that your app will not show notifications. } } private fun askNotificationPermission() { // This is only necessary for API level >= 33 (TIRAMISU) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED ) { // FCM SDK (and your app) can post notifications. } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { // TODO: display an educational UI explaining to the user the features that will be enabled // by them granting the POST_NOTIFICATION permission. This UI should provide the user // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission. // If the user selects "No thanks," allow the user to continue without notifications. } else { // Directly ask for the permission requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } } }
Java
// Declare the launcher at the top of your Activity/Fragment: private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted) { // FCM SDK (and your app) can post notifications. } else { // TODO: Inform user that that your app will not show notifications. } }); private void askNotificationPermission() { // This is only necessary for API level >= 33 (TIRAMISU) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) { // FCM SDK (and your app) can post notifications. } else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) { // TODO: display an educational UI explaining to the user the features that will be enabled // by them granting the POST_NOTIFICATION permission. This UI should provide the user // "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission. // If the user selects "No thanks," allow the user to continue without notifications. } else { // Directly ask for the permission requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); } } }
โดยทั่วไป คุณควรแสดง UI ที่อธิบายให้ผู้ใช้ทราบถึง ฟีเจอร์ที่จะเปิดใช้หากผู้ใช้ให้สิทธิ์แก่ แอปในการโพสต์การแจ้งเตือน UI นี้ควรมีตัวเลือกให้ผู้ใช้ยอมรับหรือปฏิเสธ เช่น ปุ่มตกลงและไม่เป็นไร หากผู้ใช้เลือกตกลง ให้ขอสิทธิ์โดยตรง หากผู้ใช้เลือกไม่เป็นไร ให้ผู้ใช้ดำเนินการต่อโดยไม่มีการแจ้งเตือน
ดูแนวทางปฏิบัติแนะนำเพิ่มเติมเกี่ยวกับเวลาที่แอปควรขอสิทธิ์ POST_NOTIFICATIONS
จากผู้ใช้ได้ที่สิทธิ์รันไทม์ของการแจ้งเตือน
สิทธิ์การแจ้งเตือนสำหรับแอปที่กำหนดเป้าหมายเป็น Android 12L (API ระดับ 32) หรือต่ำกว่า
Android จะขอสิทธิ์จากผู้ใช้โดยอัตโนมัติในครั้งแรกที่แอปของคุณ สร้างแชแนลการแจ้งเตือน ตราบใดที่แอปทำงานอยู่เบื้องหน้า อย่างไรก็ตาม มีข้อควรระวังที่สำคัญเกี่ยวกับช่วงเวลาในการสร้างช่อง และการขอสิทธิ์ดังนี้
- หากแอปสร้างแชแนลการแจ้งเตือนแรกเมื่อทำงานในเบื้องหลัง ซึ่ง FCM SDK จะทำเมื่อได้รับการแจ้งเตือน FCM Android จะไม่อนุญาตให้แสดงการแจ้งเตือนและจะไม่แจ้งให้ผู้ใช้ขอสิทธิ์การแจ้งเตือนจนกว่าจะมีการเปิดแอปในครั้งถัดไป ซึ่งหมายความว่าการแจ้งเตือนใดๆ ที่ได้รับ ก่อนที่แอปจะเปิดขึ้นและผู้ใช้ยอมรับสิทธิ์จะหายไป
- เราขอแนะนําอย่างยิ่งให้อัปเดตแอปเพื่อกำหนดเป้าหมายเป็น Android 13 ขึ้นไปเพื่อ ใช้ประโยชน์จาก API ของแพลตฟอร์มในการขอสิทธิ์ หากทำไม่ได้ แอปควรสร้างแชแนลการแจ้งเตือนก่อนที่จะส่งการแจ้งเตือนใดๆ ไปยังแอปเพื่อทริกเกอร์กล่องโต้ตอบสิทธิ์การแจ้งเตือนและตรวจสอบว่าไม่มีการแจ้งเตือนใดสูญหาย ดูข้อมูลเพิ่มเติมได้ที่แนวทางปฏิบัติแนะนำเกี่ยวกับสิทธิ์การแจ้งเตือน
ไม่บังคับ: นำสิทธิ์เข้าถึง POST_NOTIFICATIONS
ออก
โดยค่าเริ่มต้น FCM SDK จะมีสิทธิ์ POST_NOTIFICATIONS
หากแอปไม่ได้ใช้ข้อความแจ้งเตือน (ไม่ว่าจะผ่านFCM
การแจ้งเตือน ผ่าน SDK อื่น หรือโพสต์โดยแอปของคุณโดยตรง) และคุณไม่ต้องการให้แอปมีสิทธิ์ดังกล่าว คุณสามารถนำสิทธิ์ออกได้โดยใช้เครื่องหมาย remove
ของเครื่องมือผสานไฟล์ Manifest
โปรดทราบว่าการนำสิทธิ์นี้ออกจะทำให้ระบบไม่แสดง
การแจ้งเตือนทั้งหมด ไม่ใช่แค่FCM เพิ่มข้อมูลต่อไปนี้ลงในไฟล์ Manifest ของแอป
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" tools:node="remove"/>
เข้าถึงโทเค็นการลงทะเบียนอุปกรณ์
เมื่อเริ่มต้นแอปเป็นครั้งแรก FCM SDK จะสร้างโทเค็นการลงทะเบียน
สำหรับอินสแตนซ์แอปไคลเอ็นต์ หากต้องการกำหนดเป้าหมายอุปกรณ์เครื่องเดียวหรือ
สร้างกลุ่มอุปกรณ์ คุณจะต้องเข้าถึงโทเค็นนี้โดยขยาย
FirebaseMessagingService
และลบล้าง onNewToken
เนื่องจากอาจมีการหมุนเวียนโทเค็นหลังจากเริ่มต้นครั้งแรก เราขอแนะนำเป็นอย่างยิ่งให้คุณดึงโทเค็นการลงทะเบียนที่อัปเดตล่าสุด
โทเค็นการลงทะเบียนอาจเปลี่ยนแปลงเมื่อเกิดเหตุการณ์ต่อไปนี้
- มีการกู้คืนแอปในอุปกรณ์เครื่องใหม่
- ผู้ใช้ถอนการติดตั้ง/ติดตั้งแอปอีกครั้ง
- ผู้ใช้ล้างข้อมูลแอป
เรียกโทเค็นการลงทะเบียนปัจจุบัน
เมื่อต้องการเรียกโทเค็นปัจจุบัน ให้เรียกใช้
FirebaseMessaging.getInstance().getToken()
:
Kotlin
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> if (!task.isSuccessful) { Log.w(TAG, "Fetching FCM registration token failed", task.exception) return@OnCompleteListener } // Get new FCM registration token val token = task.result // Log and toast val msg = getString(R.string.msg_token_fmt, token) Log.d(TAG, msg) Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() })
Java
FirebaseMessaging.getInstance().getToken() .addOnCompleteListener(new OnCompleteListener<String>() { @Override public void onComplete(@NonNull Task<String> task) { if (!task.isSuccessful()) { Log.w(TAG, "Fetching FCM registration token failed", task.getException()); return; } // Get new FCM registration token String token = task.getResult(); // Log and toast String msg = getString(R.string.msg_token_fmt, token); Log.d(TAG, msg); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } });
ตรวจสอบการสร้างโทเค็น
onNewToken
การเรียกกลับจะทำงานทุกครั้งที่มีการสร้างโทเค็นใหม่
Kotlin
/** * Called if the FCM registration token is updated. This may occur if the security of * the previous token had been compromised. Note that this is called when the * FCM registration token is initially generated so this is where you would retrieve the token. */ override fun onNewToken(token: String) { Log.d(TAG, "Refreshed token: $token") // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // FCM registration token to your app server. sendRegistrationToServer(token) }
Java
/** * There are two scenarios when onNewToken is called: * 1) When a new token is generated on initial app startup * 2) Whenever an existing token is changed * Under #2, there are three scenarios when the existing token is changed: * A) App is restored to a new device * B) User uninstalls/reinstalls the app * C) User clears app data */ @Override public void onNewToken(@NonNull String token) { Log.d(TAG, "Refreshed token: " + token); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // FCM registration token to your app server. sendRegistrationToServer(token); }
หลังจากได้รับโทเค็นแล้ว คุณจะส่งโทเค็นไปยังเซิร์ฟเวอร์ของแอปและจัดเก็บ โดยใช้วิธีที่คุณต้องการได้
ตรวจสอบว่ามีบริการ Google Play หรือไม่
แอปที่ใช้ SDK ของบริการ Play
ควรตรวจสอบ APK ของบริการ Google Play ที่เข้ากันได้ในอุปกรณ์เสมอ
ก่อนที่จะเข้าถึงฟีเจอร์ของบริการ Google Play เราขอแนะนำให้ดำเนินการนี้ใน 2 ที่ ได้แก่ ในเมธอด onCreate()
ของกิจกรรมหลัก และในเมธอด onResume()
ของกิจกรรมหลัก การตรวจสอบใน onCreate()
ช่วยให้มั่นใจว่าแอป
จะใช้งานไม่ได้หากตรวจสอบไม่สำเร็จ การตรวจสอบใน onResume()
จะช่วยให้มั่นใจได้ว่า
หากผู้ใช้กลับไปยังแอปที่กําลังทํางานผ่านวิธีอื่น เช่น
ผ่านปุ่มย้อนกลับ ระบบจะยังคงทําการตรวจสอบ
หากอุปกรณ์ไม่มีบริการ Google Play เวอร์ชันที่เข้ากันได้ แอปของคุณจะเรียกใช้
GoogleApiAvailability.makeGooglePlayServicesAvailable()
เพื่อให้ผู้ใช้ดาวน์โหลดบริการ Google Play จาก Play Store ได้
ป้องกันการเริ่มต้นอัตโนมัติ
เมื่อสร้างโทเค็นการลงทะเบียน FCM ไลบรารีจะอัปโหลดตัวระบุและข้อมูลการกำหนดค่าไปยัง Firebase หากต้องการป้องกันไม่ให้ระบบสร้างโทเค็นโดยอัตโนมัติ ให้ปิดใช้การรวบรวมข้อมูล Analytics และการเริ่มต้น FCM โดยอัตโนมัติ (คุณต้องปิดใช้ทั้ง 2 อย่าง) โดยเพิ่มค่าข้อมูลเมตาต่อไปนี้ลงใน AndroidManifest.xml
<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" /> <meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
หากต้องการเปิดใช้การเริ่มต้นอัตโนมัติของ FCM อีกครั้ง ให้เรียกใช้รันไทม์ดังนี้
Kotlin
Firebase.messaging.isAutoInitEnabled = true
Java
FirebaseMessaging.getInstance().setAutoInitEnabled(true);
หากต้องการเปิดใช้การรวบรวมข้อมูล Analytics อีกครั้ง ให้เรียกใช้เมธอด
setAnalyticsCollectionEnabled()
ของคลาส FirebaseAnalytics
เช่น
setAnalyticsCollectionEnabled(true);
ค่าเหล่านี้จะยังคงอยู่เมื่อรีสตาร์ทแอปเมื่อตั้งค่าแล้ว
ส่งข้อความแจ้งเตือน
คุณสามารถส่งข้อความแจ้งเตือนทดสอบโดยใช้คำสั่งต่อไปนี้เพื่อให้แน่ใจว่าไคลเอ็นต์ Android ได้รับการตั้งค่าอย่างถูกต้อง
- ติดตั้งและเรียกใช้แอปในอุปกรณ์เป้าหมาย
- ตรวจสอบว่าแอปทำงานอยู่เบื้องหลังในอุปกรณ์
- เปิดหน้าการรับส่งข้อความในFirebaseคอนโซล
- หากนี่เป็นข้อความแรก ให้เลือกสร้างแคมเปญแรก ข้อความการแจ้งเตือนของ Firebase แล้วคลิกสร้าง
- หรือในแท็บแคมเปญ ให้เลือกแคมเปญใหม่ แล้วเลือก การแจ้งเตือน
- ป้อนข้อความ ช่องอื่นๆ ทั้งหมดเป็นช่องที่ไม่บังคับ
- เลือกส่งข้อความทดสอบจากแผงด้านขวา
- ในช่องที่มีป้ายกำกับว่าเพิ่มโทเค็นการลงทะเบียน FCM ให้ป้อนโทเค็นการลงทะเบียนที่คุณได้รับในส่วนก่อนหน้าของคู่มือนี้
- เลือกทดสอบ
อุปกรณ์ไคลเอ็นต์เป้าหมายที่มีแอปทำงานในเบื้องหลังควรได้รับการแจ้งเตือน
ดูข้อมูลเพิ่มเติมเกี่ยวกับการนำส่งข้อความไปยังแอปได้ที่FCM แดชบอร์ด การรายงาน ซึ่งบันทึกจำนวนข้อความที่ส่งและเปิดในอุปกรณ์ Apple และ Android พร้อมกับข้อมูลการแสดงผล (การแจ้งเตือนที่ผู้ใช้เห็น) สำหรับ แอป Android
ขั้นตอนถัดไป
หลังจากตั้งค่าแอปไคลเอ็นต์แล้ว คุณจะเริ่มรับหรือส่งข้อความ ไปยังผู้ใช้ได้โดยทำดังนี้