java - Firebase - 远程配置应用程序更新

标签 java android firebase firebase-remote-config

我使用 Firebase Remote Config 通过对话框提醒用户新的应用更新。如果用户更新应用程序,我如何处理用户已更新并且不再推送用户更新?

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

最佳答案

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

  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上阅读整个教程。

关于java - Firebase - 远程配置应用程序更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926645/

相关文章:

java - 行为类似于类字段的方法

java - 向后按 fragment 整理 Activity

android - 在 `FCM Cloud Messaging` 中获取与推送通知相关的分析

java - 我如何逐步查看 jetty 的源代码以查看它如何处理典型的 servlet 请求?

java - Joda-Time `DateTime` 对象到 `org.simpleframework.xml` ("Simple XML Serialization"库中字符串的转换器)

android - 如何将数据插入 Assets 文件夹中的数据库文件

android - 生产中的 FirebaseRemoteConfigClientException (Android)

swift - Firebase swift - 如何根据特定唯一键列表打印出值(存储在数组中的唯一键)

java - 如何检索关系表列的值

android - 在新的 intel x86 android 模拟器中访问 google api