我正在使用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);
}
答案 0 :(得分:1)
您可以在firebase控制台上添加条件。因此,您的新配置仅影响适当的用户。例如,我在我的应用程序中添加了app_version用户属性
答案 1 :(得分:1)
有一个很简单的版本可以做到这一点。只需按照以下步骤
将其与应用程序的当前版本代码进行比较,您可以 通过以下代码获取
private int getCurrentVersionCode() {
try {
return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
如果获取的版本代码大于当前版本,
显示AlertDialog
,要求更新应用程序。否则,该应用程序已更新。
因此,每当推出新版本时,都需要放置新版本 Firebase远程配置面板中的版本代码
您可以在how to force users to update the app using Firebase Remote Config
上阅读整个教程。