Push Notification in Android using Firebase (FCM) and PHP

Change Log
  • Updated the code to work on Android Oreo and above.

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. It can be used to send an unlimited number of notifications to your application.

In this tutorial, I will explain you on how to integrate Firebase Cloud Messaging to your app and send push notifications from Firebase Console or from Custom PHP Console. This tutorial will help you in learning on:

  • how to send the notification when the app is running, when the app is in the background and when the app is not running (or killed).
  • how to send the notification with an image.
  • how to open a website URL on click of notification.
  • how to open a specific activity on click of notification.
  • how to open Google Play Store on click of notification.

Types of Firebase Messages

Firebase message can contain 2 kinds of information notification and data payload. So we can have 3 types of messages one with notification, one with data payload and another with both.

Notification Message Type

Notification type will have mainly 2 fields title and body. As the name suggests they constitute the notification title and notification body. A typical Firebase Notification request looks as follows:

Data Payload Message Type

Data payload message type will have data field in place of notification. It allows additional data to be sent as part of the notification. Below is an example of the same:

Notification and Data Payload Message Type

We can also have both notification and data in one request as shown below:

It is advisable to use data payload since it helps in sending additional data and customization.

Message Recipients

We can send the message to a particular user or a set of users. We can target the message in 2 ways:

Sending Notification to a Single Device

We can send the notification to a particular user by providing the device’s FCM registration token. This token is generated when the app is registered in the Firebase. This usually happens during the first launch of the app. While sending the message to a single device we need to mention the FCM registration token in the “to” field of the request JSON:

Sending Notification to a Topic

Topics are nothing but subscription lists. Whenever you want to send the notification to a certain segment of users, you can make use of topics. You can create any number of topics and a user can be part of multiple topics. When you want to send the notification to a topic, you need to specify the topic name in the request JSON:

Setting up FCM in your Application

In Android Studio create a new project named “Firebase Push Notification”. If you already have an application in which you wish to receive the notification then you just need to open the project in Android Studio.

Click on Tools ==> Firebase. Now a sidebar will appear as shown below:

Setting up Firebase

Now in Cloud Messaging, click on “Setup Firebase Cloud Messaging”. Now the sidebar will change will display the following:

Setting up Firebase

If you are setting up firebase first time in your application then you need to connect your app to Firebase. For this, click on “Connect to Firebase”. Now your browser will open and you will be asked to log in to your Google Account.

Once you have successfully logged in, you will be prompted to create a new Firebase Project as shown below:

Setting up Firebase

Choose your country from the drop-down and click on Connect to FIrebase.

If the connection was successful, you should see “Connected” in green:

Setting up Firebase

Now Click on “Add FCM to your app”. Now you will get a popup window:

Setting up Firebase

This will clearly show you the changes that will happen to your gradle files once you Add FCM to your app. Click on “Accept Changes”. Now you can open project level and app level build.gradle files and observe the changes.

Creating Packages and Activities

Create the packages inside the main package as shown below:

Firebase Notification Package Structure

Also, create an Activity named SecondActivity. This is used to demonstrate opening another activity on click of the notification. Change the message in SecondActivity, so that we can easily recognize when this activity is opened.

Creating NotificationVO

Create a class named NotificationVO inside vo package. Create required fields and add getter and setter methods

Adding Notification tone to Project

Create a folder named raw inside res folder. Download notification.mp3 and copy it into the raw folder:

Firebase Notification Package Structure

Accessing the Firebase Registration Token

You can access token’s value by creating a new service ( File > New > Service > Service) which extends FirebaseInstanceIdService. Place the service inside services folder. You can access the token by overrding the method onTokenRefresh()  and by logging the token:

Once the token is received we will be logging it so that it can be used to send notifications. You can send the token to your app server by implementing the method sendRegistrationToServer().

Subscribe the user to “global” topic so that you can send notifications to all the apps

In order to unsubscribe from a topic you can use:

Also, add the service to AndroidManifest.xml file:

Creating Notification Utility

Create a class called NotificationUtils inside the utils package. This will be used to display the notification.

It has 3 methods:

  • displayNotification(NotificationVO, Intent): Displays the notification using the values populated in NotificationVO
  • getBitmapFromURL(String): Fetches the image contents from the Internet and decodes it into Bitmap.
  • playNotificationSound(): Helps in playing the notification sound stored in raw folder whenever a notification is received.

Handling Notifications

In order to handle the data payload of the notification, you need to create a new service inside the services package, which extends FirebaseMessagingService.

Override the onMessageReceived() method to receive the notification message.

Here the preference is given to data payload over notification. That is we use notification only if the data payload is not present.

Add an entry in AndroidManifest.xml  for the newly created service:

Now run the application on your device and monitor the LogCat. You will see the Firebase registration token printed there:

Firebase Registration Token

Sending Messages from Firebase Console

Open Firebase Console, click on the Project you have created for Firebase notifications and click on Grow ==> Notification ==> Send your First Notification (or New Message if you have already sent notifications).

Firebase Console

Provide the notification message and title. Choose User Segment as the target and select the app from the drop-down. You can also specify the target as a topic.

In Custom data field, you can specify the data payload as key-value pairs. Once you have filled the necessary details, click on “Send Message”. If everything went fine, you should have received the notification by now.

Firebase Notification on Device 670x1024 - Push Notification in Android using Firebase (FCM) and PHP

Note: When you send notifications from Firebase console, you will not receive data payload if your app is in the background.

To overcome this, we make use of PHP to develop custom console where we can send data payload in the message even if the app is in the background.

Sending notifications using PHP

In your applications, you might need to send notifications on the trigger of an event or schedule an event for future. This cannot be achieved via Firebase console. So we can make use of Firebase API and integrate it into the backend.

Make sure you have LAMP or XAMPP installed on your local machine.

Obtaining Firebase Server API Key

In Firebase Console, click on Project Settings

Obtaining Firebase Server API Key

Now click on Cloud Messaging, there you’ll see the Server Key

Firebase Server API Key

Creating PHP Project

1. Start WAMP/XAMPP server and navigate to www or htdocs folder and create a folder named firebase inside it. Inside this folder, create a subfolder called notifications

2. Now create a file named notification.php inside notifications folder. This will have Notification class which will help in constructing the notification data payload.

3. Now create index.php and add the following content. This contains the form to fill notification details and CURL request, that is made to the Firebase API.

4. Now go to http://localhost/firebase/notifications/ in your browser and access the PHP console. If you have any port configured to Apache, then don’t forget to use it.

Firebase PHP console

5. You can post notifications from here: Demo Console. You need not to worry about using your own Firebase API key. We do not have any logic to access or store them.

Demo Video

You can watch the demonstration of running the app in the below video:

If you wish to open Google Play Store, then you can give the App page URL in the destination field.

That’s it. It has been a long read. Hope you will not encounter any issues. If you do then please post them in the comments section. If you have liked this tutorial, please do follow us on Twitter and Facebook.

Download Source Code and APK

Download Source CodeDownload APK

Stay Connected

No spam guarantee.



Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

20 thoughts on “Push Notification in Android using Firebase (FCM) and PHP”

  1. Looks good. In “the Sending notifications using PHP” I can send a notification just to a specific application and all users?


  2. Hi, thank for this tutorial. when i check the box “Include Action”, i get this error : Curl failed: OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to fcm.googleapis.com:443 . how to fix it please?

      1. Thanks i have solved i will now like to know how i can transmit the data contained in my message in another android activity please help me!

  3. Excuse me for disturbing you but I try your link it does not work! can you give me a piece of code inserted in the code of your tutorial for me to do so please?

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *