Back

How to Send a Push Notification to Android Using Firebase

Down

In 2014, Google acquired Firebase, Inc. As you can imagine, such successful corporate giants as Alphabet (the holding company that owns Google) would never purchase a company, regardless of price, just for the sake of it. In this case, the obvious reasoning behind the deal includes the promise that Firebase technologies has.

What did Andrew Lee and James Tamplin - the founders of the company - do to create something so simple and ingenious? Firebase is a provider of cloud services and technologies platforms. In other words, Firebase is a cloud-based framework and NoSQL database for mobile applications which, through the API, allows development companies to store and synchronize data between clients.

In 2016, Google announced the new and significantly improved Firebase product, refined and filled with additional useful features. One of the new components is Firebase Cloud Messaging (FCM).

FCM is a tool for the exchange of free messages using cloud technologies. FCM is fast, reliable, and most importantly – supports cost-free delivery of your messages, which is great for startups and established businesses, alike.

Of course, sending messages using cloud technologies is not a novelty; Google has a service they created years ago and their users still use, today - Google Cloud Messaging. Google developers, however, have concluded that FCM is a more convenient service - the major reason being that you do not need to know or see the server code to send messages through FCM.

We are a company with extensive experience in outsource software development for enterprises of different scales, from the smallest startups to industrial giants. User experience design, mobile apps of varying caliber, websites, testing - anything you can think of - we have, at least, tried it. While most of our projects are developed under strict NDAs, thus making them unavailable to preview, you can still review showcases of open projects on our site.

We find it appropriate and helpful to be able to share our experience and knowledge with our readers. This article, for instance, is a manual that will help you understand how to send a push notification to Android using Firebase. We'll tell you how to use the new version of Firebase and configure the sample app to send notifications to Android.

How to Send a Push Notification to Android Using Firebase

Firebase Push Notification: Who Might Need It, and Why?

First of all, it is worth understanding what Firebase push notification actually is. Essentially, push technology is a way of transferring messages from server to user in a forced or timed manner. Most often, these technologies are used for mass distribution of news or useful information to application users.

Of course, sending push notifications to Android devices, though useful, is a very specific service which may not be necessary for everyone. So, who would benefit from this instruction? There are several categories of users who would value the possibilities provided by FCM and, if you are reading this article, most likely one of the following statements is true about you:

  • You are already using an application that employs the Google Cloud Messaging service. In this case, FCM should be considered a more advanced version of messenger with additional functionality;
  • For some reason, you are not satisfied with your current push notification service provider and you are ready for a change;
  • You are developing a new application for Android and are going to implement the push notifications sending and receiving service.

It is also possible that you are simply interested in innovations within the IT field. In any case, we hope that this tutorial with sample codes will be useful to you.

Android Push Notifications Using Firebase: How is it done?

Firebase gives developers the ability not only to send notifications but also to transfer point-by-point messages, the recipient of which can be a single device. How to send a notification to Android app?

What Do You Need to Start?

To start using the Firebase Cloud Messaging service in your application, you will need:

  • The device that uses Android OS version 4.0 and higher;
  • Google Play version 10.2.1 or higher;
  • Google Play services SDK (available in the Google Repository in the Android SDK Manager);
  • Android Studio version 1.5 or later;
  • Firebase account; and
  • To generate the application instance registration token at the used device.

As you can see, all the necessary elements are in the public domain. Having received the required components, it is possible to start implementing FCM in your application. If you want to experience the capabilities of FCM without creating the new Android Studio project, you have the option to simply download one of the quick-start samples.

Let Us Help

Although setting up the FCM service looks like something complicated, it is actually done in five simple steps. If you follow the instructions below correctly, the whole procedure for starting the service will take no more than half an hour. So, let's get started!

Step One: Connect a Project to Firebase

To add push notifications to Android apps, you first need to create a new project, or import an existing project, in the Firebase console. To create a new project, specify its name and country (in this case we used the Firebase demo project). The next step will be adding Firebase to your Android application (done by selecting the appropriate item in the console menu) and specifying the name for the package of your application. Be sure to download the supplied google-services.json file.

Step Two: Copy the Needed JSON File

As mentioned above, google-services.json is an important file that must be copied into the application folder. To do this, place the downloaded google-services.json in the application folder, replacing the existing file, if needed.

Step Three: Configure the Build Files

In order to use Google services plug-ins, you’ll need to edit the build file in Android Studio. To do this, open Android Studio, update the build.gradle project (the one that is situated in the application folder) and add to it, as follows:

buildscript {

dependencies {

classpath 'com.google.gms:google-services:3.0.0'

}

}

Add the following line to the end of the build.gradle file:

apply plugin: 'com.google.gms.google-services'

To the same file, we add the Firebase dependencies:

dependencies {

compile 'com.google.Firebase:Firebase-core:10.2.1' //The string required for Firebase integration

compile 'com.google.Firebase:Firebase-messaging:10.2.1' //The string required to make Firebase Cloud Messaging available

}

