What is Firebase Remote Config?
– Firebase Remote Config is a cloud service.
– That lets you change the behavior and appearance of your app without requiring users.
– You create in-app default values that control the behavior and appearance of your app.
– You can later use the firebase console or the Remote Config to override in-app default values for all app users or for segments of your user base.
How does it work?
– You define in-app default settings that govern the functionality and presentation of your app. You may subsequently utilise the Firebase interface or Remote Config to alter in-app default values for all app users or subsets of your user base.
Follow the below step for setup FIREBASE REMOTE CONFIG :
1> Open firebase console and create the project in firebase.
2> Firebase > Remote Config click then add getting your app key and value.
3> Download the google-service.json file.
4> This file add to your project.
5> Then go to your app code and add initialization firebase.
6> Add the below dependency for firebase remote config.
// add classpath and plugin dependencies { classpath 'com.android.tools.build:gradle:4.1.3' classpath 'com.google.gms:google-services:4.3.10' } apply plugin: 'com.google.gms.google-services' // dependency implementation 'com.google.firebase:firebase-analytics:18.0.3' implementation 'com.google.firebase:firebase-config:20.0.4'
7> Then use the below code in your app.
public class MainActivity extends AppCompatActivity { private static final String PRICE_DATA = "price"; private FirebaseRemoteConfig mFirebaseRemoteConfig; private TextView mPriceTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPriceTextView = findViewById(R.id.txt); mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().setMinimumFetchIntervalInSeconds(3600).build(); mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings); mFirebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config_defaults); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fetchDiscount(); } }); } private void fetchDiscount() { mPriceTextView.setText(R.string.loading); long cacheExpiration = 3600; mFirebaseRemoteConfig.fetch(cacheExpiration).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d("FETCH", getString(R.string.succeeded)); mFirebaseRemoteConfig.activate(); } else { Log.d("FETCH", getString(R.string.failed)); } displayPrice(); } }); } private void displayPrice() { String finalPrice2 = mFirebaseRemoteConfig.getString(PRICE_DATA); mPriceTextView.setText("Your Price is : " + finalPrice2); } }
Main Layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.config.MainActivity"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/btn" android:gravity="center" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:text="Checking your price…" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <Button android:id="@+id/btn" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#000000" android:text="@string/fetch" android:textAllCaps="false" android:textColor="#FFFFFF" /> </RelativeLayout>
8> Below the output screenshot check this.
Hii