【问题标题】:Firebase - Remote Config App UpdateFirebase - 远程配置应用更新
【发布时间】:2018-06-19 10:53:13
【问题描述】:

我正在使用 Firebase 远程配置通过对话框提醒用户有新的应用更新。如果用户更新了应用程序,我该如何处理用户已经更新并且不再推送用户更新?

这是我的UpdateHelper 班级:

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;
    }
}

我的扩展应用程序的类:

 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();

            }
        }
    });

我的 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);
}

【问题讨论】:

    标签: java android firebase firebase-remote-config


    【解决方案1】:

    有一个非常简单的版本可以做到这一点。只需按照以下步骤操作

    1. 只需将应用的最新版本代码保存在 Firebase 远程配置面板上
    2. 在应用打开时获取该版本代码值
    3. 与应用的当前版本代码进行比较,您可以 通过以下代码获取

      private int getCurrentVersionCode() {
          try {
              return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
          } catch (NameNotFoundException e) {
              e.printStackTrace();
          }
          return -1;
      }
      
    4. 如果获取的版本代码大于当前版本, 显示AlertDialog 要求更新应用程序。否则,应用程序已经更新。

    5. 因此,无论何时推出新版本,都需要将新版本 Firebase 远程配置面板中的版本代码

    你可以在how to force users to update the app using Firebase Remote Config阅读整个教程

    【讨论】:

    • 感谢您的教程很棒!
    【解决方案2】:

    您可以在 Firebase 控制台上添加条件。因此,您的新配置仅影响适当的用户。例如,我在我的应用程序中添加了 app_version 用户属性

    【讨论】:

    • 好的,谢谢,在我的控制台中我有“版本”作为参数,条件会是什么样子?
    • 我创建了自定义用户属性(“app_version”),然后在我的应用程序中设置它,它在firebase控制台上显示为用户属性。我在我的情况下使用它。
    • 好的,我在 Firebase 中使用“app_version”设置了一个用户属性,我应该在其中声明我的应用程序吗?抱歉,只是想全面了解
    • @Dan530c 您只需将应用程序的版本发送到 Firebase,例如 FirebaseAnalytics.getInstance(this).setUserProperty("app_version","3.2.1");
    • 好的,在我的 MainActivity 中我添加了“public static FirebaseAnalytics mFirebaseAnalytics;”在我的 onCreate 中“mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);”然后 "public static FirebaseAnalytics getmFirebaseAnalytics() { mFirebaseAnalytics.setUserProperty("app_version","1.0.3"); return mFirebaseAnalytics; }" 这样对吗?
    猜你喜欢
    • 2018-08-26
    • 2020-10-30
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多