Remote Config

Change the behavior and appearance of your app without publishing an app update.

Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update. When using Remote Config, you create in-app default values that control the behavior and appearance of your app.

Before you start, please read about Firebase Remote Config in the official documentation:

Before you begin

For Firebase projects created before the March 7, 2018 release of the Remote Config REST API, you must enable the API in the Google APIs console.

  1. Open the Firebase Remote Config API page in the Google APIs console.
  2. When prompted, select your Firebase project. (Every Firebase project has a corresponding project in the Google APIs console.)
  3. Click Enable on the Firebase Remote Config API page.

You can work with your Firebase application’s Remote Config by invoking the getRemoteConfig() method of your Firebase instance:

use Kreait\Firebase;

$firebase = (new Firebase\Factory())->create();
$remoteConfig = $firebase->getRemoteConfig();

Get the Remote Config

$template = $remoteConfig->get();
echo json_encode($template, JSON_PRETTY_PRINT);

Create a new Remote Config

use Kreait\Firebase\RemoteConfig;

$template = RemoteConfig\Template::new();

Add a condition

use Kreait\Firebase\RemoteConfig;

$germanLanguageCondition = RemoteConfig\Condition::named('lang_german')
    ->withExpression("device.language in ['de', 'de_AT', 'de_CH']")
    ->withTagColor(TagColor::ORANGE); // The TagColor is optional

$template = $template->withCondition($germanLanguageCondition);

Add a parameter

use Kreait\Firebase\RemoteConfig;

$welcomeMessageParameter = Parameter::named('welcome_message')
        ->withDefaultValue('Welcome!')
        ->withDescription('This is a welcome message') // optional
;

Conditional values

use Kreait\Firebase\RemoteConfig;

$germanLanguageCondition = RemoteConfig\Condition::named('lang_german')
    ->withExpression("device.language in ['de', 'de_AT', 'de_CH']");

$germanWelcomeMessage = RemoteConfig\ConditionalValue::basedOn($germanLanguageCondition, 'Willkommen!');

$welcomeMessageParameter = Parameter::named('welcome_message')
        ->withDefaultValue('Welcome!')
        ->withConditionalValue($germanWelcomeMessage);

$template = $template
    ->withCondition($germanLanguageCondition)
    ->withParameter($welcomeMessageParameter);

Note

When you use a conditional value, make sure to add the corresponding condition to the template first.

Validation

Usually, the SDK will protect you from creating an invalid Remote Config template in the first place. If you want to be sure, you can validate the template with a call to the Firebase API:

use Kreait\Firebase\Exception\RemoteConfig\ValidationFailed;

try {
    $remoteConfig->validate($template);
} catch (ValidationFailed $e) {
    echo $e->getMessage();
}

Note

The ValidationFailed exception extends Kreait\Firebase\Exception\RemoteConfigException, so you can safely use the more generic exception type as well.

Publish the Remote Config

use Kreait\Firebase\Exception\RemoteConfigException

try {
    $remoteConfig->publish($template);
} catch (RemoteConfigException $e) {
    echo $e->getMessage();
}

Change history

Since August 23, 2018, Firebase provides a change history for your published Remote configs.

Versions

The following properties are available from a Kreait\Firebase\RemoteConfig\Version object:

$version->versionNumber();
$version->user(); // The user/service account the performed the change
$version->description();
$version->updatedAt();
$version->updateOrigin();
$version->updateType();
$version->rollBackSource();

List versions

To enhance performance and prevent memory issues when retrieving a huge amount of users, this methods returns a Generator.

$users = ;

foreach ($auth->listVersions() as $version) {
    /** @var \Kreait\Firebase\RemoteConfig\Version $user */
    // ...
}
// or
array_map(function (\Kreait\Firebase\RemoteConfig\Version $user) {
    // ...
}, iterator_to_array($auth->listVersions()));

Get a specific version

$version = $remoteConfig->getVersion($versionNumber);

Rollback to a version

$template = $remoteConfig->rollbackToVersion($versionNumber);