Send Device-to-Device Push Notification using Firebase Cloud Messaging without using external server

Robin Singh
MindOrks
Published in
4 min readJul 21, 2019

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost. It allows you to send push notification directly from Firebase console or with an app server or some trusted server where the logic runs. This article will tell you how to send push notification between devices like most of the chat apps without using any external server.

Step 1 : Create a new Android Studio Project and add the necessary dependencies

First, we need to create a new Android Studio project and add the relevant dependencies to it.

The first is a simple one — set up Firebase in your project. You can find a good tutorial here. In order to use FCM, you’ll need to add the following dependency to your app:

Step 2: Create Firebase Service

Now we need to create a Firebase service: MyFirebaseMessagingService. This service will used to receive and display the notifications. Services do not have any visual interface and are used for background running operations. To create a service, right click on the app folder and select New -> Service -> Service.

New Service

Type in your service name and click Finish button to create the service.

Go to the AndroidManifest.xml file and update your service declarations under the application tag. Also, add INTERNET and CLOUD TO DEVICE MESSAGING permissions so your app can interact with FCM server.

Step 3: Setting up the service

MyFirebaseMessagingService extends FirebaseMessagingService class in order to receive messages from the FCM server. This service handles the reception and display of notifications. OverrideOnMessageReceived() function within the service so that it will be called whenever a new notification message is received.

Step 4: Implementing notification sending logic

First you need to get your Server Key from Firebase console using the following steps:-

  • Click the gear icon beside Project Overview, and then click on Project settings
  • Click on Cloud Messaging tab, and copy your Server key

Implement the Sending Logic

Sending push notification requires just an HTTP post request to FCM server with the following request properties:

Method Type: POST

URL: https://fcm.googleapis.com/fcm/send

Headers:

Authorization: key="Firebase server key"
Content-Type: application/json

Body:

{
"to": "/topics/topic_name",
"data": {
"title": "Notification title",
"message": "Notification message",
"key1" : "value1",
"key2" : "value2" //additional data you want to pass
}
}

Keeping in mind the above structure, you will first create a JsonObject of the notification body within your activity class. This object will contain the receiver’s topic, notification title, notification message, and other key/value pairs you wish to add.

We will be using volley library to make a network request to FCM server, then the server will use the request parameters to route the notification to the targeted device.

With that, you have completed building your app. You can start sending push notifications between devices without using any external server. Always ensure that you get the topic of the recipient correct else the notification will not be delivered. If you did everything right, you will have a similar result to this.

The code for the sample app above can be found over here, feel free to fork it and follow along :

Thanks for reading! If you enjoyed this story, please click the 👏 button and share to help others find it! Feel free to leave a comment 💬 below.

Let’s connect on Twitter, LinkedIn

--

--

Robin Singh
MindOrks

Software Engineer @ Grofers | Google-certified Android Developer | Open Source Contributor