Firebase Cloud Messaging (FCM) là một giải pháp nhắn tin đa nền tảng của Google cho phép gửi tin nhắn một cách tin cậy và miễn phí.
Với việc sử dụng FCM, ta có thể gửi notification message đến cho client app lên đến 4KB.
Firebase Push Notification hoạt động như thế nào ?
Việc thực thi FCM bao gồm hai thành phần chính để gửi và nhận:
- Một môi trường tin cậy như Cloud Function của Firebase hoặc một app server để build, target và send messages.
- Client app iOS, Android hoặc web (JavaScript) nhận tin nhắn qua dịch vụ transport dành riêng cho từng nền tảng tương ứng.
Tích hợp FCM vào Android Project
Bước 1: Truy cập trang Firebase Console
Bước 2: Tiến hành tạo một Project nếu bạn chưa có.
Bước 3: Chọn nền tảng để bắt đầu.
Bước 4: Nhập package name của Project và mã SHA-1.
Để lấy mã SHA-1 bạn hãy vào Android Studio -> Chọn tab Gradle ở thanh bên phải -> Chọn app modules -> Tasks -> Android -> signing report
Bước 5: Tải file google-services.json và đặt nó vào trong module app.
Bước 6: Tích hợp Firebase SDK
- Thêm vào file build.gradle Project:
1 2 3 4 5 6 7 | buildscript { dependencies { // Add this line classpath 'com.google.gms:google-services:4.3.2' } } |
- Thêm vào file build.gradle Module App:
1 2 3 4 5 6 7 | apply plugin: 'com.google.gms.google-services' dependencies { // Add this line implementation 'com.google.firebase:firebase-messaging:20.0.0' } |
- Tiến hành Sync Project bằng cách click Sync now ở Android Studio
Bước 7: Tạo một Service
kế thừa từ FirebaseMessagingService
để xử lý việc nhận message từ Firebase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onNewToken(token: String) { sendRegistrationToServer(token) } private fun sendRegistrationToServer(token: String?) { // TODO: Implement this method to send token to your app server. } override fun onMessageReceived(remoteMessage: RemoteMessage) { val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager val channelId = getString(R.string.project_id) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { handleNotificationAndroidO(notificationManager, channelId) } val notification = NotificationCompat.Builder(this, channelId) .setAutoCancel(true) .setColor(ContextCompat.getColor(this, R.color.colorAccent)) .setContentTitle(getString(R.string.app_name)) .setContentText(remoteMessage.notification?.body) .setDefaults(Notification.DEFAULT_ALL) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.ic_launcher_foreground) .setAutoCancel(true) .build() notificationManager.notify(1000, notification) } @RequiresApi(Build.VERSION_CODES.O) private fun handleNotificationAndroidO( notificationManager: NotificationManager, channelId: String ) { createNotificationChannel(notificationManager, channelId) notificationManager .getNotificationChannel(channelId) ?.canBypassDnd() } @RequiresApi(Build.VERSION_CODES.O) private fun createNotificationChannel( notificationManager: NotificationManager, channelId: String ) { val notificationChannel = NotificationChannel(channelId, getString(R.string.app_name), IMPORTANCE_HIGH) notificationManager.createNotificationChannel(notificationChannel) } } |
Bước 8: Thêm vào file AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 | <service android:name=".MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" /> |
Gửi Push Notification sử dụng Firebase Console
Bước 1: Vào Firebase Console và chọn Project đã tạo.
Bước 2: Chọn Cloud Messaging
ở Left Menu.
Bước 3: Click chọn New notification
.
Bước 4: Nhập thông tin của notification và tiến hành gửi.
Bước 5: Check thành quả trên device.
Tham khảo
Mời các bạn tham khảo thêm tại:
https://firebase.google.com/docs/cloud-messaging
https://firebase.google.com/docs/cloud-messaging/android/client