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 the XRequest feature to make an HTTP request to FCM servers which shall send push notifications to an Android device.
- Metamug Console
- Android Studio
- Android Test device
You can refer the following query
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.
message.xml should look as follows
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
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
The Authorization header requires the FCM server key which can be found in
Project Settings > Cloud Messagingsection of your app in the firebase console. You can test whether you can send push notifications to your Android device using any REST client.
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