How to Send Push Notifications in Android Using Firebase

In this tutorial, you'll learn how to use Firebase to send push notifications in Android.

Send Push Notifications in Android

What is a Push Notification?

A push notification is a basic message that appears in the notification tray or as a popup message, depending on the platform.

Clients, such as Firebase in this example, receive push notifications from backend servers. It's a technique to send a message to users without them having to launch the app. For developers and advertisers, it's a powerful tool for re-targeting and re-engaging dormant people.

What is FCM (Firebase Cloud Messaging)

Google Cloud Messaging, or GCM, is a cloud cross-platform messaging system. GCM is a contemporary version of Firebase Cloud Messaging. Firebase Cloud Messaging is free to use on any end-user device, including iOS, Android, and the web.

Firebase allows two types of messages:

Notification Notifications. When the customer application receives the FCM message, it will behave differently depending on whether it is running in the background or in the foreground. This is one of the simplest techniques to send notifications to your consumers because the Android software assembles the information for you. If your application receives an FCM message while it is running in the foreground, it will not handle this telling automatically, leaving you to process the message on the onMessageReceived() callback of your application. We'll look into onMessageReceived() later in this tutorial, but keep in mind that if your programmed receives a message while it's running in the foreground, the message will not be displayed to the consumer by default.

Data Notifications, You can transmit specific data components to the consumer programme via info messages. However, those information packets are limited to 4KB by FCM, thus if your payload is more than 4KB, you'll need to use WorkManager or the JobScheduler API to get more data.

Send a Google Firebase Push Notification

With an example, here is a step-by-step explanation on how to build Firebase push notification:

1. Things You Need to Get Started

  1. Android Studio
  2. A Firebase account (you can use your existing Google account - no additional registration is necessary).
  3. To test your Android app, you'll need either a real Android smartphone or an emulator.

2. In Android Studio, make an application

  1. Create a new project in Android Studio.
  2. Fill up the app's details - app name, domain name, and location – and leave the rest blank, such as the empty activity and Android version minimum. My app's package name is com.firebasetutorials.testingnotification in my case, but you may use any of your options.
  3. Leave Android Studio after it has finished generating your app and move on to the next step; we will return to Android Studio soon.

3. Create a Firebase Push Notifications account

To set up Firebase for Android app notifications, follow these three steps:
  • Signing up
  • Creating a Project
  • Registering an App
1. Sign up for Firebase

To begin, you'll need a Firebase account. Login up for Firebase Console here, or sign in with your Google account.

2. Create a Project

Now go to the Firebase Console and click on the ‘Add Project' button. Enter the project's name and choose a country or region. Then press the ‘Create Project' button.

3. Register App with Firebase

To register your Android app with Firebase, follow these three simple steps:

Register App: When your first project is finished, you'll be taken to a page similar to the one below. Select ‘Add Firebase to your Android App' from the drop-down menu. Fill up the blanks with ‘Android package name.' It's 'com.firebasetutorials.testingnotification' in my case; you can choose one of your options. Fill up the blanks with your app's moniker. I'm going to put something in the 'Testing Notification App' field; it has nothing to do with your app. ‘Debug signing certificate SHA-1' is the third field. We won't be using Firebase Authentication, so you may skip it for now. To register your app with Firebase, click "Register App.".

Download Config Files: To get a JSON file, click the ‘Download google-services.json' button. Save the JSON file to the app directory of your newly created Android Studio project.

Add Firebase SDK: After downloading the JSON file to your project's App Directory, click Continue on the Firebase Console Dialog.

Open the build.gradle file in Android Studio. It's in the project directory, not the one in your app directory. Inside dependencies, add the following line of code: classpath 'com.google.gms:google-services:3.2.0'

After you've done that, a yellow notification will appear, requesting you to sync the project. For the time being, you can disregard it. Go to the app-level build.gradle file now. It'll be located in your project's app directory. In the dependents section, add the following lines.

compile 'com.google.firebase:firebase-core:11.8.0' //For sending push notifications, use the Firebase Messaging library. compile 'com.google.firebase:firebase-messaging:11.8.0'

Finally, outside of the dependents section, add the following line and press the Sync Now button. apply plugin: 'com.google.gms.google-services'

Send Notification from Firebase Console

Return to the Firebase Console page. Open the Firebase project and select ‘Notification' from the left pane, or click here.

Hit ‘SEND YOUR FIRST MESSAGE’

You'll then be taken to the 'Compose Message' form, where you may fill in the details. In the ‘Message Text‘ field, type the message you wish to appear in your Android app's Notification Bar.

You can input the message moniker in the ‘Message Label‘ field; it has nothing to do with the client-side notification and is only used to save data in Firebase Console.

You can choose the delivery time from the 'Delivery Date' drop-down menu. When you choose 'Send Now,' your message will be delivered and displayed right away. If you choose ‘Deliver Later,' you will be given further options for when you want to send the message to your users.

You can select the ‘Recipient Time Zone‘ option to have your message delivered in the recipient's time zone, or you may specify a custom time zone.

We'll use the 'Send Now' option in this lesson to send the notification right now. We can target a certain group of users based on their behavior and much more under the target area. All of this will be covered in a separate section.

Select the app package name from the next drop-down menu. You can pick many apps at the same time, but they must all be part of the same project. After that, there are options for ‘Conversion events,' where you may watch your notification's analytics data. The fields ‘Sent‘ and ‘Opened‘ are pre-filled, but you can add additional.

The numbers Sent and Opened are displayed in Firebase's Notification Section to illustrate how many users received and opened the notification message. For the sake of this tutorial, we will not create a conversion event. Finally, there are a couple "Advanced Options," but we'll stick with the default. That's for Data Messages; we'll go over that in more depth later.

Now press the 'Send Message' button to send the notification to the client app. Your notice will appear on your Android phone, and tapping it will activate the app. If you don't understand the text, you can view the movie above.

Post a Comment

Previous Post Next Post