基于用户属性的 Firebase 受众

Firebase audience based on user properties

我需要仅针对之前选定的一组用户测试我的应用程序的某些功能。

我在 user_id exactly matches 123456 中创建了一个受众。 123456 是我自己的ID。

在 Remote Config 中,我创建了一个条件来匹配上面受众中的用户。

然后我在 Remote Config 中创建了一个名为 feature_available 的参数,对于条件,我将 return 值设置为 true。默认值为 false.

在我的应用程序中,我设置了 Firebase:

    FIRApp.configure()

    let remoteConfig = FIRRemoteConfig.remoteConfig()
    if let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: false) {
        remoteConfig.configSettings = remoteConfigSettings
    }
    remoteConfig.setDefaultsFromPlistFileName("FirebaseRemoteConfigDefaults")

并设置用户ID:

    FIRAnalytics.setUserID("123456")

然后我从 Firebase 获取:

    var expirationDuration: Double
    // If in developer mode cacheExpiration is set to 0 so each fetch will retrieve values from the server.
    expirationDuration = remoteConfig.configSettings.isDeveloperModeEnabled ? 0 : 3600

    remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void in
        if status == .success {
            remoteConfig.activateFetched()
        } else {
            assertionFailure("Firebase config not fetched. Error \(error!.localizedDescription)")
        }
    }

我做的最后一件事是从 Firebase 获取值并检查我是否启用了该功能:

    let featureIsAvailable = remoteConfig["feature_available"].boolValue
    if featureIsAvailable { ... }

问题是,每次来自 Firebase 的值 return 都是 false,我无法设法将它设为 return 匹配的正确值我创建的观众。 我还尝试设置用户 属性 而不是使用 setUserID() 并得到相同的结果。 有什么建议吗?

我以前 运行 遇到过类似的问题,有时需要一段时间才能完成提取。成功修复配置后,需要检查功能是否可用。希望这样的东西也适合你:

var featureIsAvailable : Bool?

override func viewDidLoad() {
    super.viewDidLoad()

    configureRemoteConfig()
    fetchConfig()      
}

func configureRemoteConfig() {
    remoteConfig = FIRRemoteConfig.remoteConfig()
    // Create Remote Config Setting to enable developer mode.
    // Fetching configs from the server is normally limited to 5 requests per hour.
    // Enabling developer mode allows many more requests to be made per hour, so developers
    // can test different config values during development.
    let remoteConfigSettings = FIRRemoteConfigSettings(developerModeEnabled: true)
    remoteConfig.configSettings = remoteConfigSettings!
    remoteConfig.setDefaultsFromPlistFileName("RemoteConfigDefaults")
}

func fetchConfig() {
    var expirationDuration: Double = 3600
    // If in developer mode cacheExpiration is set to 0 so each fetch will retrieve values from
    // the server.
    if (self.remoteConfig.configSettings.isDeveloperModeEnabled) {
        expirationDuration = 0
    }

    // cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously
    // fetched and cached config would be considered expired because it would have been fetched
    // more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless
    // throttling is in progress. The default expiration duration is 43200 (12 hours).
    remoteConfig.fetch(withExpirationDuration: expirationDuration) { (status, error) in
        if (status == .success) {
            print("Config fetched!")
            self.remoteConfig.activateFetched()

            let featureIsAvailable = self.remoteConfig["feature_available"]

            if (featureIsAvailable.source != .static) {
                self.featureIsAvailable = featureIsAvailable.boolValue
                print("should the feature be available?", featureIsAvailable!)
            }
        } else {
            print("Config not fetched")
            print("Error \(error)")
        }
        self.checkIfFeatureIsAvailable()
    }
}

func checkIfFeatureIsAvailable() {
    if featureIsAvailable == false {
        // Don't show new feature
    } else {
        // Show new feature
    }
}

我向 Firebase 团队发送了一封电子邮件请求支持,他们告诉我这个问题是 Xcode 8(.0 和 .1)中使用 Swift 的错误。 更新到今天发布的最新版本 8.2,解决了这个问题。