Q:

Firebase remote config condition by userId

I integrated Firebase into my Android project, to get a different parameter value for different application user.
I did the following:

Setup users in my Firebase Project
Created audiences to match the users:  UIDs were AAAAAAA..., and BBBBBBB... accordingly.
Created a parameter in Remote Config section:
Added conditions for this parameter:   and set values for the conditions: 
Entered the following code to sign in the user from the application:
Task resultTask =   
    firebaseAuth.signInWithEmailAndPassword("test1@gmail.com", password);
Made sure sign in was successful.
Then I tried to fetch the remote config parameter: firebaseRemoteConfig.fetch() 
                .addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if (task.isSuccessful()) {
                            // Once the config is successfully fetched it must be activated before newly fetched
                            // values are returned.
                            firebaseRemoteConfig.activateFetched();
                            Log.d(TAG, firebaseRemoteConfig.getString("MyParameter"));
                        } else {
                            Log.d(TAG, "fetch firebase remote config failed. Reason = " + task.getException());
                        }
                    }
                });

The result was that I always got the default value: DefaultValue
What did I do wrong? What did I miss?

A:

After some investigation I figured that Firebase Analytics and Firebase Authentication are 2 different modules which don't automatically inter-connect.
Using Firebase Authentication did not automatically identify the user as part of the particular audience, as I expected.
I needed to tell Firebase Analytics, that the current user has the specific user ID, so it can match it to the relevant audience. I added the following code to the sign-in onComplete callback:
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:

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.
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. So, while testing make sure not to throttle the server or you'll have to wait a long time between tests.
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. 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.

