FIREBASE

We mainly use Firebase for our RealTime DB. It is a platform for mobile and web apps.

Realtime DB

The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. All of your clients share one Realtime Database instance and automatically receive updates with the newest data.

Structure

realtime-db-struct

In the structure above, we can make an efficient listener for our users. The idea is that they can only see the updates of their order based on their mobile numbers.

  • transactions - <ROOTOBJECT> This is a static root object to group the transactions.
  • 999999999 - <MOBILENUMBER> This is the mobile number user use to order an item
  • 489771923346567800 - <TRACKING> This is the tracking number for that order.
  • IN-TRANSIT - <BODY> This is the order status.

How to update the order status

The basic write operation through the REST API is PUT. Using PUT, we can write a string, number, boolean, array or any JSON object to our Firebase database. In this case, we'll pass it an object:

Example:

  curl -X PUT -d <BODY> 
  "https://<DATABASEAPP>.firebaseio.com/<ROOTOBJECT>.json"

Since the object we will update is one layer deeper, we will need to map it as we are routing to the pages.

  curl -X PUT -d <BODY> 
  "https://<DATABASEAPP>.firebaseio.com/<ROOTOBJECT>/<MOBILENUMBER>/<TRACKING>.json"

It will be then:

  curl -X PUT -d "\"IN-TRANSIT\"" 
  https://<DATABASEAPP>/transactions/9999999999/489771923346567800.json

NOTE: authToken will be passed to restrict DB access.

  curl -X PUT -d "\"IN-TRANSIT\"" 
  https://<DATABASEAPP>/transactions/9999999999/489771923346567800.json?auth=D5S9zGA8SsN9J28KMUlCrPGrVhHkXiBjSRSdQ0Tc

This token will be implemented in the staging platform only. This will change once deployed in production and should be saved on your env or config file.

Firebase Cloud Messaging

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

How does it work?

An FCM implementation includes two main components for sending and receiving:

  1. A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target and send messages.
  2. An iOS, Android, or Web (JavaScript) client app that receives messages.

You can send messages via the Admin SDK or the HTTP and XMPP APIs. For testing or for sending marketing or engagement messages with powerful built-in targeting and analytics, you can also use the Notifications composer.

How to send notification?

We will be using HTTP and XMPP APIs.

  • SERVER KEY - A server key that authorizes your app server for access to Google services, including sending messages via Firebase Cloud Messaging. You obtain the server key when you create your Firebase project.
  • BROWSER TOKEN - An ID generated by the FCM SDK for each client app instance. Required for single device and device group messaging. Note that registration tokens must be kept secret.
  • BODY - Body of the notification
  • TITLE - Title of the notification
  • URL - The Url where user will be redirected once they click the notification

Curl command:

curl -X POST --header "Authorization: key=<SERVER KEY>" --Header 
"Content-Type:application/json" https://fcm.googleapis.com/fcm/send -d 
"{\"to\":\"<BROWSER TOKEN>\",
\"data\":{
  \"notification\":{
      \"body\": \"<BODY>\",
      \"title\":\"<TITLE>\",
      \"data\": {
        \"url\":\"<URL>\"
      }
    }
  },\"priority\":10}"

HTTP Postman

Header:

FCM-Header

Body:

FCM-Body