Firebase Remote Config Notification Update

Firebase remote config for create notification update

Hello every one this is my first english blog, and my english is not good enough, so i hope you can understand my writing. And today i will share about how to create notification update using Firebase Remote Config.

Problem

When your android app is going to live there’s will always so much problem, the real world is not like our expectations. Android apps is not like web which we can not updates on the fly. We need to compile out code again to apk and upload to play store and then user need to update their app to get fix error on their app. But sometimes user do not know if there our application is have new update on playstore. So that way we need to make a schema the will notify out users to update their app.

Firebase Remote Config

And one of the solution is we can use Firebase Remote Config.  Basically firebase remote config is variable of config that we can change the value from firebase dashboard. With firebase remote config we can control the value of configuration in our android app from firebase. In this way will give advantages to us for manage the value of configuration inside our android app which has going live. So one of implementation that we can use Firebase Remote Config to create notification to user about new update of our app.

Action

Now let’s get started to implement firebase remote config to our apps. First we need to create new project android connect to firebase. There’s many way to connect our project to firebase but my favorite one is using firebase plugin on Android Studio. But right now i’m not going to explain how to connect the project to the firebase please  read the firebase documentation to connect your project into firebase.

So next we need to add some dependency to our project.

implementation 'com.google.firebase:firebase-config:11.0.4'

And now add this code into activity or fragment to checking current update version of our app.

private void checkUpdate() {
    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

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

    long cacheExpiration = 3600;
    if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
        cacheExpiration = 0;
    }

    mFirebaseRemoteConfig.fetch(cacheExpiration)
            .addOnCompleteListener(this, task -> {
                if (task.isSuccessful()) {
                    mFirebaseRemoteConfig.activateFetched();
                }
                String build_version = mFirebaseRemoteConfig.getString("build_version");
                int updatedVersion = Integer.parseInt(build_version);
                if (updatedVersion > BuildConfig.VERSION_CODE) {
                    new AlertDialog.Builder(this)
                            .setTitle(R.string.alert_title)
                            .setMessage(R.string.plase_update_version)
                            .setCancelable(false)
                            .setNegativeButton("Skip", (dialogInterface, i) -> {
                                dialogInterface.dismiss();
                            })
                            .setPositiveButton("Ok", (dialog, which) -> {
                                openPlayStore();
                                dialog.dismiss();
                            }).show();
                } else {
                    Log.d("Response", "checkUpdate: false");
                }
            });
}

public void openPlayStore() {
    final String appPackageName = this.getPackageName();
    try {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
    }
}

Setting Firebase Remote Config

Now go to Firebase Remote Config and add new parameter remote config.

Firebase Remote Config

And don’t forget to publish the changes :

And if we change the value of build_version we will get alert like this :

 

So the idea is we will check the build version of our app and if the updateVersion is more higher than BuildVersion we ask to user to update their app. Build version can be found on build.gradle like this.

defaultConfig {
    applicationId "com.ahmadrosid.notificationupdate"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

For this sample project you can download here. And that for to day i hope you get the point. Don’t forget to share this article.

About the author

ocittwo

Hi i'm Rosid, I always learning every day, and the more I learn, the more I realize how much I don’t know.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *