DOUG STEVENSON: Hey!
Thanks for tuning in.
My name is Doug Stevenson, and in this Firecast,
I'll get you started with Firebase Remote Config.
You may have noticed that it's kind of a pain to build, test,
and publish a new version of your app just
to change some hardcoded value in it.
But with Firebase Remote Config, you
could instead change that value in the Firebase console
and have that affect your app without having to republish it.
Remote Config gives you the ability
to define a set of named parameters
and assign them values in the console.
Then in your app, you can fetch those values
and use them as you need.
Any time you change a parameter's value
in the Firebase console, it will be made available to your app.
If you want to make use of Firebase Remote Config,
it's just a few lines of code to fetch the values
and put them into place.
The first thing to do is integrate Firebase
into your app.
If you haven't done that yet, check out this video first
where I walk you through the basic integration,
then come back here to continue with Remote Config.
I already have an app that's using Firebase,
so I'll add Remote Config to that
and use it to change some of the behavior from the console.
Here's a simple app that lets you type text into an edit text
widget, and that gets overlaid on an image of you
with the Firebase logo on it, kind of like a meme.
Right now, there's no limit to the number
of characters you can type into it,
which can be kind of awkward.
So what I want to do is limit the number
of characters that can be typed into the edit text widget
so things don't get out of control.
If I switch into my project in Android Studio
and open up MainActivity, I can add a few lines of code
to set the max length of that edit text.
You can see I've hardcoded a value of 5 right here for now,
but I'd rather be able to change that whenever
I want without having to rebuild the app.
I'll do a bit of coding for that.
First, I need to configure my app
to include the Remote Config SDK so I can move
my hardcoded settings into it.
I'll open my app's build.gradle and find the project
dependencies block.
Then I'll add the dependency for Firebase Remote Config.
The latest version at the time of this recording is 10.0.1,
but you should find and use the latest version.
After that, I'll sync the project
to make the APIs available.
OK, now I need to convert that hardcoded value
into a Remote Config parameter.
In MainActivity, I can get a reference
to the singleton Remote Config object
and store that in a member field.
Then in On Create, I'll do three things.
First, I'll configure Remote Config
to enable developer mode.
I'll say more about developer mode later.
Second, I'll define a new parameter
and assign it a default value.
Here, I'm using a map to tell Remote Config about my default.
I'll give my parameter a name of overlay_max_chars
and give it a default value of 5.
The default will be used until the app is
able to fetch a change to this parameter from the console.
Third, I'll call the fetch method
to tell Remote Config to download all the parameters I
defined in the console.
I'm passing it a zero for now, which is a cache expiration
time, and I'll tell you what that means later.
Fetch returns immediately with a task object, which tracks
the progress of the download.
Then I'll add a listener on it, which
will get invoked when the fetch completes successfully.
In the success callback, I'll choose
to activate the new parameters using
the activateFetched method.
Any changes to the fetched parameters
won't be accessible until they are activated like this.
Also, I'll call the method from earlier that updates the max
length of the edit text.
And finally in that method, I'll ask Remote Config
for the current value of the parameter
instead of hardcoding it there.
All right.
Let's see how it works.
When I type text into the box, you
can see that it's limited to the default of five characters
that I assigned in the map of default values.
Typing more characters doesn't do anything,
and that's what I expected.
So what if I want to change the setting dynamically
at the Firebase console?
Let's look at the console for this project.
In the list of features on the left, I'll click Remote Config.
It's asking me to add my first parameter,
so I'll go ahead and do that.
I'll call it overlay_max_chars, exactly
as I named it in my code, and give it a value of 10.
When I click Add Parameter, it records the value
and lets me know that I should publish my changes when
I'm ready.
This parameter won't be seen in my app
until I push the Publish button.
So I'll go ahead and do that now.
And if I switch back into the app,
leave it, then launch it again, I
can see that the new limit of 10 characters
was fetched and activated.
Now, with my app using Remote Config,
I can change the max number of characters for the overlay text
as often as I want.
All I have to do is change its value in the console
and publish the change.
You should know that you can store different types of values
in Remote Config.
You can enter Boolean, double, long, and string values
into the console, and each type is obtained through the client
API with a different method.
Since every value is stored as a string,
you should take care to format them correctly
so they can be parsed by the SDK when you access them.
So there are a couple of things I mentioned before that
need a little more explanation.
First, fetched values are normally cached for 12 hours
before another call to fetch will actually
make a request for new values.
The code I wrote to set the cache expiration to zero
is helpful during development to let me get new values
every time I make a call to fetch.
This makes frequent value changes at the console
easier to test, but you shouldn't publish your app
like this.
Instead, you should only use the setting during development.
In this code, I'm using the BuildConfig object
to determine the best cache expiration.
Second, you should know that the SDK normally
imposes rate limiting on your request to fetch.
So a poorly behaved app can't cause too much network traffic.
When your app is being throttled like this, calls to fetch
won't work for a while.
But during development, you can enable developer mode
to bypass this restriction which makes
it possible to test calling fetch repeatedly
for new values.
Again, only do this during development
so you don't have problems in production.
Third, you should give some consideration
about when you call fetch to get new values
and when you call activateFetched to make
those new values available.
If you only call fetch once in your main activity,
it could be a long time before that happens again,
and you'll end up with stale values.
Also, if you activate some new parameters that change the user
experience drastically, that can be
kind of awkward for your users.
To best deal with these issues, you can watch this other video
by my colleague Todd Kerpelman.
OK., Remote Config can do even more than what I've shown here.
It works in tandem with Firebase Analytics
to provide a way to perform AB testing
with the value of a parameter.
This helps you experiment to find out what
settings your users prefer.
Also you can provide different values
for a parameter based on factors like the user's analytics
audience or a user property.
This lets you provide a customized experience
for your app, depending on who's using it.
You can learn about these things and more in the Firebase Remote
Config documentation in the description below.
And that's it for this Firecast.
If you have any problems, you can leave a comment below
or contact us through one of our support channels.
Have fun remotely configuring your app,
and I'll see you next time.
[MUSIC PLAYING]