Firebase Remote Config 使用入门


您可以使用 Firebase Remote Config 来定义应用中的参数并在云端更新它们的值。这样,您无需更新应用即可修改应用的外观和行为。本指南逐步介绍了入门步骤并提供了一些示例代码,所有这些代码都可以从 firebase/quickstart-ios GitHub 代码库中克隆或下载。

第 1 步:将 Remote Config 添加到您的应用

  1. 将 Firebase 添加到您的 Apple 项目(如果尚未添加)。

  2. 使用 Remote Config 时,如需根据用户属性和目标设备来有条件地定位应用实例,则需要用到 Google Analytics(分析)。请务必在项目中启用 Google Analytics(分析)

  3. 创建单例 Remote Config 对象,如以下示例所示:

    Swift

    remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()
    settings.minimumFetchInterval = 0
    remoteConfig.configSettings = settings

    Objective-C

    self.remoteConfig = [FIRRemoteConfig remoteConfig];
    FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] init];
    remoteConfigSettings.minimumFetchInterval = 0;
    self.remoteConfig.configSettings = remoteConfigSettings;

此对象用于存储应用内默认参数值,从 Remote Config 后端提取更新后的参数值,以及控制提取的值何时可供应用使用。

在开发期间,建议设置相对较短的最小提取间隔。如需了解详情,请参阅限制

第 2 步:设置应用内默认参数值

您可以在 Remote Config 对象中设置应用内默认参数值,以便应用在连接到 Remote Config 后端之前能够按预期运行,并且保证在后端中未设置任何值时可以使用默认值。

  1. 使用 NSDictionary 对象或 plist 文件定义一组参数名称和默认参数值。

    如果您已配置 Remote Config 后端参数值,则可以下载包含所有默认值的 plist 文件,并将其保存到您的 Xcode 项目中。

    REST

    curl --compressed -D headers -H "Authorization: Bearer token -X GET https://firebaseremoteconfig.googleapis.com/v1/projects/my-project-id/remoteConfig:downloadDefaults?format=PLIST -o RemoteConfigDefaults.plist
    

    Firebase 控制台

    1. 参数标签页中,打开菜单,然后选择下载默认值

    2. 看到提示时,启用 .plist (iOS),然后点击下载文件

  2. 使用 setDefaults: 将这些值添加到 Remote Config 对象。以下示例通过 plist 文件设置应用内默认值:

    Swift

    remoteConfig.setDefaults(fromPlist: "RemoteConfigDefaults")

    Objective-C

    [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];

第 3 步:获取要在应用中使用的参数值

现在,您可以从 Remote Config 对象中获取参数值。如果您稍后在 Remote Config 后端中设置这些值,然后提取并激活它们,这些值便可以供您的应用使用。否则,您将获得使用 setDefaults: 方法配置的应用内参数值。如需获取这些值,请调用 configValueForKey: 方法,并传入参数键作为调用的实参。

第 4 步:设置参数值

使用 Firebase 控制台或 Remote Config 后端 API,您可以创建新的后端默认值,这些值会根据您希望的条件逻辑或用户定位条件来替换应用内的值。本部分介绍了在 Firebase 控制台中创建这些值的步骤。

  1. Firebase 控制台中打开您的项目。
  2. 从菜单中选择 Remote Config,查看 Remote Config 信息中心。
  3. 使用您在应用中指定的参数名称来定义参数。对于每个参数,您可以设置默认值(最终将替换应用内默认值),也可以设置条件值。如需了解详情,请参阅 Remote Config 参数和条件

第 5 步:提取并激活值

如需从 Remote Config 中提取参数值,请调用 fetchWithCompletionHandler:fetchWithExpirationDuration:completionHandler: 方法。系统将提取您在后端中设置的所有值,并将其缓存在 Remote Config 对象中。

如果要在一次调用中提取并激活值,请使用 fetchAndActivateWithCompletionHandler:

以下示例从 Remote Config 后端(而非缓存的值)中提取值,并调用 activateWithCompletionHandler: 以使其可供应用使用:

Swift

remoteConfig.fetch { (status, error) -> Void in
  if status == .success {
    print("Config fetched!")
    self.remoteConfig.activate { changed, error in
      // ...
    }
  } else {
    print("Config not fetched")
    print("Error: \(error?.localizedDescription ?? "No error available.")")
  }
  self.displayWelcome()
}

