我有一个类来获取Firebase远程配置数据,其中远程配置中有两个值。但不幸的是,我无法获取数据。
public class FirebaseConfig {
FirebaseRemoteConfig mFirebaseRemoteConfig;
FirebaseRemoteConfigSettings configSettings;
long cacheExpiration = 5;
String vehicle_latitude = "";
String vehicle_longitude = "";
public FirebaseConfig() {
mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
configSettings = new FirebaseRemoteConfigSettings.Builder()
.build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);
mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);
}
public String fetchLatitude(Context context) {
mFirebaseRemoteConfig.fetch(getCacheExpiration())
.addOnCompleteListener((Activity) context, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// If is successful, activated fetched
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
} else {
Log.d("", "");
}
vehicle_latitude = mFirebaseRemoteConfig.getString("tracker_vehicle_latitude");
}
});
return vehicle_latitude;
}
public String fetchLongitude(Context context) {
mFirebaseRemoteConfig.fetch(getCacheExpiration())
.addOnCompleteListener((Activity) context, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// If is successful, activated fetched
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
} else {
Log.d("", "");
}
vehicle_longitude = mFirebaseRemoteConfig.getString("tracker_vehicle_longitude");
}
});
return vehicle_longitude;
}
public long getCacheExpiration() {
// If is developer mode, cache expiration set to 0, in order to test
if (mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) {
cacheExpiration = 0;
}
return cacheExpiration;
}}
当我从另一个类中调用此函数时,无法从firebase中获取数据。
在MainActivity中
FirebaseConfig fc = new FirebaseConfig();
vehicle_latitude = fc.fetchLatitude(this);
vehicle_longitude = fc.fetchLongitude(this);
任何建议将不胜感激。
谢谢!
答案 0 :(得分:1)
尝试以这种方式获取
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
.setMinimumFetchIntervalInSeconds(10)
.build();
mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
mFirebaseRemoteConfig.fetchAndActivate();
vehicle_latitude = mFirebaseRemoteConfig.getString("tracker_vehicle_latitude");
vehicle_longitude = mFirebaseRemoteConfig.getString("tracker_vehicle_longitude");
有关如何设置的更多信息,请选中this