24
22

More than 5 years have passed since last update.

[Android] Firebase Remote Configを試してみる

Last updated at Posted at 2016-06-28

Firebase Remote Config概要

Firebaseの機能の一つでFirebaseコンソール上から値を操作して簡単にアプリに反映させることができます。
booleanやString、Long型でkeyを指定してvalueを受け取るという単純なしくみです。
無料で使えます。

※書いてる途中で、こちらの記事Firebase Remote Config for Androidの勘所が公開されまして内容が被ってしまったのですが、もったいないので公開します。。。

使い方

早速サンプルを参考に実装します。
Firebaseの基本は組み込まれている前提です。

gradleのdependenciesに追記

build.gradle
    compile 'com.google.firebase:firebase-config:9.0.2'

下記はfabを押すとremoteConfigの値を出力し、buttonを押すとremoteConfigの値を更新する簡単なサンプルです。

    FirebaseRemoteConfig mFirebaseRemoteConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //instance取得
        mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

        //デベロッパーモード指定
        FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(BuildConfig.DEBUG)
                .build();
        mFirebaseRemoteConfig.setConfigSettings(configSettings);

        //デフォルトの値を読み込む
        mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, mFirebaseRemoteConfig.getString("test_string"), Snackbar.LENGTH_LONG).setAction("Action", null).show();
            }
        });

        Button button = (Button) findViewById(R.id.fetch_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更新
                fetch();
            }
        });

    }

    private void fetch() {
        int cacheTime = mFirebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled() ? 0 : 60 * 60;// Cache時間設定(秒)
        mFirebaseRemoteConfig.fetch(cacheTime)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            //値を反映
                            mFirebaseRemoteConfig.activateFetched();
                        } else {
                            //fetch失敗
                        }
                    }
                });
    }


remote_config_defaults.xml
<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
        <key>test_string</key>
        <value>文字列だよ</value>
    </entry>
    <entry>
        <key>test_bool</key>
        <value>false</value>
    </entry>
    <entry>
        <key>test_long</key>
        <value>100</value>
    </entry>
</defaultsMap>


値の取得

設定した値はこんな感じで簡単に取れます。
実装でgetBooleanのkeyにコンソールで文字列や数字を指定するとfalseが返ってくる、getLongのkeyにコンソールで文字列を入れると0が返ってくるようです。

 mFirebaseRemoteConfig.getString("test_string");
 mFirebaseRemoteConfig.getBoolean("test_bool");
 mFirebaseRemoteConfig.getLong("test_long");

第二引数が入れられるようなのですが使い方がわからなかったです
mFirebaseRemoteConfig.getString("test_string","name_space");

値の設定(コンソール)

firebaseのコンソール上で値を設定します。

スクリーンショット 2016-06-28 13.26.20.png

値は条件を作って、条件別に設定できます。
スクリーンショット 2016-06-28 13.29.18.png

条件の適応
スクリーンショット 2016-06-28 13.30.32.png

デベロッパーモード

サンプルのコメントには普通のリクエストは1時間に5リクエストの制限があるようです。
デベロッパーモードだとその上限が増える(らしい)

FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
                .setDeveloperModeEnabled(BuildConfig.DEBUG)
                .build();
mFirebaseRemoteConfig.setConfigSettings(configSettings);

値の更新

自分で更新してあげないとコンソールで設定した値が反映されません。
fetchの制限があるので適切なタイミングで実行する必要があります。

    mFirebaseRemoteConfig.fetch(cacheTime).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
         public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                //値を反映
                mFirebaseRemoteConfig.activateFetched();
            } else {
                //fetch失敗
            }
        }
    });

デフォルト値を読み込む

Mapでもできるようですが、xmlで設定する方法もあります。

mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);

雑感

  • 自動でfetchしてくれるかと思っていたが自分でfetchしてactivateFetchedしないと反映されない
  • UIの色などを変えてABテストするなどの紹介があったのでそういう機能があると思っていたが、値取得だけでその先は自分で実装しないといけない→実装ミスると落ちるとかありそう
  • アナリティクスと連動していればユーザーの属性(地域、デバイス、言語、アクティブ)率などを手軽に設定して値を変更してABテストできるのが一番便利だと思う。逆にアナリティクスが入ってないのであればあまり。。
24
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
22