Objective-C

[self.remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
    if (status == FIRRemoteConfigFetchStatusSuccess) {
        NSLog(@"Config fetched!");
      [self.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) {
        if (error != nil) {
          NSLog(@"Activate error: %@", error.localizedDescription);
        } else {
          dispatch_async(dispatch_get_main_queue(), ^{
            [self displayWelcome];
          });
        }
      }];
    } else {
        NSLog(@"Config not fetched");
        NSLog(@"Error %@", error.localizedDescription);
    }
}];

由于这些更新后的参数值会影响应用的行为和外观,因此您应在提取后谨慎选择激活这些值的时机,以确保为用户提供流畅的体验,例如在用户下次打开您的应用时激活。如需更多信息和示例,请参阅 Remote Config 加载策略

第 6 步:实时监听更新

提取参数值后,您可以使用实时 Remote Config 监听 Remote Config 后端中的更新。有更新可用时,实时 Remote Config 会向关联设备发送信号,并会在有新的 Remote Config 版本发布后自动提取更改内容。

适用于 Apple 平台的 Firebase SDK v10.7.0 及更高版本支持实时更新。

  1. 在您的应用中,调用 addOnConfigUpdateListener 以开始监听更新并自动提取任何新参数值或更新后的参数值。以下示例会监听更新,并在 activateWithCompletionHandler 被调用时使用新提取的值显示更新后的欢迎辞。

    Swift

    remoteConfig.addOnConfigUpdateListener { configUpdate, error in
      guard let configUpdate, error == nil else {
        print("Error listening for config updates: \(error)")
      }
    
      print("Updated keys: \(configUpdate.updatedKeys)")
    
      self.remoteConfig.activate { changed, error in
        guard error == nil else { return self.displayError(error) }
        DispatchQueue.main.async {
          self.displayWelcome()
        }
      }
    }
    

    Objective-C

    __weak __typeof__(self) weakSelf = self;
    [self.remoteConfig addOnConfigUpdateListener:^(FIRRemoteConfigUpdate * _Nonnull configUpdate, NSError * _Nullable error) {
      if (error != nil) {
        NSLog(@"Error listening for config updates %@", error.localizedDescription);
      } else {
        NSLog(@"Updated keys: %@", configUpdate.updatedKeys);
    
        __typeof__(self) strongSelf = weakSelf;
        [strongSelf.remoteConfig activateWithCompletion:^(BOOL changed, NSError * _Nullable error) {
          if (error != nil) {
            NSLog(@"Activate error %@", error.localizedDescription);
          }
    
          dispatch_async(dispatch_get_main_queue(), ^{
            [strongSelf displayWelcome];
          });
        }];
      }
    }];
    
  2. 下次有新版本的 Remote Config 发布时,运行应用并监听更改的设备便会调用完成处理程序。

限制

如果应用在短时间内提取次数太多,则提取调用会受到限制,并且 SDK 会返回 FIRRemoteConfigFetchStatusThrottled。在 SDK 6.3.0 版本之前,应用在 60 分钟内最多可以发送 5 次提取请求(在较新版本中,此限制更为宽松)。

在应用开发期间,您可能需要更频繁地提取以非常频繁地刷新缓存(每小时很多次),从而可以在开发和测试应用时进行快速迭代。在服务器上更新配置时,实时 Remote Config 更新会自动绕过缓存。为了让具有众多开发者的项目快速迭代,您可以在应用中临时添加一个具有较短最小提取间隔 (MinimumFetchInterval) 的 FIRRemoteConfigSettings 属性。

Remote Config 默认和建议的生产提取间隔为 12 小时,这意味着无论实际上调用了多少次提取方法,在 12 小时的时间段内最多从后端提取一次配置。具体而言,系统按以下顺序确定最小提取间隔:

  1. fetch(long) 中的参数
  2. FIRRemoteConfigSettings.MinimumFetchInterval 中的参数
  3. 默认值 12 小时

后续步骤

浏览 Remote Config 使用场景并查看一些关键的概念和高级策略文档(如果您尚未这样做),包括: