[#103][안드로이드] Firebase 실시간 데이터베이스 데이터 저장하기
Firebase 실시간 데이터베이스 데이터 저장하기
Firebase 초기 연동 가이드 (https://firebase.google.com/docs/guides/?hl=ko)1. UserData.java
: 사용자 정보 저장할 클래스 생성
package com.example.ghd_t.myapplication;
import android.graphics.drawable.Drawable;
import android.net.Uri;
/**
* Created by ghd-t on 2018-04-23.
*/
public class UserData {
private String userName;
private String profile;
public UserData(String userName, String profile) {
this.userName = userName;
this.profile = profile;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
}
| cs |
2. LoginActivity.java
: Firebase Auth를 통해 Google 계정으로 로그인 한 사용자 정보를 DB에 저장
private void firebaseAuthWithGoogle(GoogleSignInAccount acct){
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.v("알림", "ONCOMPLETE");
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.v("알림", "!task.isSuccessful()");
Toast.makeText(LoginActivity.this, "인증에 실패하였습니다.", Toast.LENGTH_SHORT).show();
}else {
Log.v("알림", "task.isSuccessful()");
FirebaseUser user = mAuth.getCurrentUser();
String cu = mAuth.getUid();
String name = user.getDisplayName();
String email = user.getEmail();
String photoUrl = user.getPhotoUrl().toString();
String phone = user.getPhoneNumber();
Log.v("알림", "현재로그인한 유저 " + cu);
Log.v("알림", "현재로그인한 이메일 " + email);
Log.v("알림", "유저 이름 " + name);
Log.v("알림", "유저 사진 " + photoUrl);
Log.v("알림", "유저 폰 " + phone);
//이 부분이 DB에 데이터 저장
UserData userdata = new UserData(name, photoUrl);
mDatabase.child("users").child(cu).setValue(userdata);
//이 부분이 DB에 데이터 저장
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "FireBase 아이디 생성이 완료 되었습니다", Toast.LENGTH_SHORT).show();
}
}
});
}
| cs |
3. Firebase에서 확인
댓글
댓글 쓰기