Remote Config
什么是 Remote Config?
Firebase 远程配置是指在 Firebase 后台存储一些键值对,在 app 中向 Firebase 请求并使用这些键值对。当我们需要更改这些键值对时,在 Firebase 后台更改即可。当 app 下一次向 Firebase 请求时,获取到的就是我们更改后的值。这样就实现了动态更新 app 配置。
集成 Remote Config
方式1:自动 Add Firebase using the Firebase Assistant
Tools > Firebase.
方式2:手动 Add Firebase using the Firebase console
- 下载
google-services.json
到 app 根目录 - root-level (project-level) 添加 gms Gradle 插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| buildscript {
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
dependencies {
...
// Add the dependency for the Google services Gradle plugin
classpath 'com.google.gms:google-services:4.3.15'
}
}
allprojects {
...
repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}
}
|
- module (app-level) 引入 GMS 插件
1
2
3
4
5
6
7
| plugins {
id 'com.android.application'
// Add the Google services Gradle plugin
id 'com.google.gms.google-services'
...
}
|
- 添加 sdk 到 module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| dependencies {
// ...
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:32.0.0')
// When using the BoM, you don't specify versions in Firebase library dependencies
// Add the dependency for the Firebase SDK for Google Analytics
implementation 'com.google.firebase:firebase-analytics-ktx'
// TODO: Add the dependencies for any other Firebase products you want to use
// See https://firebase.google.com/docs/android/setup#available-libraries
// For example, add the dependencies for Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth-ktx'
implementation 'com.google.firebase:firebase-firestore-ktx'
}
|
1
2
3
4
5
6
7
8
9
| dependencies {
// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:32.0.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'
}
|
1
2
3
4
5
| val remoteConfig: FirebaseRemoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 3600 // 最小获取间隔时间,默认12h
}
remoteConfig.setConfigSettingsAsync(configSettings)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>key_test_remote_config</key>
<value>here is client default value</value>
</entry>
<entry>
<key>has_discount</key>
<value>false</value>
</entry>
<entry>
<key>main_color</key>
<value>red</value>
</entry>
</defaultsMap>
|
1
| remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
|
Get parameter values to use in your app
Firebase 后台配置值
- Parameter name 配置 key
- Data Type 配置类型
- Default value 配置 value
- User in-app default 配置的话,就会从
remote_config_defaults.xml
获取默认值,否则走的是 Default value
Fetch and activate values
Firebase 拉取远程配置后,拉取的键值对默认在本地缓存 12 小时,12 小时后才去重新拉取。如果想要修改缓存时间,在 fetch 中添加时间参数即可,使用 firebaseRemoteConfig.fetch(0),远程配置的值就会实时更新:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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()
}
|
实时监听更新
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| remoteConfig.addOnConfigUpdateListener(object : ConfigUpdateListener {
override fun onUpdate(configUpdate : ConfigUpdate) {
Log.d(TAG, "Updated keys: " + configUpdate.updatedKeys);
if (configUpdate.updatedKeys.contains("welcome_message")) {
remoteConfig.activate().addOnCompleteListener {
displayWelcomeMessage()
}
}
}
override fun onError(error : FirebaseRemoteConfigException) {
Log.w(TAG, "Config update error with code: " + error.code, error)
}
})
|
下次有新版本的 Remote Config 发布时,运行应用并监听更改的设备便会调用 ConfigUpdateListener。
默认值
- Firebase Remote config 中没有配置的 key 的值,默认值
- 有 key 但没取到,也是默认值
- 不同类型的默认值
1
2
3
4
5
6
7
8
9
10
| /** The static default string value for any given key. */
public static final String DEFAULT_VALUE_FOR_STRING = "";
/** The static default long value for any given key. */
public static final long DEFAULT_VALUE_FOR_LONG = 0L;
/** The static default double value for any given key. */
public static final double DEFAULT_VALUE_FOR_DOUBLE = 0D;
/** The static default boolean value for any given key. */
public static final boolean DEFAULT_VALUE_FOR_BOOLEAN = false;
/** The static default byte array value for any given key. */
public static final byte[] DEFAULT_VALUE_FOR_BYTE_ARRAY = new byte[0];
|