Search code examples
androidfirebasefirebase-remote-config

Not instant Remote Config value android?


implementation 'com.google.firebase:firebase-config:11.8.0'

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

    mFirebaseRemoteConfig.setConfigSettings(configSettings);

    mFirebaseRemoteConfig.fetch(0).addOnCompleteListener(this, new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                mFirebaseRemoteConfig.activateFetched();
                String appDefaultColor = mFirebaseRemoteConfig.getString(FIREBASE_REMOTE_CONFIG_DEFAULT_COLOR);
                if (appDefaultColor != null && appDefaultColor.length() > 0) {
                    System.out.println("==== appDefaultColor : " + appDefaultColor);
                }
            }

        }
    });

public static String FIREBASE_REMOTE_CONFIG_DEFAULT_COLOR = "project_default_theme_color";

here is my implementation of Firebase remote config. As above my code explanation, project4_default_theme_color, i get the value from firebase, But the situation is that i change that value from Firebase remote config , but i did't get.

My firebase remote config Key project_default_theme_color and value is #f04030 and Publish Changes.is any wrong in this?


Solution

  • Follow below instruction to resolve this issue

    • Update your firebase library version

      implementation 'com.google.firebase:firebase-config:19.1.0'
      implementation 'com.google.firebase:firebase-core:17.2.1'
      
    • Initialize FirebaseRemoteConfig

      FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
      
    • Set firebaseRemoteConfig parameter default value

      firebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults);
      
    • Add below code in remote_config_defaults.xml

      <?xml version="1.0" encoding="utf-8"?>
      <defaultsMap>
          <entry>
              <key>your_key</key>
              <value>defaultValue</value>
      </entry>
      

    • Add this code in your java file

         firebaseRemoteConfig.fetch(cacheTimeDuration)
              .addOnCompleteListener(new OnCompleteListener<Void>() {
                  @Override
                  public void onComplete(@NonNull Task<Void> task) {
      
                      String errorString = "";
      
                      if (task.isSuccessful()) {
                          firebaseRemoteConfig.activate();
                          errorString = " task is successful  ";
                      } else {
                          errorString = "task is canceled";
                      }
                      Log.i(TAG, "onComplete: error " + errorString);
                      Log.i(TAG, " Get firebase remote config value " + firebaseRemoteConfig.getString("your_key"));
      
                  }
              });
      
    • Note :

      • If your Apk is debug then use this method .fetch(cacheTimeDuration)
      • If your Apk is Release then use this method .fetch()