Mobile push notifications are used to send important messages to mobile devices when user is not actively using your application. App developers use mobile push notifications to do a lot of things; for example, let user know about a sale or let user know about a sports score updates.
In this tutorial, we will see how to configure and use Firebase Cloud Messaging platform in your Android application.
1. What is Firebase Cloud Messaging?
Firebase Cloud Messaging (FCM) is the new version of GCM. Built on top of the GCM infrastructure, it provides the ability send messages to multiple platforms beyond Android: iOS and Mobile web.
Via Firebase dashboard, anyone (even non technical users) can now easily send and schedule messages directly to an segment of users or to an specific device.
Google recommend developers to upgrade to FCM and use it for all new App developments. And they have announced that all the new client-side features will be added to FCM SDKs only.
However, all the existing applications built on GCM will still continue to work.
FCM SDK makes it easy for developers to integrate the cloud messaging. Now you no longer have manually register your device with GCM server. The FCM SDK will take care of itself.
Let us see how to integrate Firebase Cloud Messaging to your Android application.
2. Configure the Firebase SDK.
Let us begin with creating a Firebase project in the Firebase console. You will be asked to login with your Google Mail account. Once you logged in, select Add App button to create a new project in Firebase Console.
Select the Android platform to continue with Android Firebase configuration.
If you have not created any app before, you will be asked to select the Project Name and Country Region. Provide the details and Continue with the configuration.
Enter your Android app package name. This should be same as defined in your android AndroidMaifest.xml file.
You may provide the debug keystore SHA-1 certificate. However this is optional and hence you may leave it empty.
Select Add App button. This will download the google-services.json configuration file for your app. Copy this file into your project’s module folder, typically inside app/ directory.
Screenshot depicts project structure after adding google-services.json file
Now, add the required Firebase SDK dependencies to your project. Modify the project level build.gradle (<project>/build.gradle) file and add the google-services.
And, add the following to app module level build.gradle file.
After the dependencies are added Sync your project with gradle file changes. With this we’re done with the project configuration.
3. Integrating Firebase Cloud Messaging
Before push notification messages are delivered, each device are registered with GCM. The Firebase Cloud Messaging SDK takes care of the registration process. Upon successful registration it calls onTokenRefresh() callback where you can retrieve the token.
You can retrieve the token by extending FirebaseInstanceIdService class.
Register the FCMInitializationService service in your AndroidManifest.xml:
Please note, implementing FCM requires android.permission.INTERNET permission. Make sure it is declare in your AndroidManifest.xml.
The FirebaseMessagingService class is the base class for communicating with Firebase Messaging. It also provides functionality such as automatically displaying notifications.
The onMessageReceived() method is called when a message is received.
Register the FCMCallbackService class in your AndroidManifest.xml file:
4. Publishing Message from Firebase Console
Let us now visit Firebase Console to test if notifications are working. Open your app in Firebase Console. Click on Notifications tab in the left panel. If visiting this for the first time, click on Send Your First Message.
Enter details and click Send Message button.
Now, You should receive a notification in your device.
5. Download Example Source Code
Download Source Code