Firebase Cloud Messaging (Push Notifications) in Android Development
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send push notifications to your app's users. It is free and highly customizable, supporting notifications and data messages. This article explains how to integrate FCM into your Android app with Kotlin examples.
1. Setting Up Firebase Cloud Messaging
Step 1: Configure Firebase in Your Project
Go to the Firebase Console, create a project, and register your Android app. Download the google-services.json
file and place it in your app's app/
directory.
Step 2: Add Dependencies
Add the Firebase Cloud Messaging dependency to your build.gradle
file:
dependencies { implementation platform('com.google.firebase:firebase-bom:32.0.0') implementation 'com.google.firebase:firebase-messaging-ktx' }
Step 3: Apply the Google Services Plugin
Ensure you apply the Google Services plugin in your build.gradle
file:
apply plugin: 'com.google.gms.google-services'
2. Receiving Push Notifications
Service to Handle Messages
Create a class that extends FirebaseMessagingService
to handle incoming messages:
class MyFirebaseMessagingService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) { // Handle data payload remoteMessage.data.let { println("Message data: $it") } // Handle notification payload remoteMessage.notification?.let { showNotification(it.title, it.body) } } private fun showNotification(title: String?, body: String?) { val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channelId = "default_channel_id" // Create notification channel (for Android 8.0+) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel(channelId, "Default Channel", NotificationManager.IMPORTANCE_HIGH) notificationManager.createNotificationChannel(channel) } val notificationBuilder = NotificationCompat.Builder(this, channelId) .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) notificationManager.notify(0, notificationBuilder.build()) } override fun onNewToken(token: String) { println("New FCM Token: $token") // Send token to your server } }
3. Sending Notifications from Firebase Console
To send a notification:
- Go to the Firebase Console.
- Navigate to the "Cloud Messaging" section.
- Click "Send your first message."
- Fill in the details (e.g., title and body) and target your app.
- Send the message.
4. Sending Data Messages
Data messages are handled in the onMessageReceived()
method and do not automatically display a notification. Here's an example payload:
{ "to": "device_token", "data": { "key1": "value1", "key2": "value2" } }
Use tools like Postman or server-side code to send data messages to your app using the FCM API.
5. Displaying Notifications
You can customize notification behavior by modifying the showNotification()
method:
private fun showNotification(title: String?, body: String?) { val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT) val notificationBuilder = NotificationCompat.Builder(this, "default_channel_id") .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setAutoCancel(true) .setContentIntent(pendingIntent) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(0, notificationBuilder.build()) }
6. Testing Push Notifications
Use the Firebase Console or the FCM REST API to send test messages to your app. Ensure your app has the correct permissions and is running on a device with Google Play Services.
Conclusion
Firebase Cloud Messaging is a robust solution for delivering push notifications and data messages to Android apps. By implementing FCM, you can enhance user engagement and provide timely updates to your users. Start integrating FCM into your Android projects today!