Help us understand the problem. What is going on with this article?

[Kotlin]Firebase Remote Configの実装

 はじめに

  • 公式はこちら
  • メンテナンスモードや強制アップデートの実装に使える

画面

Firebase consoleでRemoteConfigの設定

スクリーンショット 2020-05-01 11.09.34.png

FirabseConsoleのRemoteConfigで設定する値は2つの為、それぞれ表示する用のTextViewを2つ用意
スクショは実装結果です

File.jpg

actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/main_first_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first text" />

    <TextView
        android:id="@+id/main_second_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="second text" />

</LinearLayout>

実装

build.gradle(app)
dependencies {
   〜

    // Remote config
    implementation 'com.google.firebase:firebase-config:19.1.4'
    // Remote Config (Kotlin)
    implementation 'com.google.firebase:firebase-config-ktx:19.1.4'

Activity

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var remoteConfig: FirebaseRemoteConfig

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // firebase-config-ktx により簡潔に書ける
        remoteConfig = Firebase.remoteConfig

        // debug modeの有効化
        val configSettings = remoteConfigSettings {
            minimumFetchIntervalInSeconds = 3600 // fetch間隔 。頻繁に取得できないので、debug時は短く設定する
        }
        remoteConfig.setConfigSettingsAsync(configSettings)

        // デフォルト値設定
        remoteConfig.setDefaultsAsync(R.xml.remote_config_defaults)

        // apply default
        main_first_text.text = remoteConfig[LOADING_PHRASE_CONFIG_KEY].asString()
        main_second_text.text = remoteConfig[LOADING_PHRASE_CONFIG_KEY].asString()

        fetchWelcome()
    }

    private fun fetchWelcome() {
        remoteConfig.fetchAndActivate()
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    val updated = task.result
                    Timber.d("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()
            }
    }

    private fun displayWelcomeMessage() {
        // 失敗してたらxmlより反映。成功してたら firebase console にて設定した値
        main_first_text.text = remoteConfig.getBoolean(WELCOME_MESSAGE_CAPS_KEY).toString()
        main_second_text.text = remoteConfig.getString(WELCOME_MESSAGE_KEY)
    }

    companion object {
        // Remote Config keys
        private const val LOADING_PHRASE_CONFIG_KEY = "loading_phrase"
        private const val WELCOME_MESSAGE_CAPS_KEY = "welcome_message_caps"
        private const val WELCOME_MESSAGE_KEY = "welcome_message"
    }
}

 所感

ソースコードはこちら

ijichi_y
株式会社Moapp 代表 Android案件をメイン iOS案件も募集してます。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした