Android Push Notifications using Firebase Cloud Messaging (FCM)

In this tutorial, we are going to discuss about Implementing Android Push Notifications using Firebase Console. In the upcoming tutorials we will be discussing about more advanced features of FCM using our own server instead of Firebase Console.

The complete code for Android Push Notifications using FCM can be cloned/downloaded from Github Link

(Note: In order for the above code to work, you need to replace your google-services.json file generated when firebase proj is created.)

1.    Adding Firebase to our Android Push Notifications Project:

a. Firstly we will be adding Firebase to our Android Push Notifications Project. Head on to the Firebase Console and click on Create a New Project.android push notifications creating project

b. Next you will be shown the following Firebase Overview tab.android push notifications - firebase overview image

c.       Since we are trying Firebase to our Android App, we will be clicking on Add Firebase to Android App.

d.       Next you will be shown a screen which will be asking you for your Android Package name which can be obtained from the manifest file.

To get the SHA1 key directly from Android Studio, you can refer to the following answer in stack overflow.

e.      After entering the required info and clicking on ADD APP we will be getting a json file which we will be placing in our project’s app folder.

android push notifications- adding google-services.json

f.       Now open the Project’s build.gradle file and include the google-services classpath to dependencies.

Now your project’s build.gradle looks similar to this:

g.       Then open your module’s build.gradle ( by default it is app module’s build.gradle which is located in app folder) and add the apply plugin line at the bottom of the file to enable the Gradle Plugin as shown in the code below:

h.  Next we will be extending two services for our Firebase Cloud Messaging Android Push Notifications:

1.    FirebaseInstanceIdService: When your app is started for the first time, Firebase SDK generates a token which can be accessed by extending the above service. It is used to send target messages to single / group of devices.

As this token changes in certain cases like clearing the data, reinstalling app etc., we can access the token by calling FirebaseInstanceId.getToken(). When the token is refreshed, onTokenRefresh() will be called where we can send the new token to our server to register the device.

In this project, I’ve created a Custom class called CustomFirebaseInstanceIdService.java which extends FirebaseInstanceIdService which is mentioned below:

2.       FirebaseMessagingService: required to handle registration tokens’ creation and updation. This is called when a message is received to set the custom icon, custom ringtone.

Here I will be placing my custom ringtone in res>raw folder and write the following code extending FirebaseMessagingService to receive the following notification with custom notification and custom icon:

i.       Next I will be declaring the above two services in my Android Manifest file which will now look like follows:

Now we have successfully integrated Android Push Notifications in our Project using Firebase Cloud Messaging. Now open your Firebase console and click on Notifications in the left panel and click on New Message

Type the message you want to send and you would be successfully sending the push notifications via FCM.

In the upcoming tutorials, I would be mentioning the Advanced version of FCM using server implementation, etc.

Vamsi Tallapudi
Connect Via

Vamsi Tallapudi

Founder and Author at Code Refer
Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook 🙂
Vamsi Tallapudi
Connect Via

Related Posts

Vamsi Tallapudi

Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook :)

You may also like...

  • Ranjan

    I got this error
    D/AndroidRuntime: Shutting down VM
    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: pdfdownload.acesoft.com.finalnoti1, PID: 2931
    java.lang.RuntimeException: Unable to instantiate service pdfdownload.acesoft.com.finalnoti1.MyFirebaseInstanceIDService: java.lang.ClassNotFoundException: Didn’t find class “pdfdownload.acesoft.com.finalnoti1.MyFirebaseInstanceIDService” on path: DexPathList[[zip file “/data/app/pdfdownload.acesoft.com.finalnoti1-2/base.apk”],nativeLibraryDirectories=[/data/app/pdfdownload.acesoft.com.finalnoti1-2/lib/x86, /vendor/lib, /system/lib]]
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2862)
    at android.app.ActivityThread.-wrap4(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: java.lang.ClassNotFoundException: Didn’t find class “pdfdownload.acesoft.com.finalnoti1.MyFirebaseInstanceIDService” on path: DexPathList[[zip file “/data/app/pdfdownload.acesoft.com.finalnoti1-2/base.apk”],nativeLibraryDirectories=[/data/app/pdfdownload.acesoft.com.finalnoti1-2/lib/x86, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2859)
    at android.app.ActivityThread.-wrap4(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Suppressed: java.lang.ClassNotFoundException: Didn’t find class “pdfdownload.acesoft.com.finalnoti1.MyFirebaseInstanceIDService” on path: DexPathList[[dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-support-annotations-24.0.0_f1922869db228c702239fe54488501c67353de9d-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_9-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_8-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_7-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_6-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_5-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_4-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_3-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_2-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_1-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-slice_0-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-internal_impl-24.0.0_ea347b886ce00961cde97a2865177ebe7de7c2bb-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-com.google.firebase-firebase-messaging-9.6.1_ffababe805b07d86a128682100e7838c215a47a6-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-com.google.firebase-firebase-iid-9.6.1_9f3815dd9987147a909c29bac63fb3ccae9ff29b-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-com.google.firebase-firebase-common-9.6.1_d015261773f8d76080ce7baa6145c3234197bb5c-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-com.google.firebase-firebase-analytics-impl-9.6.1_490a2947b198709d6f7eafca6c666d2a30e33e4e-classes.dex”, dex file “/data/data/pdfdownload.acesoft.com.finalnoti1/files/instant-run/dex/slice-com.google.
    I/art: WaitForGcToComplete blocked for 29.950ms for cause Background
    Application terminated.

  • adam adamski

    Great job! Do you know how to make working if phone is in sleep mode (turn off screen) ?