Tutorial: How to Send Push Notifications to Android Device

For now, push notifications seem to be the easiest way for startups with an app to get to the end user. The advantage of such messages is that customers don’t have to use an application to get the latest news from it. This way, a company can support the custom’s engagement, speak directly to each person, and there is a big chance for the message to be read.

Apple was the first to present the concept of push notifications to the IT industry in June 2009. In less than a year, May 2010, Google developers shared their own achievement presenting Google Cloud to Device Messaging (C2DM). Since then, push notifications have become available on all platforms including Fire OS, Windows, and Blackberry. Such a fast growth proves the importance of this new concept for businesses.

Google’s innovation of nowadays is a cross-platform solution called FCM (Firebase Cloud Messaging) which allows delivering messages at no cost. It is a new generation of push notifications’ sending, as Google states:

“...GCM users are strongly recommended to upgrade to FCM, in order to benefit from new FCM features today and in the future”.

So, what does it mean specifically for Android developers? It means sooner or later FCM will be a part of your everyday life as some features of GSM are becoming depreciated. From now on, all new client-side upgrades will be added to FCM, and you have to move forward with them. In other words, implementing push notifications on Android means dealing only with Firebase Cloud Messaging.

Basic Types of Messages in FCM

With FCM, you can operate with two types of messages: notification and data messages.

Notification messages (or display messages) contain an optional data payload which becomes available when users tap on the notification. This type of messages is more lightweight with a 2 KB limit and used when you want FCM to display a message on your client’s app behalf.

Data messages allow processing notifications on the client app. Developers can send up to 4 KB of key-value pairs.

Knowing the abilities of Firebase Cloud Messaging for Android, you can start off with building a new application and creating a Firebase account. Below, there is a 3-step tutorial on Android push notifications.

I. Building a new Android application and setting up Firebase in it

1.In Android Studio, create a new Android Project.

Create new Android Studio Project

2. Give a name for your new project. In our example, it is “StartPush”. After that, click on “Next” button.

Configure your new project

3. It’s time to choose an activity now. We pick “Google Maps Activity” - this way, we’ll be able to send map coordinates with a push notification - more useful information instead of a simple text. This is a huge benefit of push notifications - you may tell about whatever you want and show it in details.

Then, click on “Finish” and wait till the studio generates a project.

Studio generates the project with Google map activity

4. This is how a console with your new application looks like now. Go to the section “Tools” on the right and choose “Firebase” to start working with messaging.

Choose Firebase in Tools section

5. When you’re there, click on “Cloud Messaging” - this is the reason we’re here.

Cloud messaging in Firebase

6. Clicking on “More info” below the “Cloud Messaging” button we can open a web console. It will be of use when we end up with all preparations and be ready to send the first push notification.

Cloud messaging web console

7. Anyway, let’s go back to the studio and click on the “Set up Firebase Cloud Messaging”.

Set up Firebase Cloud Messaging

8. Now we see a detailed instruction on how to add FCM to your project. Without any doubts, we follow these instructions.

Add FCM to your project

9. Begin with the first step - “Connect your app to Firebase”, click on the button below - “Connect to Firebase”. The studio would ask us to sign in via a Google account.

Connection to Firebase

10. Here we have a request from Android Studio. As we can see, Google would like to view and manage the details from your account and activity across GCM services.

Google account and Android Studio

11. When you agree, you’ll see a successful confirmation in the browser. It means now your activity in Android Studio is connected with Firebase.

Activity in Android Studio is connected with Firebase

12. When you’re a part of the Firebase community, the studio would suggest you creating a new project in the Firebase console:

Create a new project in Firebase console

13. We agree with that too filling in the name of our project “StartPush”. We see a confirmation in the studio then:

Firebase project connected locally

14. We’re done with the first connecting part, so, we may go to the second one - “Add FCM to your app”. Before sending push notifications to Android devices, the studio would suggest to add necessary changes to our application, we accept that.

Add Cloud Messaging

