Firebase Remote Config 遠端配置
Firebase遠端配置就是在Firebase後臺儲存一些鍵值對,然後在app內向Firebase請求並使用這些鍵值對。當我們需要更改這些鍵值對時,就在Firebase後臺更改,當app下一次向Firebase請求時,獲取到的就是我們更改後的值。這樣就實現了動態更新app配置
文章目錄
- Firebase與專案連結
- 點擊Cloud Firestore將library與google-services.json導入進專案
- Firebase console建立設定你要的key&value
- 使用應用程式內預設值 需要透過xml去添加
- RemoteConfig調用
- 使用預設資料
- 使用後台更新的資料
- 效果展示
1.Firebase與專案連結

2.點擊Cloud Firestore將library與google-services.json導入進專案

3.Firebase console建立設定你要的key&value
建立設定

儲存

發布變更

4.使用應用程式內預設值 需要透過xml去添加
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
<entry>
<key>title</key>
<value>Remote Config</value>
</entry>
<entry>
<key>message</key>
<value>Remote Config Message</value>
</entry>
</defaultsMap>
5.RemoteConfig調用
val message = findViewById<TextView>(R.id.message)
//取得RemoteConfig KTX版本
val remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
//最小獲取時間(秒) 不設置只要後台更新就同步資料
minimumFetchIntervalInSeconds = 10
}
remoteConfig.setConfigSettingsAsync(configSettings)
remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)
//取得改變資料
remoteConfig.fetchAndActivate()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
title = remoteConfig.getString("title")
message.text = remoteConfig.getString("message")
}
}
6.使用預設資料

7.使用後台更新的資料

8.效果展示
