首页 > 解决方案 > 用户在帐户 firebase 创建帐户 Android 时关闭应用

问题描述

我有一个简单的应用程序,人们可以注册(注册)。之后我做两件事:

1) 我发送验证邮件;

2) 我为这个用户创建了 firebase 数据库信息。

如图所示,我创建了两个节点。

“用户”节点

“users_settings”节点

而且有两个问题:

1)似乎每20个帐户中有1个在创建时出错,并且这个特殊帐户没有创建“users_settings”节点,它只是创建了“users”节点并且只有full_name字段。就像这张照片中的这个。

没有“users_settings”节点

我询问了遇到此问题的两个用户,但没有发现他们做错了什么,我创建了多个帐户,但没有收到此错误。这就是为什么它很难解决这个问题。

2)如果用户在加载时按下注册按钮并关闭应用程序,firebase会创建帐户,发送验证电子邮件,但不会创建任何firebase数据库信息,因为它没有进入这部分代码。有没有办法防止这种情况发生?

这是当人按下注册按钮时的代码,它只需要字段(名称、电子邮件、密码)并在 firebase 中注册。

注册片段.java

    /**
 * When the user press the REGISTER BUTTON.
 */
private void init(){
    Log.i(TAG, "init: Initializing the button click");

    mRegisterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String email = mEmail.getText().toString();
            String fullName = mFullName.getText().toString();
            String password = mPassword.getText().toString();

            if (stringIsNull(email) || stringIsNull(fullName) || stringIsNull(password)){
                Toast.makeText(getActivity(), R.string.fillin_all_fields, Toast.LENGTH_SHORT).show();
            } else {

                firebaseMethods = new FirebaseMethods(getActivity());

                /*
                 * 1) Create an intent to the same Login Screen;
                 * 2) Register email (Send verification email, if OK start Login Screen again and send message to verify the email).
                 */
                Intent intentLogin = new Intent(getActivity(), LoginActivity.class);

                firebaseMethods.registerNewEmail(email, password, fullName, mProgressBar, intentLogin);

            }

        }
    });

}

这是我的registerNewEmail Firebase 方法:

FirebaseMethods.java

/**
 * Register a new user to the Firebase Authentication with his email and password.
 * @param email the user's email for registering
 * @param password the user's password for registering
 */
public void registerNewEmail(String email, String password, final String fullName, final ProgressBar progressBar, final Intent intent){

    progressBar.setVisibility(View.VISIBLE);

    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.i(TAG, "createUserWithEmail:success");
                        final FirebaseUser user = mAuth.getCurrentUser();

                        if (user != null){

                            user.sendEmailVerification()
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()){
                                                Log.i(TAG, "onComplete: Successful send verification email");
                                                Toast.makeText(mContext, mContext.getString(R.string.confirm_email_verification)
                                                        + user.getEmail(), Toast.LENGTH_SHORT).show();

                                                // 1) Add user's info to database;
                                                // 2) Sign out to wait for the email verification;
                                                // 3) Go to login activity.
                                                addNewUser(fullName, "", "", "", "", "", false);

                                                mContext.startActivity(intent);

                                            } else {
                                                Log.i(TAG, "onComplete: Failed to send verification email");
                                                Toast.makeText(mContext, mContext.getString(R.string.failed_send_verification_email),
                                                        Toast.LENGTH_SHORT).show();
                                            }
                                        }
                                    });

                        }

                        /*updateUI(user);*/
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.i(TAG, "createUserWithEmail:failure", task.getException());
                        if (task.getException() != null) {
                            Toast.makeText(mContext, "Authentication failed." + task.getException().getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }
                        /*updateUI(null);*/
                    }

                    progressBar.setVisibility(View.GONE);
                }
            });

}

这是我的addNewUser方法:

FirebaseMethods.java

    private void addNewUser(String fullName, String birthDate, String beltColor, String registrationNumber, String dojo, String profileImgURL, boolean verified) {

    FirebaseUser currentUser = mAuth.getCurrentUser();

    if (currentUser != null) {

        String userID = currentUser.getUid();

        Log.i(TAG, "addNewUser: Adding new user database information: " + userID);


        // Creating the user model with the information provided.
        User user = new User(
                dojo,
                fullName,
                profileImgURL,
                registrationNumber,
                verified);

        // Adding to the database;
        myRef.child(mContext.getString(R.string.dbname_users))
                .child(userID)
                .setValue(user);

        // Creating the user_settings model with the information provided.
        UserSettings user_settings = new UserSettings(
                beltColor,
                birthDate,
                userID);

        // Adding to the database
        myRef.child(mContext.getString(R.string.dbname_users_settings))
                .child(userID)
                .setValue(user_settings);

        // 1) Added the information for the user that just registered;
        // 2) Sign out to wait for email verification.
        mAuth.signOut();

    }

}

您可以看到我创建了完整的模型 User(dojo、fullName、profileImgURL、registrationNumber、verified)和完整的模型 UserSettings(beltColor、birthDate、userID)。

而且我立即将其添加到 Firebase,因此我看不到它仅添加全名字段而不添加 UserSettings 的方式。

请帮帮我!这是我发布的第一个应用程序。

标签: androidfirebasefirebase-realtime-databasefirebase-authentication

解决方案


推荐阅读