How do I retrieve user email from Firebase Firestore and send it to an email intent when a button is clicked?

Neha :

I'm trying to fetch user email using FirebaseAuth and open an email intent to show the email fetched automatically in the email.

I have tried to do the intent however I need help to get the email from Firebase and link it to email intent:

        mFirebaseAuth = FirebaseAuth.getInstance();
        emailButton = findViewById(R.id.openEmailButton);

        emailButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userInformation();

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
                intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
                intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

                startActivity(Intent.createChooser(intent, "Send Email"));
            }
        });


    public void userInformation(){
        FirebaseUser user = mFirebaseAuth.getCurrentUser();

        if (user != null) {
           email = user.getEmail();
        }
    }

Instead of [email protected] I want the email address fetched from Firebase.

Alex Mamo :

I need help to get the email from firebase

If you need add the email from the Firebase authentication to the intent, please note that you're almost there. Just add the email that you got from the FirebaseUser object in userInformation() method to your intent object. So please change the following line of code:

intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");

to

intent.putExtra(Intent.EXTRA_EMAIL, email);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161708&siteId=1