简体   繁体   English

userId的Firebase远程配置条件

[英]Firebase remote config condition by userId

I integrated Firebase into my Android project, to get a different parameter value for different application user. 我将Firebase集成到我的Android项目中,以便为不同的应用程序用户获取不同的参数值。 I did the following: 我做了以下事情:

  1. Setup users in my Firebase Project 在我的Firebase项目中设置用户 在此输入图像描述
  2. Created audiences to match the users: 创建了与用户匹配的受众群体: 在此输入图像描述 UIDs were AAAAAAA... , and BBBBBBB... accordingly. UID是AAAAAAA ......BBBBBBB ......因此。
  3. Created a parameter in Remote Config section: 在“远程配置”部分中创建了一个参数: 在此输入图像描述
  4. Added conditions for this parameter: 添加了此参数的条件: 在此输入图像描述 and set values for the conditions: 并设置条件的值: 在此输入图像描述
  5. Entered the following code to sign in the user from the application: 输入以下代码以从应用程序登录用户:

     Task resultTask = 任务resultTask =   \n    firebaseAuth.signInWithEmailAndPassword("test1@gmail.com", password); firebaseAuth.signInWithEmailAndPassword(“test1@gmail.com”,密码); 
  6. Made sure sign in was successful. 确保登录成功。
  7. Then I tried to fetch the remote config parameter: 然后我尝试获取远程配置参数:
     firebaseRemoteConfig.fetch() firebaseRemoteConfig.fetch() \n                .addOnCompleteListener(new OnCompleteListener() { .addOnCompleteListener(new OnCompleteListener(){\n                    @Override @覆盖\n                    public void onComplete(@NonNull Task task) { public void onComplete(@NonNull Task task){\n                        if (task.isSuccessful()) { if(task.isSuccessful()){\n                            // Once the config is successfully fetched it must be activated before newly fetched //成功获取配置后,必须在新提取之前激活它\n                            // values are returned. //返回值。\n                            firebaseRemoteConfig.activateFetched(); firebaseRemoteConfig.activateFetched();\n                            Log.d(TAG, firebaseRemoteConfig.getString("MyParameter")); Log.d(TAG,firebaseRemoteConfig.getString(“MyParameter”));\n                        } else { } else {\n                            Log.d(TAG, "fetch firebase remote config failed. Reason = " + task.getException()); Log.d(TAG,“获取firebase远程配置失败.Results =”+ task.getException());\n                        } }\n                    } }\n                }); }); 

The result was that I always got the default value: DefaultValue 结果是我总是得到默认值: DefaultValue

What did I do wrong? 我做错了什么? What did I miss? 我错过了什么?

After some investigation I figured that Firebase Analytics and Firebase Authentication are 2 different modules which don't automatically inter-connect. 经过一番调查后,我发现Firebase Analytics和Firebase身份验证是2个不同的模块,不会自动互连。

Using Firebase Authentication did not automatically identify the user as part of the particular audience, as I expected. 正如我所料,使用Firebase身份验证并未自动将用户标识为特定受众的一部分。

I needed to tell Firebase Analytics, that the current user has the specific user ID, so it can match it to the relevant audience. 我需要告诉Firebase Analytics,当前用户具有特定的用户ID,因此可以将其与相关受众进行匹配。 I added the following code to the sign-in onComplete callback: 我将以下代码添加到登录onComplete回调中:

Task<AuthResult> resultTask =   
    firebaseAuth.signInWithEmailAndPassword("test1@gmail.com", password); 
resultTask.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            // Task completed successfully
            if (task.isSuccessful()) {
               firebaseAnalytics.setUserId(task.getResult().getUser().getUid());
            } else {
                Log.d(TAG, "signInWithEmail firebase failed");
            }
        }
    });

The important line is: 重要的是:

firebaseAnalytics.setUserId(task.getResult().getUser().getUid());

Some things to note: 有些事情需要注意:

  1. Once a user is in an audience it can never exit the audience, which means, if you change the user in the app from the same device, you will get a match to 2 audiences: The new audience, and the previous one. 一旦用户在观众中,它就永远不会退出观众,这意味着,如果您从同一设备更改应用中的用户,您将获得与2个观众的匹配:新观众和前一个观众。
  2. While testing this I had to fetch the remote config parameter very frequently. 在测试时,我不得不经常获取远程配置参数。 Although I set the cache expiration to 0, I had a different problem: The Firebase server responded sometime with throttling exception. 虽然我将缓存过期时间设置为0,但我遇到了另一个问题:Firebase服务器在某个时候响应了限制异常。 So, while testing make sure not to throttle the server or you'll have to wait a long time between tests. 因此,虽然测试确保不要限制服务器,否则你将不得不在测试之间等待很长时间。
  3. I'm not sure, but it seams that Firebase Analytics keeps track of the app user with a different id then the one defined with Firebase Authentication. 我不确定,但是Firebase Analytics使用与使用Firebase身份验证定义的ID不同的ID来跟踪应用用户的情况。 I think, this is why if you sign-in with different user id in Firebase Authentication from the same device, you still match to the audience that was setup for the previuos Firebase user ID. 我想,这就是为什么如果您从同一设备使用Firebase身份验证中的不同用户ID登录,您仍然可以匹配为之前的Firebase用户ID设置的受众群体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM