Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Setup users in my Firebase Projectenter image description here
  2. Created audiences to match the users: enter image description here UIDs were AAAAAAA..., and BBBBBBB... accordingly.
  3. Created a parameter in Remote Config section:enter image description here
  4. Added conditions for this parameter: enter image description here and set values for the conditions: enter image description here
  5. Entered the following code to sign in the user from the application:

    Task resultTask =   
        firebaseAuth.signInWithEmailAndPassword("[email protected]", password);
  6. Made sure sign in was successful.
  7. 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?

like image 669
noti Avatar asked Aug 18 '16 09:08

noti


People also ask

How do I get data from Firebase remote config?

Getting started Run the sample on an Android device or emulator. Change one or more parameter values in the Firebase Console (the value of welcome_message , welcome_message_caps , or both). Tap Fetch Remote Config in the app to fetch new parameter values and see the resulting change in the app.

How does Firebase remote configuration work?

What does Firebase Remote Configuration do? Firebase Remote Config is a cloud service that lets you change the behavior and appearance of your app without requiring users to download an app update. When using Remote Config, you create in-app default values that control the behavior and appearance of your app.


1 Answers

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("[email protected]", 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. 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.
  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. 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.
like image 72
noti Avatar answered Sep 27 '22 20:09

noti