Push notifications allow business owners send various information to users even if the mobile application is not running. Furthermore, it is a cheaper marketing channel than traditional SMS. So how to send a notification to an Android app using Firebase?
According to the report prepared by the analytics company called Localytics, which also runs a mobile marketing platform, 52% of smartphone users opt-in push messaging on their mobile devices. The statistics shows that push notifications drive 88% higher user engagement and more app launches on average on mobile devices with enabled push messaging. Moreover, such notifications drive 278% higher engagement for eCommerce mobile applications. Push messaging is obviously a very effective promotion tool.
The study provided by Urban Airship shows that those app publishers that do not send push notifications to users waste 95% of the Acquisition cost. First 90 days after installation are crucial for assuring smartphone owners to continue using your app. The sooner you begin to notify your users and the more frequently you do it, the more chances your app will not be deleted. According to Urban Airship’s report, sending push messages on a daily basis during the first week since the first Android app open drives an 80% user retention rate on average. So, in our tutorial, we will describe how to make a push notification in Android with the Firebase platform.
What is Firebase?
Firebase is a free platform that allows to send push notifications to mobile devices. Its full name is Firebase Cloud Messaging (FCM) and it is created by Google. Many beginner developers call it a framework, which is not actually. In 2016, Google released new platform improvements that simplify the work with this useful tool. It has become more developer-friendly and easier to use.
- Sending data messages;
- Sending notification messages;
- Versatile message targeting;
- Sending messages from client apps.
Another advantage of this platform is that it provides instruments for sending messages for both Android and iOS mobile operating systems at no price. It also allows to create and send both collapsible and non-collapsible notifications. In this tutorial, we will show how you can create a Firebase push notification in a detailed manner and how to send it to Android-based mobile devices.
Sending Push Notifications to Android Devices: Project Configuration
To start using the Firebase platform, you need to do the preparation work. At first, download and install the latest version of the integrated development environment called Android Studio. Then create an account in Firebase.
Go to the Firebase console and do the following:
- Click on “Create New Project”.
- Set a project name.
- Choose your country.
- Click on “Create Project”.
- Choose the “Add Firebase to your Android app” option.
- Enter a package name.
- Click on “Add app”.
After pushing the “Add app” button, the google-services.json file will be automatically downloaded. Open the root folder of your application and replace the google-services.json file you have just downloaded.
Gradle Files Configuration
In Android Studio, choose the option called “build.gradle (Project: YourAppName) in the Gradle Scripts collapsible list. Then add the following dependency classpath ‘com.google.gms:google-services:3.0.0’ to the build.gradle file of your project folder. Open another gradle file following the path: Your Project Name/Your App Module and add the command that will call the Google’s plugin: apply plugin: ‘com.google.gms:google-services’.
To add push notifications to an Android app, enter the dependencies that will allow to integrate with Firebase and use FCM: compile ‘com.google.firebase:firebase-core:9.2.0’ and compile ‘com.google.firebase:firebase-messaging:9.2.0’. And at last, proceed to the defaultConfig sections of your gradle file and add the ID of your application there: applicationId “com.sample.your.application”.
Adding App Services
To implement FCM, you need to add the following services: a push notification testing code and a code that handles sending/receiving messages.
Extending the Firebase Messaging Service
At first, set a service that extends the Firebase Messaging Service.
The logic must be as the following:
- Implementing the FCM service;
- Handling both notification and data messages if the app is in the foreground;
- Displaying the sender identification and message body.
Open the Androidmanifest.xml file and add the following sample code there:
Extending the Firebase Instance ID Service
Then add a service which extends the Firebase Instance ID Service like you have made it with the Firebase Messaging Service. Using the same analogy, update your Manifest file by adding the new service.
Creating Android Push Notifications Using Firebase: Sending the Message
When the configuration is done, you may proceed to sending your first test message via Firebase. Go to the Firebase console and choose the menu option called “Notifications”. Enter the text of your test message and message nickname. Choose a delivery date (for a test, it is better to choose “Send Now” option) and a recipient. In our case, a target is the specific app we have created before (we have updated its AndroidManifest.xml file in the tutorial above). Then click on “Send Message”.
When your mobile application is running on the background, your push notification will show up in the notification center of your smartphone. Remember that it can take up to several minutes for notifications to be received. When a test message is received, you can learn how to send notifications to Android devices using advanced message settings.
Setting the Needed Time
Besides the “Send Now” option, which you have selected for your test message, there is the other option available: “Send Later”. It allows to send notifications on the specific date and at the specific time you need. Furthermore, you can select a time zone to ensure that users, who are situated in on the other side of the world, will not receive your message at night time.
Targeting the Right Audience
For startup owners, it is often necessary to send push notifications to a specific audience but not to all app users at once. The good news is that the FCM provides a possibility to target different users offering such parameters as a single device, topic, and user segment.
Sending Messages to a Specific Device
To send a message to a specific device, you have to get your FCM registration token (ID). To get it, open the Android Monitor in Android Studio right after you have added the service that extends the Firebase Instance Id Service. You will find your instance ID there. You may also display the FCM registration token every time you open the application by setting the following command in the onCreate() function: Log.d(“FCM”, “Instance ID: ” + FirebaseInstanceId.getInstance().getToken());.
To get the ID from server data, you can initiate sending the token to it using the “sendRegistrationToServer” function in the section where you have extended the Firebase Instance ID service. Just paste the ID to the FCM registration token field and click on “Send Message”.
Sending Messages to the Users Who Have Subscribed to a Specific Topic
To create a push notification for Android-based devices and send it to those users who have subscribed to a specific topic, you need to add the following line to your application code:
FirebaseMessaging.getInstance().subscribeToTopic(“News”); This piece of code will allow users to subscribe to the topic called “News”. You can set your own topic name. It is worth noting that it usually takes one day for your topic to appear in the Firebase console. So, select a “Topic” option in the “Target” section and click on “Send Message”. You may notice that the system calculates an approximate number of subscribed users.
Segmenting the Audience
The FCM provides various filters to target an audience more precisely. Available filters:
The first filter called “Audience” allows to send messages to those users who have made any purchases in your application. The “Version” filter lets select those users who use a specific version of your mobile applications. The last filter allows to target users by the language they have chosen on their device.
The “Advanced Options” block allows to set a unique title and priority for you push notification, enable or disable a notification sound, choose the expiry period, and add some custom data.
- Compilation errors may occur when you set the wrong versions of the Firebase core or messaging.
- If you use the Android Monitor log in Android Studio to see the test notification you have sent, you can get “FirebaseCrash is not linked”. In this case, you can ignore this error because you might not use the Firebase Crash Analytics service.
- If the Firebase app is not initialized, you should check whether you have added the Firebase core dependency to your manifest file besides the Firebase messaging one. If you have done it, then remove the tools:node=”replace” attribute from the application tag in the Manifest file.
Now, you see that creating and sending Android push notifications using Firebase is not that difficult as it may seem at first. Most developers with at least basic knowledge in coding for an Android platform will easily manage this task by following our checklist.
If you still need any assistance with sending notifications to mobile devices through Firebase or mobile app development in general, feel free to contact us at email@example.com and we will be more than happy to help. Generate more sales by using the most effective mobile technologies with Lunapps!