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?
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With