Implementing Fcm ( Firebase Cloud Messaging ) In Android App
The final project is available for download from the Github repository
What is FCM?
FCM (Firebase Cloud Messaging) replaces Google Cloud Messaging (GCM) as the API to send/receive push notifications from.
Most of the popular apps in the market uses push notifications to engage the user and i am sure you are interested to implement them in your app too so follow along.
How does FCM work
As shown in the figure, the FCM is the middle ware between your device and the server.
You send the message you want to send to the devices to the Firebase server and it redirects it to the appropriate device. It handles all the middle ware part like scheduling notification time, failure and success rate, unique device tokens etc.
We can use a http/xmpp server to send the message to the FCM server but in our tutorial we use the handy tool provided by the FCM guys - Notification console.
Step 1. Setting up the Android project
Prerequisite : You need to have Studio with version 1.5 or higher for the services to work.
Create a new Android Studio Project and name it FCM_Tutorial.
Now note down the package name, we will need it to make the google-services.json
file.
Head over to Firebase console and click Create New Project. Enter FCM_Tutorial as the name project and click create project. You will be redirected to a screen like this
Click Add firebase to your Android project and in the popup - Enter your package name
Press Add app and now you will get the google-services.json downloaded automatically. Now keep pressing continue to finish the integration steps and get back to the project.
Drag the json you downloaded into your project root (where .build gradle file is of the app module). The json file actually includes all the relevant settings for the firebase app including the firebase database url for your app.
Setup Gradle
Open your project build.gradle file and add the following dependencies
Now open your app module build.grade and add these dependencies
At the end of the file we need to apply our google play servcies plugin :
apply plugin: 'com.google.gms.google-services'
Sync your gradle and you will have the firebase setup in your project.
Step 2. Creating our TokenService
Each device need to be uniquely identified so that we can check success/failure and target individual user. Firebase provides FirebaseInstanceId class which takes care of creating unique device token for the current device.
The token is refreshed randomly based on duration or other things so to avoid checking each time for new token, We have to use a Service class that extends FirebaseInstanceIdService.
In it, the onTokenRefresh is called each time a new token is generated.
So it’s time to create a new class in our project. Name it TokenService.java and have it extend FirebaseInstanceIdService. Here is our implementation
We get the token from the FirebaseInstanceId instance and just log it currently to the logcat so that we can use it later. You can send it to server in a production app to store it in your db.
Step 3. Registering our service in manifest
For our TokenService to actually work we need to make sure it is added to our AndroidManifest.xml file. Open up the manifest and inside our application tag add the service lines
Run the app and you should see the token in Logcat.
Step 4. Testing A Push notification through the Notification Console
This was really easy right. You can now use Notification Console to start sending push notification to your app. Go to the notification console as shown in the following pics
Note: For the notification to show make sure the app is in background and not in foreground
In the second screenshot, you can set the Title and also send messages based on filter like all devices/groups/particular device, adjust time for your notification. We will use the Single device and use the token we got in #step 3 of our tutorial.
After filling the title and token, scroll down and click Send Message. You will see the notification show up in your device notification panel.
Step 5. Creating our FCMMessageReceiverService
Well our app is able to receive notifications and show in our app but in actual we don’t have any code in our app that actually does the part of displaying the notification. This can be tested by doing step 4 with app in foreground, you won’t see the notification with app in foreground.
This is a feature of Firebase; it shows the notification automatically if the app is in background. However if the app is in foreground, we have to extend FirebaseMessagingService.
This class has a method onMessageReceived which is called when a FCM is received in foreground. Create a new class in your project and name it FCMMessageReceiverService.
Here is our implementation :
We override onMessageReceived and call the sendNotification
method with the FCM title. The notification method simply creates a Notification and show it.
Step 6. Registering our message service in manifest
Just like TokenService our FCMMessageReceiverService needs to be registered in manifest too so that it is visible and can be called by Firebase. Update your manifest application tag with
Testing our push notification once again
Go Ahead and do the Step 4 again with app in foreground. You will see the logcat show the message received notification as well as the notification appear in the notification panel.
Conclusion
Thank you for reading this far. For any questions you are welcome to ask in our chat room.
Next Steps
Please take a look at our other tutorials :)