Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user reengagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app. Firebase Cloud Messaging (FCM) offers a broad range of messaging options and capabilities. The information in this page is intended to help you understand notification messages and data messages and what you can do with them using FCM’s options.

With FCM, you can send two types of messages to clients:

  • Notification messages, sometimes thought of as “display messages.”
  • Data messages, which are handled by the client app.

 

Setting up Firebase account

  1. Go to — https://console.firebase.google.com
  2. Login with the account of Google developer console.
  3. Click on Add Project

    Firebase Add new project
    Add new Project
  4. After project creation is completed, you come to a dashboard screen where you can connect Firebase to iOS, Android or Web App. Select Add Firebase to your Android App
  5. To add an Android project, you have to specify the following data
    1. App Package name
    2. App Name
    3. Debug SHA
      Add Firebase to Android
      Add Firebase to Android

       

  6. Once this is done, you will get the link to download a google-services.json file. Download this file and copy it into the app  directory of your Android project
    Google-service.json
    Google-service.json

Setup Firebase in Android Studio

  1. To the Module App build.gradle file  add the lines in dependencies

     

  2. Add this to the last line of Module app build.gradle file

     

  3. Add this line to Project level build.gradle file in dependencies

     

  4. In Manifest file, dont forget to add Internet permission

     

To use Firebase notifications, you require 2 important java files :-

  1. FirebaseIDService.java – This file receives a unique firebase token when the app starts for the first time or when clear data is performed on the app. This unique token is used to send notification to a particular device. This token gets automatically registered on the Firebase server. To send custom messages yourself from your server, you need to save this Firebase token in your server database. Tag this to your UserId or DeviceId to send customised messages to users.
  2. MyFirebaseMessagingService.java – Whenever a Firebase Notification is received on the phone, this class is called and the function”OnMessageReceived(RemoteMessage remoteMessage)” is called. Use this method to customise the incoming message as per your requirement.

 

FirebaseIDService.java

SaveTokenToServer() is our custom method which we use to save our unique token on our server.(optional)

Using this code, we sent out our UserId and FirebaseToken to be saved on our server. The code for PHP is not shown as you can choose to write it in your own format as and when required. This is an optional part.

 

MyFirebaseMessagingService.java

Lets breakdown the above code line by line

OnMessageReceived gets called everytime the phone receives a message.

P.S. – If you send message from the firebase console, then OnMessageReceived only gets called if your app is not open currently on the phone. Otherwise an internal message function is called whcih cannot be customised. If you send notification message from your own server then this message is always called

We call our custom method sendNotification to breakdown the incoming message which looks like this :


  1. “activity” is sent as a data payload in the message to specify the String name of the Activity which we want this message to open. If we dont specify this, the launcher activity of the app will be opened upon clicking the message. The String name of Activity is put inside the <intent-filter> inside the Manifest.xml file

  2. This code takes care of a large image if you want to send a Large image notification
  3. The rest of the parameters in NotificationCompat.Builder is used to specify the designs of the notification tab.
Android Notification
Android Notification

Updating the Android Manifest

Finally for this code to run – we need to set the above two classes in the Android Manifest File

add the below code in your Manifest file inside application

 

Send your first Firebase message

Go back to firebase console inside your app and click the notifications tab on the left Menu bar

Click on New Message button and add your custom message and click on SEND MESSAGE

Firebase Message
Firebase Message

 

Part 2  Send custom message to Android phone using PHP

 

 

 

4 comments
  • hackgamemobile. com
    October 25, 2017 at 6:11 pm

    Great, this is what I was looking for in bing

    Reply
    • Siddhant Chandgothia
      November 9, 2017 at 5:52 am

      Great. Happy to help

      Reply
  • busy 17 5.1
    October 29, 2017 at 7:35 am

    This is nice!

    Reply
    • Siddhant Chandgothia
      November 9, 2017 at 5:52 am

      Thanks

      Reply

Leave a Comment

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