Checking new app update using Firebase Remote Config.

Step 1 : Install the Firebase SDK in your app, and be sure to add the dependency for Remote Config to your app-level build.gradle file as part of this step.
compile 'com.google.firebase:firebase-config:11.0.4'
Create a new project in firebase console and download google-services.json file  and put in your app src directory.

Step 2 : Go to https://console.firebase.google.com/u/0/project/<your-project-name>/config
and create new parameter in config.



Step 3 : Create force-update checker interface. Here all fetching key should have same name as declared in the firebase console.
package easydo.ithebk.listener;
import android.content.Context;
import android.support.annotation.NonNull;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import easydo.ithebk.BuildConfig;
/**
* Created by bharath on 8/5/17.
*/
public class ForceUpdateChecker {
public static final String KEY_UPDATE_REQUIRED = "force_update_required";
public static final String KEY_APP_VERSION_CODE = "google_play_app_version_code";
public static final String KEY_PLAY_WHATS_NEW_TEXT = "play_whats_new_text";
public static final String PLAY_WHATS_NEW_TEXT_DEFAULT = "Bug fixes";
private static final String TAG = ForceUpdateChecker.class.getSimpleName();
private OnUpdateNeededListener onUpdateNeededListener;
private Context context;
public ForceUpdateChecker(@NonNull Context context,
OnUpdateNeededListener onUpdateNeededListener) {
this.context = context;
this.onUpdateNeededListener = onUpdateNeededListener;
}
public static Builder with(@NonNull Context context) {
return new Builder(context);
}
public void check() {
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
String playStoreVersionConfig = remoteConfig.getString(KEY_APP_VERSION_CODE);
int appVersion = BuildConfig.VERSION_CODE;
String whatsNew = remoteConfig.getString(KEY_PLAY_WHATS_NEW_TEXT);
;
int playStoreVersion;
try {
playStoreVersion = Integer.parseInt(playStoreVersionConfig);
} catch (NumberFormatException e) {
playStoreVersion = 0;
}
System.out.println("playStoreVersion version:" + playStoreVersion);
System.out.println("App version:" + appVersion);
if (playStoreVersion != 0 && playStoreVersion > appVersion
&& onUpdateNeededListener != null) {
onUpdateNeededListener.onUpdateNeeded(whatsNew);
}
}
}
public interface OnUpdateNeededListener {
void onUpdateNeeded(String updateUrl);
}
public static class Builder {
private Context context;
private OnUpdateNeededListener onUpdateNeededListener;
public Builder(Context context) {
this.context = context;
}
public Builder onUpdateNeeded(OnUpdateNeededListener onUpdateNeededListener) {
this.onUpdateNeededListener = onUpdateNeededListener;
return this;
}
public ForceUpdateChecker build() {
return new ForceUpdateChecker(context, onUpdateNeededListener);
}
public ForceUpdateChecker check() {
ForceUpdateChecker forceUpdateChecker = build();
forceUpdateChecker.check();
return forceUpdateChecker;
}
}
}
Step 4 : Add update check listener inside fragment or in your activity class, and call fetch method for fetching firebase config.

package easydo.ithebk.fragment;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
import java.util.HashMap;
import java.util.Map;
import easydo.ithebk.BuildConfig;
import easydo.ithebk.R;
import easydo.ithebk.databinding.FragmentHomeBinding;
import easydo.ithebk.listener.ForceUpdateChecker;
/**
* Created by bharath on 28/8/16.
*/
public class HomeFragment extends Fragment implements ForceUpdateChecker.OnUpdateNeededListener {
private static final String TAG = HomeFragment.class.getSimpleName();
FragmentHomeBinding binding;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
updateConfig();
ForceUpdateChecker.with(getContext()).onUpdateNeeded(HomeFragment.this).check();
return binding.getRoot();
}
private void updateConfig() {
final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
// .setDeveloperModeEnabled(BuildConfig.DEBUG) // Enable this on debugging
.build();
firebaseRemoteConfig.setConfigSettings(configSettings);
// set in-app defaults
Map<String, Object> remoteConfigDefaults = new HashMap();
remoteConfigDefaults.put(ForceUpdateChecker.KEY_APP_VERSION_CODE, BuildConfig.VERSION_CODE);
remoteConfigDefaults.put(ForceUpdateChecker.KEY_UPDATE_REQUIRED, false);
remoteConfigDefaults.put(ForceUpdateChecker.KEY_PLAY_WHATS_NEW_TEXT, ForceUpdateChecker.PLAY_WHATS_NEW_TEXT_DEFAULT);
firebaseRemoteConfig.setDefaults(remoteConfigDefaults);
long cacheExpiration = 3600;
//onDevelopment make cacheExpiration as zero second;
if (firebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
firebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
System.out.println("remote config is fetched.");
firebaseRemoteConfig.activateFetched();
} else {
System.out.println("fetch failed:" + task.getException());
}
}
});
}
@Override
public void onUpdateNeeded(final String whatsNew) {
System.out.println("Update need to be pushed");
}
}

Comments

Post a Comment

Popular posts from this blog

Dynamically create view