Reputation: 907
I integrated Firebase into my Android project, to get a different parameter value for different application user. I did the following:
Entered the following code to sign in the user from the application:
Task resultTask = firebaseAuth.signInWithEmailAndPassword("[email protected]", password);
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?
Upvotes: 14
Views: 9776
Reputation: 907
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:
Upvotes: 15