[英]Firebase - Remote Config App Update
我正在使用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;
}
}
我的类扩展了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();
}
}
});
我的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);
}
您可以在Firebase控制台上添加条件。 因此,您的新配置仅影响适当的用户。 例如,我在应用程序中添加了app_version用户属性
有一个很简单的版本可以做到这一点。 只需按照以下步骤
将其与应用程序的当前版本代码进行比较,您可以通过以下代码获得
private int getCurrentVersionCode() { try { return getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return -1; }
如果获取的版本代码大于当前版本,请显示AlertDialog
要求更新应用程序。 否则,该应用程序已更新。
因此,每当推出新版本时,都需要将该新版本代码放入Firebase远程配置面板中
您可以阅读有关如何使用Firebase Remote Config强制用户更新应用程序的整个教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.