با Firebase Remote Config شروع کنید


شما می‌توانید از Firebase Remote Config برای تعریف پارامترها در برنامه خود و به‌روزرسانی مقادیر آنها در فضای ابری استفاده کنید، که به شما امکان می‌دهد ظاهر و رفتار برنامه خود را بدون توزیع به‌روزرسانی برنامه تغییر دهید.

کتابخانه Remote Config برای ذخیره مقادیر پیش‌فرض پارامترها در برنامه، دریافت مقادیر به‌روزرسانی‌شده پارامترها از بک‌اند Remote Config و کنترل زمان در دسترس قرار گرفتن مقادیر دریافتی در برنامه شما استفاده می‌شود. برای کسب اطلاعات بیشتر، به «استراتژی‌های بارگذاری Remote Config» مراجعه کنید.

مرحله ۱: اضافه کردن فایربیس به برنامه

قبل از اینکه بتوانید از Remote Config استفاده کنید، باید:

  • پروژه ++C خود را ثبت کنید و آن را برای استفاده از Firebase پیکربندی کنید.

    اگر پروژه ++C شما از قبل از Firebase استفاده می‌کند، پس از قبل برای Firebase ثبت و پیکربندی شده است.

  • کیت توسعه نرم‌افزاری Firebase C++ را به پروژه C++ خود اضافه کنید.

توجه داشته باشید که اضافه کردن Firebase به پروژه C++ شما شامل وظایفی هم در کنسول Firebase و هم در پروژه C++ باز شما می‌شود (برای مثال، شما فایل‌های پیکربندی Firebase را از کنسول دانلود می‌کنید، سپس آنها را به پروژه C++ خود منتقل می‌کنید).

مرحله ۲: Remote Config به برنامه خود اضافه کنید

اندروید

بعد از اینکه Firebase را به برنامه خود اضافه کردید:

  1. یک برنامه Firebase ایجاد کنید و محیط JNI و Activity را به آن ارسال کنید:

    app = ::firebase::App::Create(::firebase::AppOptions(), jni_env, activity);

  2. کتابخانه Remote Config همانطور که نشان داده شده است، مقداردهی اولیه کنید:

    ::firebase::remote_config::Initialize(app);

آی‌او‌اس+

بعد از اینکه Firebase را به برنامه خود اضافه کردید:

  1. ایجاد یک برنامه فایربیس:

    app = ::firebase::App::Create(::firebase::AppOptions());

  2. کتابخانه Remote Config همانطور که نشان داده شده است، مقداردهی اولیه کنید:

    ::firebase::remote_config::Initialize(app);

مرحله ۳: تنظیم مقادیر پیش‌فرض پارامترها در برنامه

شما می‌توانید مقادیر پیش‌فرض پارامترهای درون‌برنامه‌ای را در شیء Remote Config تنظیم کنید، به طوری که برنامه شما قبل از اتصال به Remote Config backend طبق انتظار رفتار کند، و اگر هیچ مقداری در backend تنظیم نشده باشد، مقادیر پیش‌فرض در دسترس باشند.

  1. مجموعه‌ای از نام پارامترها و مقادیر پیش‌فرض پارامترها را با استفاده از یک شیء ConfigKeyValue* یا یک شیء ConfigKeyValueVariant* با اندازه آرایه تعریف کنید.

    اگر قبلاً مقادیر پارامترهای backend مربوط به Remote Config را پیکربندی کرده‌اید، می‌توانید فایلی را که حاوی این جفت‌های کلید/مقدار است دانلود کنید و از آن برای ساخت شیء map خود استفاده کنید. برای اطلاعات بیشتر، به Download Remote Config template defaults مراجعه کنید.

  2. Add these values to the Remote Config object using SetDefaults() .

Step 4: Get parameter values to use in your app

Now you can get parameter values from the Remote Config object. If you set values in the Remote Config backend, fetched them, and then activated them, those values are available to your app. Otherwise, you get the in-app parameter values configured using SetDefaults() .

To get these values, call the method listed below that maps to the data type expected by your app, providing the parameter key as an argument:

Step 5: Set parameter values

  1. In the Firebase console , open your project.
  2. Select Remote Config from the menu to view the Remote Config dashboard.
  3. Define parameters with the same names as the parameters that you defined in your app. For each parameter, you can set a default value (which will eventually override the in-app default value) and conditional values. To learn more, see Remote Config parameters and conditions .

Step 6: Fetch and activate values

  1. To fetch parameter values from the Remote Config backend, call the Fetch() method. Any values that you set on the backend are fetched and cached in the Remote Config object.
  2. To make fetched parameter values available to your app, call the ActivateFetched()

Step 7: Listen for updates in real time

After you fetch parameter values, you can use real-time Remote Config to listen for updates from the Remote Config backend. Real-time Remote Config signals to connected devices when updates are available and automatically fetches the changes after you publish a new Remote Config version.

Real-time updates are supported by the Firebase C++ SDK v11.0.0+ and higher for Android and Apple platforms.

  1. In your app, call AddOnConfigUpdateListener to start listening for updates and automatically fetch any new or updated parameter values. The following example listens for updates and, when Activate is called, uses the newly fetched values to display an updated welcome message.
remote_config->AddOnConfigUpdateListener(
    [](firebase::remote_config::ConfigUpdate&& config_update,
       firebase::remote_config::RemoteConfigError remote_config_error) {
      if (remote_config_error != firebase::remote_config::kRemoteConfigErrorNone) {
        printf("Error listening for config updates: %d", remote_config_error);
      }
      // Search the `updated_keys` set for the key "welcome_message."
      // `updated_keys` represents the keys that have changed since the last
      // fetch.
      if (std::find(config_update.updated_keys.begin(),
                    config_update.updated_keys.end(),
                    "welcome_message") != config_update.updated_keys.end()) {
        remote_config->Activate().OnCompletion(
            [&](const firebase::Future& completed_future,
               void* user_data) {
              // The key "welcome_message" was found within `updated_keys` and
              // can be activated.
              if (completed_future.error() == 0) {
                DisplayWelcomeMessage();
              } else {
                printf("Error activating config: %d", completed_future.error());
              }
            },
            nullptr);
      }
    });

The next time you publish a new version of your Remote Config , devices that are running your app and listening for changes will call the config update listener.

مراحل بعدی

If you haven't already, explore the Remote Config use cases , and take a look at some of the key concepts and advanced strategies documentation, including: