Send Device-to-Device Push Notifications Without Server-side Code
Firebase Cloud Messaging (FCM) allows you to send push notifications directly from Firebase console or with an app server or some other trusted environment where the server logic runs. However, you may want to send push notifications between devices, like in most chat apps, but without writing any server-side code. Of course, it’s very possible. This tutorial explains exactly how you could achieve that in four easy steps.
You will need the following tools and skills set:
- Android Studio (version 3.x or later)
- Firebase account (sign up here)
- Basic knowledge of Android development
If you have these prerequisites, then the rest will be easier than you thought.
1. Create your android project and link to Firebase
The first step is to create your project on Android Studio, and then link it up with Firebase. If you need any help with that, I have already written about it here.
Also, add Firebase messaging dependency to your app level build.gradle
2. Create Firebase services
The next step is to create two Firebase services: MyFirebaseInstanceIDService
and MyFirebaseMessagingService.
The first service will handle the device registration process, and the second will handle the reception and display of notifications. Unlike activities, services lack a visual user interface. They’re used to implement long-running background operations or a rich communications API that can be called by other applications. To create a service, right click on the app folder and select: New >Service>Service.
Type in your service name, and click the Finish button. Repeat the same steps for the second service.
MyFirebaseInstanceIDService
Go to your 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.
3. Setup the services
i. FirebaseInstanceIdService
MyFirebaseInstanceIDService
must extend FirebaseInstanceIdService class in order to handle the device registration process. Within this service, override theonTokenRefresh()
method so it will be invoked whenever the system determines to refresh tokens. This usually happens when the user installs/reinstalls the app or when the user clears app data.
Since you are sending notifications between devices, each user must subscribe to a topic with a distinct user_id. This ensures that the users receive notifications sent to the topic that matches their user_id. Here is the implementation of the MyFirebaseInstanceIDService
class:
ii. FirebaseMessagingService
MyFirebaseMessagingService
is actually where the main work takes place. It extends FirebaseMessagingService class in order to receive messages from the FCM server. This service handles the reception, customization, and display of notifications. OverrideOnMessageReceived()
method within the service so it will be called whenever a new notification message is received.
As you may have noticed in(line 15-17),
you have to verify if the user’s device version, and if it’s Android Oreo or a higher version, then set up a notification channel, else notifications wouldn’t be received by that device. Here is how to set up a notification channel.
4. Implement the notification sending logic
This is the most important part of the whole article. This is where you define the content of the notification and how it will be modelled. However, before you dive into coding, follow these steps to obtain your Server Key from Firebase console.
- Click the gear icon beside Project Overview, and then click on Project settings
- Navigate to Cloud Messaging tab, and copy your Server key
Design your User Interface
This is optional, but for the purpose of this tutorial, I designed a simple user interface to help the sender compose the content of the notification. The UI consists of two TextViews
for title and message, and a sendButton
.
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/notification_userId",
"data": {
"title": "Notification title",
"message": "Notification message",
"key1" : "value1",
"key2" : "value2" //additional data you want to pass
}
}
With these concepts in mind, 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.
The next step is to make a network request to FCM server using Volley library, then the server will use the request parameters to route the notification to the targeted device.
Finally, add MySingleton
class which will serve as a RequestQueue for the notification requests.
With that, you have completed building your app. You can start sending push notifications between devices without writing any server-side code. 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.
I hope this article was quite helpful. I’ve uploaded the demo app to GitHub so you can check it out.
📝 Read this story later in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.