Home Docs Examples Firebase Cloud Messaging With Metamug Xrequest

Firebase Cloud Messaging With Metamug Xrequest

External API Call: FCM

Firebase Cloud Messaging (FCM) is a service which is useful for sending push notifications to Android, iOS and few other platforms.

In this article, we shall build a simple push messaging API using Metamug console and use XRequest feature to make HTTP request to FCM servers which shall send push notifications to an Android device.

Prerequisites

Metamug Console

Android Studio

Android Test device

Create Backend

Create a backend in the Metamug Console with name pushnotifier . Use the SQL Editor to create a new table called messages which contains columns title and body.

You can refer the following query

CREATE TABLE messages (id INT NOT NULL AUTO_INCREMENT, title VARCHAR(20) NOT NULL, body VARCHAR(50) NOT NULL, PRIMARY KEY(id))

Create Resource

Create a resource file called message.xml which contains a GET request to fetch all records in the messages table and a POST request for inserting a new message record.

The message.xml should look as follows

<Resource xmlns="http://xml.metamug.net/resource/1.0" v="1.0"> <Request method="GET"> <Sql> SELECT * FROM messages </Sql> </Request> <Request method="POST"> <Sql> INSERT INTO messages (title, body) VALUES ($title, $body) </Sql> </Request> </Resource>

After saving the above resource file, the REST API for message will get generated. The endpoint for the API will be http://localhost:7000/pushnotifier/v1.0/message and its functions would be as follows

GET request made to the API shall return the list of records from the messages table POST request along with form params title and body shall insert a new record into the table

Setting up FCM client on Android

In order to receive FCM push notifications in your Android app, we need to extend FirebaseMessagingService from the Firebase SDK. The detailed instructions to implement FCM in your Android project are given here.

We shall make use of topic messaging in FCM. Subscribe to a topic messages by adding the following line in your Android code

FirebaseMessaging.getInstance().subscribeToTopic("messages");

Sending Push notifications

Sending push notifications to the devices subscribed to a particular topic requires you to make an HTTP POST request to the FCM servers. The format of the request is as follows

https://fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "to": "/topics/{topicName}", "notification": { "title": {titleValue}, "body": {bodyValue} } }

The Authorization header requires the FCM server key which can be found in Project Settings > Cloud Messaging section of your app in the firebase console. You can test whether you can send push notifications to your Android device using any REST client.

Using XRequest

Let us update message.xml so that everytime a message is added to the table using POST request, a push notification is sent to the Android devices that have subscribed to the topic messages .