Я хочу сохранить данные аутентифицированных пользователей в Firebase, есть два способа аутентификации данных в моем приложении, после чего пользователь перенаправляется на MainActivity.Java
.Я хотел бы видеть, использует ли пользователь приложение впервые, если это так, добавьте детали в дерево пользователей в Firebase RealtimeDatabase.
На данный момент, после добавления кода в OnCreate под комментарием о добавлении пользователя, приложение не вылетает и не выдает никаких ошибок, но также не попадает в базу данных Firebase и не обновляет ее.
Вот мой код раздела OnCreate MainActivity.Java
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Carousel
customCarouselView = findViewById(R.id.customCarouselView);
customCarouselView.setPageCount(sampleImages.length);
customCarouselView.setSlideInterval(4000);
customCarouselView.setImageListener(imageListener);
customCarouselView.setImageClickListener(new ImageClickListener() {
@Override
public void onClick(int position) {
// Toast.makeText(MainActivity.this, "Clicked item: " + position, Toast.LENGTH_SHORT).show();
redirectToHotDeals();
}
});
// -- Carousel
FirebaseAuth.getInstance().getCurrentUser();
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
// User is signed in.
String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = headerView.findViewById(R.id.emailText);
TextView navUID = headerView.findViewById(R.id.uid);
navUID.setText("ID: " + uid.substring(0, 10));
navUsername.setText(email);
} else {
// No user is signed in.
redirectToLogin();
}
// Action Bar
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Coupons List");
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// Set Layout
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// Send Query to Firebase Db
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Data");
// Add User to DB or Update it
String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
User user = new User(name, email);
mRef.child("User").child(fUser.getUid()).setValue(user);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
Вот изображение базы данных Firebase: я хотел бы, чтобы у первого ребенка был UID, а затемадрес электронной почты и контакт, как его ребенок.
Вот мой User.Java
:
public class User {
public String name, email, phone;
public User(String name, String email){
}
public User(String name, String email, String phone) {
this.name = name;
this.email = email;
this.phone = phone;
}
}