您可以使用 Firebase Remote Config 在您的应用程序中定义参数并在云端更新它们的值,从而允许您修改应用程序的外观和行为而无需分发应用程序更新。本指南将引导您完成入门步骤并提供一些示例代码,所有这些代码都可以从firebase/quickstart-android GitHub 存储库中克隆或下载。
第 1 步:将 Firebase 和 Remote Config SDK 添加到您的应用
如果您还没有,请将 Firebase 添加到您的 Android 项目中。
对于 Remote Config,需要 Google Analytics 才能将应用程序实例有条件地定位到用户属性和受众。确保在项目中启用 Google Analytics 。
在您的模块(应用级)Gradle 文件(通常为
<project>/<app-module>/build.gradle
)中,添加 Remote Config Android 库的依赖项。我们建议使用Firebase Android BoM来控制库版本。此外,作为设置 Analytics 的一部分,您需要将适用于 Google Analytics 的 Firebase SDK 添加到您的应用中。
Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.0') // Add the dependencies for the Remote Config and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-config-ktx' implementation 'com.google.firebase:firebase-analytics-ktx' }
通过使用Firebase Android BoM ,您的应用将始终使用兼容版本的 Firebase Android 库。
(备选)在不使用 BoM 的情况下添加 Firebase 库依赖项
如果您选择不使用 Firebase BoM,则必须在其依赖项行中指定每个 Firebase 库版本。
请注意,如果您在应用中使用多个Firebase 库,我们强烈建议您使用 BoM 来管理库版本,以确保所有版本都兼容。
dependencies { // Add the dependencies for the Remote Config and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-config-ktx:21.2.1' implementation 'com.google.firebase:firebase-analytics-ktx:21.2.0' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.0') // Add the dependencies for the Remote Config and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-config' implementation 'com.google.firebase:firebase-analytics' }
通过使用Firebase Android BoM ,您的应用将始终使用兼容版本的 Firebase Android 库。
(备选)在不使用 BoM 的情况下添加 Firebase 库依赖项
如果您选择不使用 Firebase BoM,则必须在其依赖项行中指定每个 Firebase 库版本。
请注意,如果您在应用中使用多个Firebase 库,我们强烈建议您使用 BoM 来管理库版本,以确保所有版本都兼容。
dependencies { // Add the dependencies for the Remote Config and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-config:21.2.1' implementation 'com.google.firebase:firebase-analytics:21.2.0' }
第 2 步:获取 Remote Config 单例对象
获取远程配置对象实例并设置最小获取间隔以允许频繁刷新:
Kotlin+KTX
val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig val configSettings = remoteConfigSettings { minimumFetchIntervalInSeconds = 3600 } remoteConfig.setConfigSettingsAsync(configSettings)
Java
FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() .setMinimumFetchIntervalInSeconds(3600) .build(); mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
单例对象用于存储应用内默认参数值、从后端获取更新的参数值,以及控制获取的值何时可用于您的应用。
在开发过程中,建议设置一个相对较低的最小获取间隔。有关详细信息,请参阅节流。
第 3 步:设置应用内默认参数值
您可以在 Remote Config 对象中设置应用内默认参数值,以便您的应用在连接到 Remote Config 后端之前按预期运行,并且如果没有在后端设置默认值,则默认值可用。
使用Map对象或存储在应用程序的
res/xml
文件夹中的XML 资源文件定义一组参数名称和默认参数值。 Remote Config 快速入门示例应用程序使用XML 文件来定义默认参数名称和值。如果您已经配置了 Remote Config 后端参数值,您可以下载包含所有默认值的生成的 XML 文件并将其保存到应用程序的
res/xml
目录:休息
curl --compressed -D headers -H "Authorization: Bearer token -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:downloadDefaults?format=XML -o remote_config_defaults.xml
Firebase 控制台
在Parameters选项卡中,打开 Menu ,然后选择Download default values 。
出现提示时,为 Android 启用 .xml ,然后单击下载文件。
使用
setDefaultsAsync(int)
将这些值添加到远程配置对象,如下所示:Kotlin+KTX
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
Java
mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
第 4 步:获取要在您的应用中使用的参数值
现在您可以从 Remote Config 对象中获取参数值。如果您在后端设置值,获取它们,然后激活它们,那么这些值可用于您的应用程序。否则,您将获得使用setDefaultsAsync(int)
配置的应用内参数值。要获取这些值,请调用下面列出的映射到您的应用程序期望的数据类型的方法,并提供参数键作为参数:
第 5 步:在 Remote Config 后端设置参数值
使用 Firebase 控制台或远程配置后端 API ,您可以创建新的服务器端默认值,根据您所需的条件逻辑或用户定位覆盖应用内值。本节介绍创建这些值的 Firebase 控制台步骤。
- 在Firebase 控制台中,打开您的项目。
- 从菜单中选择远程配置以查看远程配置仪表板。
- 使用与您在应用程序中定义的参数相同的名称定义参数。对于每个参数,您可以设置一个默认值(最终会覆盖相应的应用内默认值),您还可以设置条件值。要了解更多信息,请参阅远程配置参数和条件。
第 6 步:获取并激活值
- 要从远程配置后端获取参数值,请调用
fetch()
方法。您在后端设置的任何值都将被提取并存储在远程配置对象中。 要使获取的参数值对您的应用程序可用,请调用
activate()
方法。对于您希望在一次调用中获取和激活值的情况,您可以使用
fetchAndActivate()
请求从远程配置后端获取值并将它们提供给应用程序:Kotlin+KTX
remoteConfig.fetchAndActivate() .addOnCompleteListener(this) { task -> if (task.isSuccessful) { val updated = task.result Log.d(TAG, "Config params updated: $updated") Toast.makeText(this, "Fetch and activate succeeded", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Fetch failed", Toast.LENGTH_SHORT).show() } displayWelcomeMessage() }
Java
mFirebaseRemoteConfig.fetchAndActivate() .addOnCompleteListener(this, new OnCompleteListener<Boolean>() { @Override public void onComplete(@NonNull Task<Boolean> task) { if (task.isSuccessful()) { boolean updated = task.getResult(); Log.d(TAG, "Config params updated: " + updated); Toast.makeText(MainActivity.this, "Fetch and activate succeeded", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "Fetch failed", Toast.LENGTH_SHORT).show(); } displayWelcomeMessage(); } });
由于这些更新的参数值会影响应用的行为和外观,因此您应该在确保用户流畅体验的时间激活获取的值,例如用户下次打开您的应用时。有关更多信息和示例,请参阅远程配置加载策略。
节流
如果应用程序在短时间内获取的次数太多,获取调用将受到限制并且 SDK 返回FirebaseRemoteConfigFetchThrottledException
。在 SDK 版本 17.0.0 之前,限制是在 60 分钟的窗口中有 5 个提取请求(较新的版本有更宽松的限制)。
在应用程序开发期间,您可能希望非常频繁地(每小时多次)获取和激活配置,以便在开发和测试应用程序时快速迭代。为了适应最多 10 名开发人员的项目的快速迭代,您可以在您的应用中临时设置一个具有较低最小提取间隔 ( setMinimumFetchIntervalInSeconds
) 的FirebaseRemoteConfigSettings
对象。
Remote Config 的默认最小提取间隔为 12 小时,这意味着在 12 小时的窗口中不会从后端提取配置超过一次,无论实际进行了多少次提取调用。具体来说,最小获取间隔按以下顺序确定:
-
fetch(long)
中的参数 FirebaseRemoteConfigSettings.setMinimumFetchIntervalInSeconds(long)
中的参数- 默认值 12 小时
要将最小提取间隔设置为自定义值,请使用FirebaseRemoteConfigSettings.Builder.setMinimumFetchIntervalInSeconds(long)
。
下一步
If you haven't already, explore the Remote Config use cases , and take a look at some of the key concepts and advanced strategies documentation, including: