我希望在用户提交电子邮件和密码后,将自动发送验证电子邮件。我做了一个发送验证电子邮件的功能,但是它不起作用,只能保存电子邮件并通过我的真实Firebase控制台传递,但不发送验证电子邮件。我尝试了一些代码,但似乎没有任何效果,这是我最新的代码,有什么问题吗?
class Activity2 : AppCompatActivity() {
private val personCollectionRef = Firebase.firestore.collection("persons")
companion object {
const val REQUEST_ON_GALLERY = 1001
}
private lateinit var auth: FirebaseAuth
private val _tag = "Activity2"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity2)
auth = FirebaseAuth.getInstance()
btnregis.setOnClickListener {
val job = lifecycleScope.launch(Dispatchers.IO) {
//two function
val user = async { sussavePerson() }
val registeremail = async { susregistrasiAkun() }
Log.d(_tag, "Saving Person data ${user.await()}")
Log.d(_tag, "Saving email ${registeremail.await()}")
}
runBlocking {
job.join()
activity()
Log.d(_tag,"Hai")
}
}
}
// here my function for registration and sending verification email
private fun susregistrasiAkun(){
registrasiAkun()
}
private fun registrasiAkun() {
val email = etemail.text.toString()
val password = etpassword.text.toString()
if(email.isNotEmpty() && password.isNotEmpty()){
CoroutineScope(Dispatchers.IO).launch {
try {
auth.createUserWithEmailAndPassword(email,password).await()
withContext(Dispatchers.Main){
checkLoggedInState()
//here i want send email verification function
withContext(Dispatchers.IO){
email()
}
}
}catch (e:Exception){
withContext(Dispatchers.Main){
Toast.makeText(this@Activity2,e.message,Toast.LENGTH_LONG).show()
}
}
}
}
}
private fun checkLoggedInState(){
if(auth.currentUser == null){
tvLoggedIn.text = getString(R.string.loggedin)
}else{
tvLoggedIn.text = getString(R.string.loggedin2)
}
}
// function for sending email
private fun email() {
val user = auth.currentUser
user!!.sendEmailVerification()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d(_tag, "Email sent.")
}
}
}