Firebase Cloud Messaging (FCM) Quick Set Up in Android

Firebase Cloud Messaging (FCM) is the new Cloud Messaging Service by Google for Android, IOS, and Chrome (Web). As Google says FCM is the new version of GCM don’t expect further development on GCM so use FCM in your new projects to benefit extra features for now and future. Don’t get confuse with Firebase pricing FCM is absolutely free.

Benefits of upgrading to FCM SDK include: (Quoted from FCM faq)
– Simpler client development. You no longer have to write your own registration or subscription retry logic.
– An out-of-the-box notification solution. You can use Firebase Notifications, a serverless notifications solution with a web console that lets anyone send notifications to target specific audiences based on Firebase Analytics insights.

OK… Lets start

fcm-working-chart

Prerequisites
– Android 2.3 or newer
– Google Play services 9.0.2 or newer
– Google Play services SDK from the Android SDK Manager
– Android Studio 1.5 or higher

Create a Firebase Project at Firebase Console
1. Go to Firebase console and login with your Gmail id.
2. Click CREATE NEW PROJECT, When prompted click CREATE PROJECT after filling your Project name and Country.
3. Click Add Firebase to your Android App
4. When prompted, enter your app’s package name and click ADD APP
(By clicking ADD APP button config file named google-services.json will be downloaded)
5. Copy the downloaded google-services.json file to your projects app directory. then click CONTINUE.

Add Firebase in Gradle
Add Google services plugin to your project level build.gradle to load the google-services.json configuration. then apply it in your app level build.gradle

1. Project-level build.gradle (/build.gradle):

2. App-level build.gradle (//build.gradle):

Click FINISH on the firebase console prompt to complete the setup.

Note:
Don’t forget to press Sync to apply the gradle changes…

That’s it now you successfully completed Firebase SDK setup. Now you need to add the required firebase service library to your app level build.gradle.

These libraries are available for the different Firebase features.

We need Firebase Cloud Messaging so add the plug8in for Cloud Messaging / Notifications to your app level build.gradle

Finally click Sync now to load the library… Thats it … let’s move in to the coding part.

Note:
To fix Failed to resolve: com.google.firebase:firebase-messaging:9.0.2 error … update your Google Play Services and Google Repository to Rev. 30 and 28 respectively.

For better project structure we keep the FCM related classes in a separate package named fcm. Create a new package and name it fcm.

1. Create a class named FcmInstanceIdService extend FirebaseInstanceIdService then Override onTockenRefresh to get the FirebaseInstance token.

It handles the creation, rotation, and updating of registration tokens. onTockenRefresh will called only on apps first startup and on the next token update… so save the token in the preference for later use.

2. Create a class named FcmMessageService extend FirebaseMessagingService then Override onMessageReceived to handle the received message.

It handles receiving notifications, receive data payload, to send upstream messages, etc.

3. Add both services to the manifest

Send Message from firebase console

1.Go to firebase console, login with your gmail account.
2. Open your project (We created before) … then click Notifications from the side panel.

firebase-console-send-message

3. In the Notifications page… Click New Message
4. In the compose form enter Message text and chose the appropriate target…

Target
User SegmentTarget a single or multiple app
TopicTarget a Topic of the app
Single deviceTarget a single device (Enter FCM registration token generated in onTockenRefresh)

5. Finally, click SEND MESSAGE and SEND if prompts.

That’s it for Android Section … Here is the PHP Server Side Implementation to Send FCM Messages to Android Device.

 

sab99r

 

5 thoughts on “Firebase Cloud Messaging (FCM) Quick Set Up in Android

  1. thank you for the post, but you did not post the code for handling the notification on receipt in MainActivity.
    Plus, i would like to ask about the difference between using tokens or topic, for target, if i subscribe the app instance to a topis “exampletopic”, then it’s enough to set the $target to ‘exampletopic’, and then all devices that have this app will receive the notification??
    in OnCreate class i added the following line:
    FirebaseMessaging.getInstance().subscribeToTopic(“exampletopic”);
    Thank you.

    1. Yes, All the device subscribed to the topic will receive will get the notification. use broadcast receiver to notify the MainActivity when receive the notification.

Leave a Reply

Your email address will not be published. Required fields are marked *

three × one =