Sending a push notification is every developer need, in order to re-target the dormant users. In this tutorial, I will be covering that exact thing which is “How to send push notification using Firebase in Android”
Firebase is really time-saving Google product for developers, from collecting data to sending a notification to your users, Firebase provides pretty much everything.
The end result of this tutorial:
What you will learn:
- 1 What is Push Notification?
- 2 What is FCM (Firebase Cloud Messaging)
- 3 How to send and receive push notification using Firebase in Android?
What is Push Notification?
Push notification is some simple messages that appear with a notification in the notification tray or like a popup messaging – depends on the platform. Push Notifications are usually pushed from some backend server like Firebase in this case. It is the way to deliver users a messaging without opening the app. It is a highly effective tool for developers and advertisers to re-target/re-engage your dormant users.
Push notifications boost app engagement by 88% (Source: Localytics Data Team)
What is FCM (Firebase Cloud Messaging)
Firebase Cloud Messaging is the new version of Google Cloud Messaging or GCM. It is a cloud cross-platform messaging solution. You can use Firebase Cloud Messaging to any kind of end-user device including iOS, Android or even Web at no-cost. Firebase allows two types of messages
- Notifications Messages, which are automatically handled by Firebase SDK. All you need to do is implement the Firebase SDK and start sending messages from Firebase Console
- Data Messages, which are handled by the client app.
NOTE: In this tutorial, we will be sending notification message only.
How to send and receive push notification using Firebase in Android?
Things you need to get started
A Firebase account (you can use the same Google account – no extra sign up required)
A real Android Device or an emulator to test your Android App
Create Application in Android Studio
- Open Android Studio and create a new project.
- Enter the app details – app name, domain name, location – and leave the rest as default i.e the empty activity and minimum Android version. In my case, the package name of my app is ‘com.firebasetutorials.testingnotification’ but you can have one of your choices.
- After Android Studio finishes the building App for you, leave it as and move to next step, we will get back to Android Studio soon.
Setup Firebase for Push Notification
There are 3 steps to Setup Firebase for your Android app notification.
Sign up for Firebase
Before anything, you need a Firebase account. Click here and signup for Firebase Console or sign in using your Google account.
Now visit Firebase Console and hit ‘Add Project’. Enter Project name, select Country/region. And hit ‘Create Project’
Register App with Firebase
There are three simple steps to get your Android App Registered with Firebase
After your first project completed you will see a page like below. Hit ‘Add Firebase to your Android App’
Enter ‘Android package name’. In my, case it is ‘com.firebasetutorials.testingnotification’ – you can enter one of your choices.
NOTE: Package name should be exact same as the app package name, otherwise it will not work.
Enter ‘App nickname’. I am entering ‘Testing Notification App’ – you can enter anyone, it has to do nothing with your app.
The third field is for ‘Debug signing certificate SHA-1’ you can skip it at this point as we are not going to use Firebase Authentication.
Hit ‘Register App’ to get your app registered with Firebase
Download Config Files
Hit ‘Download google-services.json’ button to download a JSON file. Save the JSON file under the app directory of your Android Studio project we recently created.
Add Firebase SDK
After downloading the JSON file under App Directory of your project hit Continue on Firebase Console Dialog.
In Android Studio, open build.gradle file. You will find it in the project directory. Not the one that is in your app directory. Add the following line of code inside dependencies,
After adding the dependencies your project will look like this
After adding this, you will see a yellow message, asking you to sync the project. You can ignore it at the moment. Now head over to app level build.gradle file. You will find it under app directory of your project.
Add the following lines of in dependencies section
compile 'com.google.firebase:firebase-core:11.8.0' //Firebase Messaging library for Sending push Notifications compile 'com.google.firebase:firebase-messaging:11.8.0'
And at the end, outside of the dependencies section, add the following line and hit ‘Sync Now’
apply plugin: 'com.google.gms.google-services'
And your build.gradle file will look like this:
That’s all we need to do in Android Studio.
Send Notification from Firebase Console
Get back to Firebase Console. Open the Firebase project, on the left pane select ‘Notification’ or click here
Hit ‘SEND YOUR FIRST MESSAGE’
And you will see ‘Compose Message‘ form, where you can enter the details.
In ‘Message Text‘ enter the message that you want to be displayed in Notification bar of your Android App
In ‘Message Label‘ you can enter the message nickname, it has to do nothing with the notification at the client side, it is just for saving purpose in Firebase Console.
In ‘Delivery Date‘ drop-down, you can select the delivery time, If you select ‘Send Now‘ your message will be sent and displayed instantly. If you select ‘Send Later’ then some more option will be displayed asking for when you want to send the notification to your users.
You can mark the ‘Recipient Time Zone‘ option and your message will be delivered according to the recipient time zone or you can provide any custom timezone.
Recipient Time Zone is very useful because sometimes we want to send notification at some custom event like New Year, so due to timezone difference, your message can be shown to users before New Year and making them feel annoying. So it’s better to use ‘Recipient Time Zone’ option in that kind of notifications
In this tutorial, we will select the ‘Send Now’ option to make the notification sent right now.
In the target section, we can target a custom range of users depending on their behavior and much more. We will be covering that all in a whole new section.
In the next drop-down, select the app package name. You can select multiple apps at the same time but they all need to be on the same project.
After that, there are ‘Conversion events’ options, here you can track the analytics data of your notification. The ‘Sent‘ and ‘Opened‘ are added by default but you can add more. Sent and Opened are shown in Notification Section in Firebase to show you how many users received the notification message and how many of them opened it. We are not adding conversion event for the sake of this tutorial.
At the end, there are few ‘Advanced Options’ but we will keep them default. Those are for Data Messages, we will cover that all in details later.
Now hit, ‘Send Message‘ to make the Notification sent to the client app.
Your Notification will be displayed on your Android Phone, and if you tap on that notification it will open the app.
You can watch the video below if you don’t understand the text