How to make Dynamic Application update using Firebase Remote Config
Bismillah and Alhamdullah. We often see that apps often display something that requires us to update the application. One way is to utilize the Remote Config from Firebase. But how? Let’s try…
Of course, the first thing is not to forget to create firebase and android projects and don’t forget to add the library :
implementation(platform("com.google.firebase:firebase-bom:32.8.1"))
implementation("com.google.firebase:firebase-config")
implementation("com.google.firebase:firebase-analytics")
Then don’t forget to add config to the Firebase Remote Config
Next we will try to define the code to retrieve data on remote config, for the code I made more or less like this
class RemoteConfig {
private var remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig
init {
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600
}
remoteConfig.setConfigSettingsAsync(configSettings)
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
override fun onUpdate(configUpdate: ConfigUpdate) {
Log.d(TAG, "onUpdate: ${remoteConfig.activate().result}")
}
override fun onError(error: FirebaseRemoteConfigException) {
Log.w(TAG, "Config update error with code: " + error.code, error)
}
})
}
fun getUpdateApp(): Boolean {
return remoteConfig[VERSION_APP].asDouble() > BuildConfig.VERSION_NAME.toDouble()
}
companion object {
private val TAG = RemoteConfig::class.java.simpleName
private const val VERSION_APP = "version_app"
}
}
Oh yeah, in the code above don’t forget to equalize the keys in the remote config, of course if key is different it will not be read
private const val VERSION_APP = "version_app"
Our last step is just to take a value whether the application needs to be updated or not
class MainActivity : ComponentActivity() {
private var isUpdateApp = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val remoteConfig = RemoteConfig()
setContent {
LaunchedEffect(key1 = Unit) {
isUpdateApp = remoteConfig.getUpdateApp()
}
UpdateAppFirebaseTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
if (isUpdateApp) {
UpdateAppContent()
} else {
HomeContent()
}
}
}
}
}
}
Let met explain a little bit about the code above
LaunchedEffect(key1 = Unit) {
isUpdateApp = remoteConfig.getUpdateApp()
}
Why use LaunchEffect? Yes, because we need when the application is first opened whether our application has an update or not. If yes then we can pop up a dialog or something like that for the user to update the application, like code below
if (isUpdateApp) {
UpdateAppContent()
} else {
HomeContent()
}
Finish, hopefully this short article can be useful. For the complete code, you can check the link below. Thanks youu….