我尝试从 Firebase 中的远程配置中获取特定值,但它总是给我第一次在客户端定义的值。
这是我的代码:
public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;
private static final String FRIENDLY_MSG_LENGTH_KEY ="friendly_msg_length";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setDeveloperModeEnabled(BuildConfig.DEBUG).build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);
Map<String, Object> defaultConfigMap = new HashMap<>();
defaultConfigMap.put(FRIENDLY_MSG_LENGTH_KEY, DEFAULT_MSG_LENGTH_LIMIT);
mFirebaseRemoteConfig.setDefaults(defaultConfigMap);
fetchCnfig();
}
private void fetchCnfig() {
long cacheExpiration = 3600;
if(mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()){
cacheExpiration = 0;
}
mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
mFirebaseRemoteConfig.activateFetched();
applyRetrievedLengthLimit();
}else{
Log.w(TAG, "error fetching config" + task.getException().getMessage());
applyRetrievedLengthLimit();
}
}
});
}
private void applyRetrievedLengthLimit() {
long friendly_msg_length = mFirebaseRemoteConfig.getLong(FRIENDLY_MSG_LENGTH_KEY);
mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(((int) friendly_msg_length))});
}
任何想法可能是错误的都会被评估