How to add firebase cloud messaging in Android App

Steps to add firebase cloud messaging in android app
  
A) Add firebase to your app

    1. Create firebase project from firebase console. If you have Google project associated to your android app then just click on import Google project option otherwise proceed with new project option in firebase console.
    2. If you are clicking on import Google project option then everything will be configured  by firebase, you just need to follow steps and lastly download the config file.
    3. If you click on new project option, then new project will be created. Your application is  recognized by package name entered in project name field in create project wizard.
    4.    At the end download google-services.json file and paste this file in your android project's app folder.
       
B) Add firebase sdk

    1. In your root level build.gradle file add classpath in dependencies section :
        dependencies {
            classpath 'com.google.gms:google-services:3.0.0'
        }

    2. Then add apply plugin:'com.google.gms.google-services' at the bottom of the file in your app level build.gradle file.
    3. Add compile 'com.google.firebase:firebase-core:9.2.0' in your app  level dependencies.

C) Adding fcm to android project

    1. Add  compile 'com.google.firebase:firebase-messaging:9.2.0' to your app level dependency.
    2. Create one service class extending FirebaseMessagingService class. Declare this service class in android manifest.xml file as below :

            <service android:name=”.MyFirebaseMessagingService”>
       <intent-filter>
       <action android:name=”com.google.firebase.MESSAGING_event”>
       </intent-filter>
     </service>


    3. Create another service class extending FirebaseInstanceIdService class which handles creation ,update , rotation of registration token. Declare this class as below in androidmanifest.xml file.

    <service android:name=”.MyFirebaseInstanceIdService”>      
       <intent-filter>
       <action android:name=”com.google.firebase.INSTANCE_id_event”>
       </intent-filter>
    </service>


    4. You can get token in onTokenRefresh() in your MyFirebaseInstanceIdService.
    5. Now you can send messages from your project in firebase console. For that just go to your project in firebase console which you have created earlier for fcm notification .
    6. Open the project and select notification option present in firebase console to send whatever   messages you want for your app notification by selecting package name for your application.
    7. There are two options for sending push notifications:
        i) Single device (firebase token is needed for user identification)
        ii) All applications with same package name.
    8. Right now only single line notifications can be sent to the devices.

Podcast

Michael Patterson sat down with the CEO of Boston Byte, Mustapha Shaikh to discuss the significance and rapid digitization of the healthcar...