0

I'm using Firebase Remote Config to alert users to a new app update with a dialog. If the users update the app how can I handle that the user has updated and no longer push for the user to update?

This is my UpdateHelper class:

public class UpdateHelper {

public static String KEY_UPDATE_ENABLE = "isUpdate";
public static String KEY_UPDATE_VERSION = "version";
public static String KEY_UPDATE_URL = "force_update_store_url";

public interface onUpdateCheckListener{
    void onUpdateCheckListener(String urlApp);
}
public static Builder with(UpdateHelper.onUpdateCheckListener context) {
    return new Builder(context);
}

private onUpdateCheckListener onUpdateCheckListener;
private Context context;

public UpdateHelper(UpdateHelper.onUpdateCheckListener onUpdateCheckListener, UpdateHelper.onUpdateCheckListener context){
    this.onUpdateCheckListener = onUpdateCheckListener;
    this.context = (Context) context;
}

public void check(){
    FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
    if(remoteConfig.getBoolean(KEY_UPDATE_ENABLE)){
        String currentVersion = remoteConfig.getString(KEY_UPDATE_VERSION);
        String appVersion = getAppVersion(context);
        String updateUrl = remoteConfig.getString(KEY_UPDATE_URL);

        if (!TextUtils.equals(currentVersion, appVersion) && onUpdateCheckListener !=null)
            onUpdateCheckListener.onUpdateCheckListener(updateUrl);
    }
}
private String getAppVersion(Context context) {
    String resuult = "";

    try {
        resuult = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        resuult = resuult.replaceAll("[a-zA-z] |-", "");
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return resuult;
}

public static class Builder{
    private UpdateHelper.onUpdateCheckListener context;
    private onUpdateCheckListener onUpdateCheckListener;

    public Builder(UpdateHelper.onUpdateCheckListener context) {
        this.context = context;
    }

    public Builder onUpdateCheck(onUpdateCheckListener onUpdateCheckListener){
        this.onUpdateCheckListener = onUpdateCheckListener;
        return this;
    }

    public UpdateHelper build(){
        return new UpdateHelper(context, onUpdateCheckListener);
    }

    public UpdateHelper check() {
        UpdateHelper updateHelper = build();
        updateHelper.check();

        return  updateHelper;
    }
}

My class that extends Application:

 public void onCreate() {
    super.onCreate();

    final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();

    // Default value
    Map<String, Object> defaultValue = new HashMap<>();
    defaultValue.put(UpdateHelper.KEY_UPDATE_ENABLE, false);
    defaultValue.put(UpdateHelper.KEY_UPDATE_VERSION, "1.0");
    defaultValue.put(UpdateHelper.KEY_UPDATE_URL, "https://play.google.com/store/apps/details?id=com.mallcommapp.toolboxgroup");

    remoteConfig.setDefaults(defaultValue);
    //fetch data from Firebase every 5 seconds, ideally 1 minute - 5mins etc
    remoteConfig.fetch(5).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if(task.isSuccessful()){
                remoteConfig.activateFetched();

            }
        }
    });

My MainActivity.java:

UpdateHelper.with(this).onUpdateCheck(this).check();
}
@Override
public void onUpdateCheckListener(final String urlApp) {
// Create alert dialog
    AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("New App Version Available").setMessage("Please update to use all the latest features and bug fixes").setPositiveButton("UPDATE", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int which) {
            redirectStore(urlApp);
        }
    }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
         dialogInterface.dismiss();
        }
    }).create();
    alertDialog.show();
}

private void redirectStore(String urlApp) {
    final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlApp));
    intent.addFlags((Intent.FLAG_ACTIVITY_NEW_TASK));
    startActivity(intent);
}
plase
  • 361
  • 1
  • 3
  • 9
Dan530c
  • 377
  • 1
  • 4
  • 13

2 Answers2

2

You can add condition on firebase console. So your new config affects only appropriated users. For example, I added app_version user property in my application

gokhan
  • 627
  • 1
  • 6
  • 16
  • Ok thanks, in my console I have "version" as a parameter, what would the condition look like? – Dan530c Jun 19 '18 at 12:07
  • I created custom user property("app_version") and then set it in my application and it showed on firebase console as user property. And I used it in my condition. – gokhan Jun 19 '18 at 12:10
  • Ok, I've set a user property in Firebase with "app_version", wherein my application should I declare this? sorry just trying to get a full understanding – Dan530c Jun 19 '18 at 12:12
  • @Dan530c you just send your application's version to firebase like FirebaseAnalytics.getInstance(this).setUserProperty("app_version","3.2.1"); – gokhan Jun 19 '18 at 12:17
  • Ok in my MainActivity I've added "public static FirebaseAnalytics mFirebaseAnalytics;" and in my onCreate " mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);" and then " public static FirebaseAnalytics getmFirebaseAnalytics() { mFirebaseAnalytics.setUserProperty("app_version","1.0.3"); return mFirebaseAnalytics; }" is this correct? – Dan530c Jun 19 '18 at 12:42
  • @Dan530c yes, When my custom property showed in firebase console I used it at firebase remote config condition. So I send remote changes only my intended app versions. – gokhan Jun 19 '18 at 12:47
  • Ok so if I'm correct in my understanding, when I update my app and build a new APK I update the user property value in Android and then in my remote config it checks "version" against "app_version" to update or not? – Dan530c Jun 19 '18 at 12:58
  • @Dan530c you should use the same name for comparison, you used "app_version" as custom property so you should use it in your remote config condition tab with "app_version" – gokhan Jun 19 '18 at 13:01
  • Ah okay and then I can do a condition like if "app_version" < "3.2.1" then update? – Dan530c Jun 19 '18 at 13:03
  • @Dan530c, yeah you will use that parameter in your condition – gokhan Jun 19 '18 at 13:07
  • Thank you so much for the help – Dan530c Jun 19 '18 at 13:08
  • @Dan530c you are welcome :) If my comments helped you, you might upvote them :) – gokhan Jun 19 '18 at 13:09
  • Will do! I'm still a noob so got low rep ha! – Dan530c Jun 19 '18 at 13:17
  • sorry one last thing, in my remote config parameters do I set a default value for "app_version"? – Dan530c Jun 19 '18 at 13:21
  • @Dan530c I m not sure but If I were you I would set the default as current version – gokhan Jun 19 '18 at 13:22
2

There is a quite an easy version to do that. Just follow these steps

  1. Just save the latest Version code of your app on Firebase Remote Config panel
  2. Fetch that version code value whenever the app is opened
  3. Compare it with the current version code of the app, which you can get by the following code

    private int getCurrentVersionCode() {
        try {
            return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }
    
  4. If the fetched version code is greater than the current version, show an AlertDialog asking to update the app. Otherwise, the app is already updated.

  5. So, whenever you roll out the new version, you need to put that new version code in Firebase Remote config panel

You can read the whole tutorial on how to force users to update the app using Firebase Remote Config

Machavity
  • 30,841
  • 27
  • 92
  • 100
Waqas Younis
  • 84
  • 1
  • 11