Then, update the services using com.google.android.gms: play-services;

To add Firebase to an existing project that uses gms play-services, you must update all the modules. The correctness of the version can be checked as follows:

compile 'com.google.android.gms:play-services-location:9.2.0'

compile 'com.google.android.gms:play-services-places:9.2.0'

Finally, add applicationId to defaultConfig:

android {

defaultConfig{

applicationId "com.example.my.app" //your app’s id

}

}

Step Four: Add the Service to the Application

To use FCM in the application, two services must be added to it: the notifications operation testing code, and code for the processing of received/sent messages and their integration into your design.

To receive notifications, we must add a service that extends the FirebaseMessagingService capabilities:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "FCM Service";

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.d(TAG, "From: " + remoteMessage.getFrom());

Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

}

}

Next, add the service to AndroidManifest.xml:

<service android:name=".MyFirebaseMessagingService">

<intent-filter>

<action android:name="com.google.Firebase.MESSAGING_EVENT"/>

</intent-filter>

</service>

Add the service that extends FirebaseInstanceIdService:

public class FirebaseIDService extends FirebaseInstanceIdService {

private static final String TAG = "FirebaseIDService";

@Override

public void onTokenRefresh() {

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Log.d(TAG, "Refreshed token: " + refreshedToken);

}

private void sendRegistrationToServer(String token) {

}

}

Provide the download service by adding the service to the AndroidManifest.xml file:

<service android:name=".FirebaseIDService";>

<intent-filter>

<action android:name="com.google.Firebase.INSTANCE_ID_EVENT"/>

</intent-filter>

</service>

Step Five: Check and Send the First Push Notification

All of the basic steps have now been followed and, if done so correctly, you will now be able to create a push notification for Android. It's best to send a test message to your own device in order to detect any possible errors. Start the test mode, write a message, select the application and click "send.” If everything is done correctly, you will receive your first push notification within minutes(yes, yes - you may have to wait a bit - do not get nervous and fix the code ahead of time. Patience is key!). If the application has connected to the FCM service correctly, you will receive a notification in the debug console. If not, the error message will appear in the Android Monitor log.

Pay attention to the little things: it is important to not only check whether the text of the message is fully transmitted, but also how it relates to the design of your application. If all services are installed and integrated correctly into the application, then the appearance of Android push notifications using Firebase will match the design.

Most of the errors and inconsistencies when sending/receiving notifications are associated with outdated service versions. If an error occures, check for updates and try again.

You might see a message in your Android Monitor log that looks like this: "com.google.Firebase.crash.FirebaseCrash is not linked. Skipping initialization." This error is not critical and appears due to the fact that you do not use the Firebase Crash Analytics service, and its initialization simply did not happen.

How to Send a Push Notification to Android Using Firebase 2

What other useful features does Firebase have?

If you have read the article up to this point, it means (at least, we really hope so) that now you know how to make a push notification in Android using FCM. Cloud Messaging, however, is not the only useful Firebase service. Developers from Google are constantly improving their products and, since 2016, the following services have become available to users:

  • Firebase Storage allows users to easily organize the storage and uploading of files;
  • Firebase Remote Config gives developers the ability to upgrade and change application elements without forced updates;
  • Firebase Authentication is a set of reliable security improvement tools;
  • Firebase Crash Reporting is a service that captures and logs the debugging information useful for finding errors in an already-released application;
  • Firebase Test Lab is a service that allows the cloud-testing of applications with real devices located in Google’s data centers;
  • Firebase Notifications notify users without affecting the source code of the application;
  • Firebase Dynamic Links contain all the instruments required for link processing; its two main areas are control over the reliability of links (now they can lead to a specific place inside the application) and their dynamism (links change their direction depending on the environment, which avoids the repeating requests);
  • Firebase App Indexing is a service that helps users find and receive results from your application. It also helps attract new users to your product.

Despite the fact that Firebase is already an excellent service with a large set of useful tools and services, Google developers are not going to stop there. The company is constantly developing its products and services. Online is a whole section of their site, created specifically for uploading error reports, as well as proposing the suggestions for new Firebase improvement ideas. Specialists from Google carefully study both reports and user reviews about their products, as well as promise to "take note" of all new ideas from their users. We can only continue to use simple and convenient Firebase services and wait for new improvements to these services in the next versions.

We hope this article was useful for you. In case your project needs developers' help, don't hesitate to contact us!

Article Rating

119 Reviews
4.3 / 5.0

We hope you enjoyed this article! It's very important for us to receive your feedback. You can use these emojis to describe your feelings.

  • 5
  • 4
  • 3
  • 2
  • 1
 
 
 
 
 
 
Request a quote
 
 
 
 
 
 
prev next
Be the first to receive helpful tips from Applikey
Please enter correct email address
Fasten your seat belts, we are taking off