如何导出所有 firebase 远程配置键和值?
How to export all firebase remote config keys and values?
我想从现有的 firebase 项目迁移到新的 firebase 项目
问题是我现有项目中有很多远程配置键和值
现在我想导出所有远程配置键和值,然后导入到新的 firebase 项目,而不是在新项目中重新编写所有内容。
有什么简单的方法吗?。
我找到了解决方案,但没有尝试。
您不能从 Firebase 控制台执行此操作,但您可以编写一个使用 Remote Config REST 的简单脚本 API - https://firebase.google.com/docs/remote-config/use-config-rest
所以基本上您可以从一个项目下载远程配置,然后以编程方式将其上传到新项目。
干杯!
mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
Map<String, String> map = getFirebaseRemoteMap(context,R.xml.defultValues);
//map contains all remote values
}
});
确保您已在 defaultValue 文件中定义所有键,activateFetched 成功后,您将能够转储所有远程值
public Map<String, String> getFirebaseRemoteMap(Context context, @XmlRes int xmlRes) {
Map<String, String> stringStringMap = new HashMap<>();
XmlResourceParser xmlResourceParser = context.getResources().getXml(xmlRes);
try {
boolean isInKeyTag = false;
while (xmlResourceParser.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (xmlResourceParser.getEventType() == XmlResourceParser.START_TAG) {
String tagName = xmlResourceParser.getName();
if (tagName.equals("key")) {
isInKeyTag = true;
}
}
if (xmlResourceParser.getEventType() == XmlResourceParser.END_TAG) {
String tagName = xmlResourceParser.getName();
if (tagName.equals("key")) {
isInKeyTag = false;
}
}
if (xmlResourceParser.getEventType() == XmlResourceParser.TEXT) {
if (isInKeyTag) {
String key = xmlResourceParser.getText();
FirebaseRemoteConfigValue val = FirebaseRemoteConfig.getInstance().getValue(key);
String s = val.asString();
stringStringMap.put(key, s);
}
}
xmlResourceParser.next();
}
return stringStringMap;
} catch (Exception e) {
return null;
}
}
您可以通过以下方式获取远程配置键和值(Swift 4.2):
let remoteConfig = RemoteConfig.remoteConfig()
let ns = NamespaceGoogleMobilePlatform // Oddly evaluates to: "configns:firebase" including spelling mistake
let remoteKeys = remoteConfig.allKeys(from: .remote, namespace: ns)
let remoteDict = Dictionary(uniqueKeysWithValues: remoteKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
let defaultKeys = remoteConfig.allKeys(from: .default, namespace: ns)
let defaultDict = Dictionary(uniqueKeysWithValues: defaultKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
let staticKeys = remoteConfig.allKeys(from: .static, namespace: ns)
let staticDict = Dictionary(uniqueKeysWithValues: staticKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
在我的简短搜索中,我无法获得扁平化的 remoteConfig 键和值(即使用下标时返回的内容:remoteConfig[key]
)。我猜你只需要将远程值覆盖在默认值上?
我知道已经回答了这个问题,但我只是 运行 参与其中。找到一个简单的方法
remoteConfigInstance?.let {
it.fetchAndActivate().addOnCompleteListener { task ->
if (task.isSuccessful) {
val result = task.result
Timber.d("RemoteConfig - updated=$result")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
it.all.forEach { (t, _) ->
Timber.i( "All remoteConfig values = $t - ${it.getBoolean(t)}")
}
}
} else {
Timber.e("RemoteConfig - ERROR fetching ..")
}
}
}
我想从现有的 firebase 项目迁移到新的 firebase 项目 问题是我现有项目中有很多远程配置键和值 现在我想导出所有远程配置键和值,然后导入到新的 firebase 项目,而不是在新项目中重新编写所有内容。
有什么简单的方法吗?。
我找到了解决方案,但没有尝试。
您不能从 Firebase 控制台执行此操作,但您可以编写一个使用 Remote Config REST 的简单脚本 API - https://firebase.google.com/docs/remote-config/use-config-rest
所以基本上您可以从一个项目下载远程配置,然后以编程方式将其上传到新项目。
干杯!
mFirebaseRemoteConfig.fetch(cacheExpiration)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
mFirebaseRemoteConfig.activateFetched();
Map<String, String> map = getFirebaseRemoteMap(context,R.xml.defultValues);
//map contains all remote values
}
});
确保您已在 defaultValue 文件中定义所有键,activateFetched 成功后,您将能够转储所有远程值
public Map<String, String> getFirebaseRemoteMap(Context context, @XmlRes int xmlRes) {
Map<String, String> stringStringMap = new HashMap<>();
XmlResourceParser xmlResourceParser = context.getResources().getXml(xmlRes);
try {
boolean isInKeyTag = false;
while (xmlResourceParser.getEventType() != XmlResourceParser.END_DOCUMENT) {
if (xmlResourceParser.getEventType() == XmlResourceParser.START_TAG) {
String tagName = xmlResourceParser.getName();
if (tagName.equals("key")) {
isInKeyTag = true;
}
}
if (xmlResourceParser.getEventType() == XmlResourceParser.END_TAG) {
String tagName = xmlResourceParser.getName();
if (tagName.equals("key")) {
isInKeyTag = false;
}
}
if (xmlResourceParser.getEventType() == XmlResourceParser.TEXT) {
if (isInKeyTag) {
String key = xmlResourceParser.getText();
FirebaseRemoteConfigValue val = FirebaseRemoteConfig.getInstance().getValue(key);
String s = val.asString();
stringStringMap.put(key, s);
}
}
xmlResourceParser.next();
}
return stringStringMap;
} catch (Exception e) {
return null;
}
}
您可以通过以下方式获取远程配置键和值(Swift 4.2):
let remoteConfig = RemoteConfig.remoteConfig()
let ns = NamespaceGoogleMobilePlatform // Oddly evaluates to: "configns:firebase" including spelling mistake
let remoteKeys = remoteConfig.allKeys(from: .remote, namespace: ns)
let remoteDict = Dictionary(uniqueKeysWithValues: remoteKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
let defaultKeys = remoteConfig.allKeys(from: .default, namespace: ns)
let defaultDict = Dictionary(uniqueKeysWithValues: defaultKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
let staticKeys = remoteConfig.allKeys(from: .static, namespace: ns)
let staticDict = Dictionary(uniqueKeysWithValues: staticKeys.map { ([=10=], remoteConfig[[=10=]].stringValue) })
在我的简短搜索中,我无法获得扁平化的 remoteConfig 键和值(即使用下标时返回的内容:remoteConfig[key]
)。我猜你只需要将远程值覆盖在默认值上?
我知道已经回答了这个问题,但我只是 运行 参与其中。找到一个简单的方法
remoteConfigInstance?.let {
it.fetchAndActivate().addOnCompleteListener { task ->
if (task.isSuccessful) {
val result = task.result
Timber.d("RemoteConfig - updated=$result")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
it.all.forEach { (t, _) ->
Timber.i( "All remoteConfig values = $t - ${it.getBoolean(t)}")
}
}
} else {
Timber.e("RemoteConfig - ERROR fetching ..")
}
}
}