Created
October 20, 2018 18:08
-
-
Save jaisonfdo/0b02d495602da9366dcd4d2323fa521d to your computer and use it in GitHub Desktop.
FirebaseRemoteConfig Code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To get the version code from the auto generated file | |
final int versionCode = BuildConfig.VERSION_CODE; | |
// Hashmap which contains the default values for all the parameter defined in the remote config server | |
final HashMap<String, Object> defaultMap = new HashMap<>(); | |
defaultMap.put(FB_RC_KEY_TITLE, "Update Available"); | |
defaultMap.put(FB_RC_KEY_DESCRIPTION, "A new version of the application is available please click below to update the latest version."); | |
defaultMap.put(FB_RC_KEY_FORCE_UPDATE_VERSION, ""+versionCode); | |
defaultMap.put(FB_RC_KEY_LATEST_VERSION, ""+versionCode); | |
// Instance to access the remote config parameters | |
FirebaseRemoteConfig mFirebaseRemoteConfig; | |
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); | |
// To set the default values for the remote config parameters | |
mFirebaseRemoteConfig.setDefaults(defaultMap); | |
Task<Void> fetchTask=mFirebaseRemoteConfig.fetch(BuildConfig.DEBUG?0: TimeUnit.HOURS.toSeconds(4)); | |
fetchTask.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
// After config data is successfully fetched, it must be activated before newly fetched | |
// values are returned. | |
mFirebaseRemoteConfig.activateFetched(); | |
String title=getValue(FB_RC_KEY_TITLE,defaultMap); | |
String description=getValue(FB_RC_KEY_DESCRIPTION,defaultMap); | |
int forceUpdateVersion= Integer.parseInt(getValue(FB_RC_KEY_FORCE_UPDATE_VERSION,defaultMap)); | |
int latestAppVersion= Integer.parseInt(getValue(FB_RC_KEY_LATEST_VERSION,defaultMap)); | |
} else { | |
Toast.makeText(HomeActivity.this, "Fetch Failed",Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
// Instance to access the remote config parameters | |
FirebaseRemoteConfig mFirebaseRemoteConfig; | |
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); | |
// To enable the developer mode | |
mFirebaseRemoteConfig.setConfigSettings(new FirebaseRemoteConfigSettings.Builder() | |
.setDeveloperModeEnabled(BuildConfig.DEBUG).build()); | |
if(latestAppVersion>versionCode){ | |
if(forceUpdateVersion>versionCode) | |
isCancelable=false; | |
// App update dialog view instance, used to show the dialog with the received values | |
AppUpdateDialog appUpdateDialog; | |
appUpdateDialog = new AppUpdateDialog(HomeActivity.this, title, description, isCancelable); | |
appUpdateDialog.setCancelable(false); | |
appUpdateDialog.show(); | |
Window window = appUpdateDialog.getWindow(); | |
assert window != null; | |
window.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment