Force your users to update your app with using Firebase in Android!

Waqas Younis
3 min readJun 29, 2018
Dialog to force users update the app.

There could be a lot of situation when you want all of user to update their app or just stop using the old one. For example you use some third party API and they made some changes in their API and mentioned that the old one won’t be working after this date.

Now, you opened Android Studio, update the API, generate signed APK and rolled it out to production. Cool… But how will you move every single user to the latest version of app? That’s a problem. Coz if the user don’t update the app, it will crash and you may get some bad reviews, which could be catestrophic.

Here, Firebase Remote Config can help you out. I know that there could be many libraries out there but I just love to Firebase and all of it’s products so …

We build a mechanism with Firebase Remote Config in such a way that it will show a dialog to old version of app users to update the app.

Here is the app running that we are going to built through this tutorials:

Running the code we will be writing through this tutorial

Overview of what we are going to implement!

We will

  1. Save the latest version code of app in Firebase Remote Config Console
  2. Fetch the value from Firebase Remote Config each time user opens the app
  3. Check if the fetched version code is greater than current version code of app or not. If it is, then show a dialog saying, Hey, please update the app or exit. Coz a no user better than an angry user.

That’s it. So whenever you release a new app, you need to put that latest version code in Firebase Remote Config Console.

Below is that Code:

Create a Firebase Remote Config Instance

private FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

Create a default HashMap, values from this HashMap will be served until a fetch request is completed

private HashMap<String, Object> firebaseDefaultMap;

Initialize the default HashMap and put the current version code in there. And finally set it as default for Firebase Remote Config instance.

firebaseDefaultMap = new HashMap<>();
firebaseDefaultMap.put(VERSION_CODE_KEY, getCurrentVersionCode());
mFirebaseRemoteConfig.setDefaults(firebaseDefaultMap);

We need to configure the settings to enable the developer mood. So that we can have multiple fetch request in an hour for development purpose. Otherwise Firebase Remote Config fetch the value and cache it for 12 hours. After 12 hours cache expires and new value is retrieved. We don’t want that for now, so we will enable the developer mode:

mFirebaseRemoteConfig.setConfigSettings(
new FirebaseRemoteConfigSettings.Builder().setDeveloperModeEnabled(BuildConfig.DEBUG)
.build());

Now we will call the fetch method and add an onCompleteListener which will be called the fetch process is complete. Not only this, but we also need to check if the task (fetching process) was successful or not. So here we go

mFirebaseRemoteConfig.fetch().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
Log.d(TAG, "Fetched value: " + mFirebaseRemoteConfig.getString(VERSION_CODE_KEY));
//calling function to check if new version is available or not
checkForUpdate();
} else {
Toast.makeText(MainActivity.this, "Someting went wrong please try again",
Toast.LENGTH_SHORT).show();
}
}
});

So, the activateFetched() method made the fetched value available in the app. It means if we call mFirebaseRemote.getString(KEY); then it will return only the default value.

Now in the check for update, we can get the latest version code and compare it with the current version code. If fetched one is greater then will show an AlertDialog asking users to update the app.

private void checkForUpdate() {
int latestAppVersion = (int) mFirebaseRemoteConfig.getDouble(VERSION_CODE_KEY);
if (latestAppVersion > getCurrentVersionCode()) {
new AlertDialog.Builder(this).setTitle("Please Update the App")
.setMessage("A new version of this app is available. Please update it").setPositiveButton(
"OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast
.makeText(MainActivity.this, "Take user to Google Play Store", Toast.LENGTH_SHORT)
.show();
}
}).setCancelable(false).show();
} else {
Toast.makeText(this,"This app is already upto date", Toast.LENGTH_SHORT).show();
}
}

That’s it. How easy it is. You can read the whole tutorial explaining each step in this tut with some extra things about Firebase Remote Config here

You can get the complete code from this GitHub Repository

Or you can watch the fast forwarded video here:

There is a lot more we can do with Firebase Remote Config.

We can apply some really helping conditions,

We can create some A/B testing with it to get the best insights

I hope you guys have understood something out of this video.

--

--