如何使用android studio为firebase中的所有新用户设置默认值

时间:2018-02-01 07:52:58

标签: android firebase firebase-realtime-database firebase-authentication google-signin

如何为使用Google登录首次注册的所有新用户设置默认整数(0)值,但对于旧用户(之前注册过的用户),该值不应在清除应用数据时更改应用

例如,我是第一次注册得分为30分的新用户,在应用程序中,我发挥了一些任务以获得更多,现在我得到了60分,但是当我再次删除应用数据时登录时我得到30分

请参阅代码

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    //getting the auth credential
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //Now using firebase we are signing in the user here
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    progressDialog.dismiss();
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
                        DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());
                        if (user.getUid() != null){
                            UserId.getDatabase();
                            Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                            Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);
                        }else {
                            UserId.child("Score").setValue(30);
                            Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                            Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);
                        }


                    } else {
                        // If the sign in fails displays a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }


                }
            });
}

当我替换此代码时

if (user.getUid() != null){
                        UserId.getDatabase();
                        Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                        Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                        main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(main2);
                    }else {
                        UserId.child("Score").setValue(30);
                        Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
                        Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                        main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(main2);
                    }

         UserId.child("Score").setValue(30);

         Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
         Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
                            main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(main2);

我的firebase数据库中创建了带分数的新uid,但问题是当我清除应用数据并再次登录分数变为30时,但如果我不替换代码,则新用户不会在数据库中创建,但它们是通过评分无效验证

那么请告诉我如何才能完成这项工作?

我正在使用他们的uid进行追踪 谢谢,任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

现在我通过创建两个函数来检查用户是否为new,然后使用uid通过valueaddedeventlistener同时添加

以下是登录的代码

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    //getting the auth credential
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);

    //Now using firebase we are signing in the user here
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    progressDialog.dismiss();
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithCredential:success");
                        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
                        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                        final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());
                        final String uid = currentFirebaseUser.getUid();
                        databaseReference.child("Users").addListenerForSingleValueEvent(
                                new ValueEventListener() {

                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {

                                        //String uid1 = dataSnapshot.hasChild(uid);

                                        if (dataSnapshot.hasChild(uid)) {
                                            //Old User
                                            userAlreadyExistsScore();

                                        } else {
                                            // User Not Yet Exists
                                            newUserScore();
                                        }
                                    }
                                    @Override
                                    public void onCancelled (DatabaseError databaseError){

                                    }
                                }

                        );

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();

                    }

                    // ...
                }
            });
}

以下是新用户的代码

private void newUserScore(){
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());

    UserId.child("Score").setValue(30);
    Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
    Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
    main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(main2);

}

以下是用户已存在的代码

private void userAlreadyExistsScore(){

    FirebaseUser user = mAuth.getCurrentUser();
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    final DatabaseReference UserId = databaseReference.child(mAuth.getCurrentUser().getUid());


    if (user != null){

        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Integer Score = dataSnapshot.getValue(Integer.class);
                UserId.child("Score").setValue(Score);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        UserId.child("Score").addListenerForSingleValueEvent(valueEventListener);
        Toast.makeText(MainActivity.this, "Sign in Successful", Toast.LENGTH_SHORT).show();
        Intent main2 = new Intent(MainActivity.this,Main2Activity.class);
        main2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(main2);

    }
}