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.
Approach
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
- Connect your app to Firebase: Complete the three steps of creating a Firebase project.
- Add FCM to the app.
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.
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.
- activity_main.xml
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:
- An ImageView
- A TextView for the Title
- A TextView for the Message.
- notification.xml
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.
- AndroidManifest.xml
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.
- AndroidManifest.xml
Step 6: Working with FirebaseMessageReceiver.java class
FirebaseMessageReceiver.java class overrides the onMessageReceived()
method to handle 2 events:
- If Notification contains any data payload, i.e it is received from the app server.
- If Notification contains any notification payload, i.e. it is sent via the Firebase Admin SDK.
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.
- FirebaseMessageReceiver.java
Note: The notification when clicked will always redirect to the launcher activity of the app when the app is in the background and to the activity passed in the intent if it is in the foreground. To redirect the user always to some other activity than the launcher, you’ll have to send data payload using a server app, sending messaging using Firebase SDK does not support this.
Step 7: Complete the MainActivity.java file
Since here nothing much is presented in the activity_main.xml, the MainActivity file does not need any additional code.
- MainActivity.java
Now run the app on your emulator or in your mobile device.
Step 8: Send the notification using FCM
- Go to Firebase console and choose the appropriate project.
- Choose Cloud Messaging.
- Choose Send your First Message. The following window pops up. Fill out the details. While the text field is compulsory, rest all is optional. One can also add an image using a link or upload it, however uploading an image cost additional storage charges.
- In the target section, choose the app domain.
- One can either send out the notification now or schedule it for some time in the future.
- Rest all the other fields are optional and can be left empty. Click on Review and then Publish.