15. This is how the build.gradle file would look like now. These changes are added automatically.

build.gradle file

II. Accessing the device registration token (optional)

The third item would be useful if you’re going to send push notifications to specific devices or groups of devices. If you’re interested in the devices altogether, you may skip the item. In our Firebase Cloud Messaging tutorial, we’ll accomplish this step.

Again, we follow the instruction:

“...On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices, or create device groups, you'll need to access this token.

You can access the token's value by creating a new class which extends FirebaseInstanceIdService. In that class, call getToken within onTokenRefresh, and log the value as shown…”

1.We create a new class which we called FCMService and redefine the method onTokenPrefresh () as it shown below:

A new class - FCMService

2. Then, we add the service to the Manifest file:

The Manifest file

This is another part of the instruction:

“...The onTokenRefresh callback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. FirebaseInstanceID.getToken() returns null if the token has not yet been generated...”   

From now on, our server will know the token of a single device and will be able to send group or individual push notifications.

III. Releasing a receiver for push notifications

Let’s proceed to the item 4. In our application, we need to release a receiver for push notifications. Let’s turn to the instruction again:

“...If you wish to do any message handling beyond receiving notifications on apps in the background, create a new Service ( File > New > Service > Service ) that extends FirebaseMessagingService . This service is necessary to receive notifications in foregrounded apps, to receive data payload, to send upstream messages, and so on.

In this service create an onMessageReceived method to handle incoming messages…”

1. This is how we’ve created a new service according to the instruction (File > New > Service > Service):

New Service

2. This service includes the next data:

Data in Firebase Service

3. After that, we register the service in the Manifest file as we’ve done it in the second part of the tutorial:

Register the service in the Manifest

4. Now, we create a notification receiver in the activity we’ve chosen at the beginning - “Google Maps Activity”. So, we go to the activity section and add this code there:

Notification receiver “Google Maps Activity

IV. Sending push notifications via the Firebase console

1. On the last stage of this Google FCM Android tutorial, we go to the Firebase console (we’ve already seen it previously, on the 6th step, part I of this tutorial). It already has our project created in the studio.

Firebase console

2. Now, we enter our project which we called “StartPush” and go to “Notifications”.

Notification in Firebase console

3. We can try to send our first push notification in the Firebase console. Fill in the message field, then choose your application and click on “Send message” button.

Our first push

4. To add more information to a push notification, we could also fill in “Advanced options” before sending a message. With that goal, we picked “Google Maps Activity” on the stage of creating the application. In our example, we supplement basic information with the map coordinates of the shop with big discounts. After opening “Advanced options”, we fill in two blocks - “latitude” and “longitude”.

Advanced options

5. We would need to add some coding information so that our map could accurately display the coordinates from the push notification. You’ll find the whole example in the repository.

Coordinates on Google Map

And the work is done! Now, we see the coordinates we set before on the Google Map in our application.

Small Changes Make a Difference

Small notes that we used to throw each other during classes now have become a concept of electronic messages of all kinds. At first, we got SMS, then, messaging applications with the diversity of tools. And now, we have an opportunity to get the latest news from our favorite mobile products. This is how the evolutionary way to push notifications can be described.

When a company already has its own application, short messaging can be a small but necessary addition to the final success. Knowing how to implement push notifications on Android may be a good start as the majority of mobile devices works on this platform.

New Google’s solution called FCM promises a lot including cross-platform opportunities at no cost. So, without paying a high price, you get a better app’s performance, a chance to get in touch with users and, therefore, to achieve more attention to your work.

Push notifications at Tecsynt company

Read Next

How Bugs in the API Can Tell You Users’ Credentials
How Bugs in the API Can Tell You Users’ Credentials
How to Build a Mobile App with Machine Learning?
How to Build a Mobile App with Machine Learning?
7 Best Mobile Security Testing Tools for 2017
7 Best Mobile Security Testing Tools for 2017
Don’t leave us hanging!
[email protected]
Get in Touch