![]() |
VOOZH | about |
Firebase Cloud Messaging is a real-time solution for sending notifications to client apps without any kind of charges. FCM can reliably transfer notifications of up to 4Kb of payload. In this article, a sample app showing how this service can be availed is developed. Though FCM also allows sending out notifications using an app server, here Firebase admin SDK is used. Follow the complete article to implement an example of FCM.
Step 1: Add Firebase to the project and the required permissions To add firebase to the project please refer Adding Firebase to Android App. The following is the gist of adding FCM to the app. Go to Tools -> Firebase -> Cloud Messaging -> Set up Firebase Cloud Messaging
Since receiving FCM notifications require the use of the internet, add the following permission to the AndroidManifest.xml file anywhere between the </application> and </manifest> tags.
<uses-permission android:name="android.permission.INTERNET" />
Note: compile '.....' this format for setting up dependencies is deprecated, instead, use implementation '.....' to declare dependencies in case of any discrepancy.
Step 2: Add all the required drawable resources Here, the following icon has been used as a drawable resource. Add all the drawable resources to the drawable resource folder.👁 gfg icon
Step 3: Customize the activity_main.xml Here, the home screen of the app just holds a TextView, however, one can customize the app as per the requirements.
Step 4: Create the Notification Layout Create a new notification.xml file to design the layout for the Notification. This step is stated as optional because the content and title too can be directly set too without customizing the appearance of the notification, however here the notification has the following layout. Here the Notification consists of:
Step 5: Create the message receiving class Create a FirebaseMessageReceiver.java class. This class extends the FirebaseMessagingService. Add the following code to the AndroidManifest.xml file between the </activity> and </application> tags to recognise the FirebaseMessagingService as a service in the app.
Here the attribute 'android: name' is assigned the name of the Java file that extends the FirebaseMessagingService so pass the class name FirebaseMessageReceiver. This service is required to do any type of message handling beyond just receiving notifications, while the client app runs in the background. It also serves the purpose of receiving notifications in foreground apps and much more. The complete AndroidManifest.xml file is given below.
Step 6: Working with FirebaseMessageReceiver.java (Java) or for Kotlin FirebaseMessageReceiver.kt. It overrides the onMessageReceived() method to handle 2 events:
This method takes RemoteMessage as a parameter. RemoteMessage is a class that extends Object Class and implements Parcelable interface. It is nothing but the object of the message passed using FCM. The above method then calls a user-defined method showNotification() which in turn accepts two parameters. A detailed explanation is provided via comments in the code itself. A notification channel is required for Notifications in Android Versions greater than Oreo. In this example, since a customized notification is designed, a method getCustomDesign() is defined and called to set the resources accordingly. This method sets the custom layout for the display of the notification received. Assuming that only title and body are received from the notification, it appropriately maps the TextViews according to the IDs and sets the image resource for the notification. The complete code for this file is given below.
Now run the app on your emulator or in your mobile device. Step 8: Send the notification using FCM