Build an Android app under maintenance feature in 5 minutes.

Dhiraj Sharma
4 min readMay 17, 2019

--

This article provides step by step guide to the implemet app under maintenance feature on your android app along with building a control panel to manage maintenance status & maintenance message.

During downtime, if the user is using the application then the user might get a frequent error which creates the frustrating user experience. Here downtime refers to scenarios like some new changes are being deployed, servers are being upgraded, the configuration is being changed, the system is facing some unexpected issues and so on. We can solve this issue by implementation app under maintenance feature, where the user receives a human-friendly message regarding the current reason of downtime & is not able to use the system.

So, let's get started!

Step I: Firebase project & real-time database

  1. Open Firebase console by logging in with Gmail account.
  2. Click on Add project
  3. Provide project name “App Under Maintenance Demo”, check accept the terms & click on create project.
  4. Now the project is created. Click on Database on the left panel of the project dashboard.
  5. Scroll down to find Real-time Database, click on Create database.
  6. Select Start in test mode, then click on Enable.
  7. Now you are on database page, click +(add child) on the right of the database name (app-under-maintenance-demo).
  8. Enter is_under_maintenance as name & false as value, click on Add.
  9. Add another child of the database, under_maintenance_message as name & “App is currently under maintenance.” as value.

Now your database should look like this:

Note: Firebase Real-time Database has some restriction on data storage and operations.

Step II: Add Firebase SDK to app

I will be using the simple Hello World application to demonstrate this feature. You can implement any of your projects.

We need to register our app with Firebase project.
To do so follow the following steps:

  1. Open Firebase console & navigate to Project Overview page.
  2. Click on Add Android App (Android icon).
  3. Enter your app’s application ID to Android package name, then click on Register app.
  4. Click on “Download google-services.json”, copy downloaded file inside app directory of your Android project, Click on next.
  5. Add dependencies to Project-level build.gradle & App-level build.gradle as instructed on Add Firebase SDK step. Click on next.
  6. You can verify the installation by running your app or can skip by clicking on skip this step.

Add dependency for Real-time Database to your app-level build.gradle file implementation ‘com.google.firebase:firebase-database:17.0.0’

Step III: Implement under maintenance feature to Android app

As we require under maintenance feature on all screens of the app, we will create a Base Activity. This Base Activity consists of implementation for under maintenance & is extended by all activities.

First, create a model for UnderMaintenance data, this model will be used to map data form Firebase real-time database.

Create an abstract activity named BaseActivity extending AppcompatActivity.

connectToFirebase method gets the reference to Firebase database & listens for changes on the database. Data received from the database is mapped to UnderMaintenance model & dialog is shown or dismissed according to flag.
connectToFirebase() should be invoked from onCreate of BaseActivity.

showUnderMaintenanceDialog shows a dialog with the message provided.

dismissUnderMaintenanceDialog dismisses dialog if showing.

Now you need to extend all activities (to which you need under maintenance implementation) by BaseActivity.

Its time for some demo. Open Real-time Database from Firebase console & run your app. Now change the value of is_under_maintenance to true. You should see the dialog on the app.

You can also update message & see changes.
If things are not working, as shown above, do not worry. You can follow the sample project I created while writing this article.

Logging in to Firebase console to toggle maintenance status every time might be time-consuming & error prone. So let’s build a simple control panel through which can control under maintenance status & message.

Step IV: Create a control panel to manage maintenance status

Clone this sample project from GitHub.

First, rename config-example.js to config.js & get firebase project API keys as instructed on that file.

index.html file contains inputs switch & text field which can be used to update under maintenance status & message.
main.js connects to firebase using credentials form config.js file & will update the firebase database according to inputs on index.html.

Now time for final demo, Open your app & open your control panel (index.html) on the browser.

And it’s done.

If anything is not working as mentioned you can follow my sample projects or have a question on the comment section.

Thanks for reading.

--

--