Пожалуйста, отправьте мне сообщение об ошибке Проверьте или заполните ключ SHA1 от студии до firebase
Номер телефона Активность
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
numberBt.setOnClickListener {
var mnumber : String = numberEt.text.toString()
var intent = Intent(this,OtpScreen::class.java)
intent.putExtra("number",mnumber)
startActivity(intent)
}
}}
Активность Otp
open class OtpScreen : AppCompatActivity(), OnCompleteListener<AuthResult> {
val REQUEST_ID_MULTIPLE_PERMISSIONS = 1
private var mVerificationId: String? = null
private var mAuth: FirebaseAuth? = null
lateinit var mobile: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_otp_screen)
mAuth = FirebaseAuth.getInstance()
mobile = intent.getStringExtra("number")
sendVerificationCode(mobile)
if (checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
}
signInBt.setOnClickListener {
var code: String = otpEt.text.toString()
verifyVerificationCode(code)
}
}
private fun sendVerificationCode(mobile: String) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+91$mobile",
60, TimeUnit.SECONDS, TaskExecutors.MAIN_THREAD, mCallbacks
)
}
var mCallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
override fun onVerificationCompleted(phoneAuthCredential: PhoneAuthCredential) {
var code = phoneAuthCredential.smsCode
otpEt.setText(code)
if (code != null) {
verifyVerificationCode(code)
}
}
override fun onVerificationFailed(p0: FirebaseException) {
Toast.makeText(applicationContext,p0.message.toString(),Toast.LENGTH_SHORT).show()
}
override fun onCodeSent(p0: String, p1: PhoneAuthProvider.ForceResendingToken) {
super.onCodeSent(p0, p1)
mVerificationId = p0
}
}
private fun verifyVerificationCode(code: String) { //creating the credential
val credential = PhoneAuthProvider.getCredential(mVerificationId!!, code)
//signing the user
signInWithPhoneAuthCredential(credential)
}
private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
mAuth!!.signInWithCredential(credential)
.addOnCompleteListener(this, OnCompleteListener { task ->
if (task.isSuccessful){
var intent = Intent(this,ProfileActivity::class.java)
startActivity(intent)
}
else{
Toast.makeText(this,task.exception.toString(),Toast.LENGTH_SHORT).show()
}
})
}
override fun onComplete(p0: Task<AuthResult>) {
}
private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
if (intent.action.equals("otp", ignoreCase = true)) {
val message = intent.getStringExtra("message")
otpEt.setText(message)
}
}
}
open fun checkAndRequestPermissions(): Boolean {
val permissionSendMessage = ContextCompat.checkSelfPermission(
this,
Manifest.permission.SEND_SMS
)
val receiveSMS =
ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
val readSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
val listPermissionsNeeded: MutableList<String> = ArrayList()
if (receiveSMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECEIVE_MMS)
}
if (readSMS != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_SMS)
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS)
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(
this,
listPermissionsNeeded.toTypedArray(),
REQUEST_ID_MULTIPLE_PERMISSIONS
)
return false
}
return true
}
override fun onResume() {
LocalBroadcastManager.getInstance(this).registerReceiver(receiver, IntentFilter("otp"))
super.onResume()
}
override fun onPause() {
super.onPause()
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver)
}}
Автоматическое заполнение класса OPP
class MySMSBroadCastReceiver : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) { // Get Bundle object contained in the SMS intent passed in
val bundle = intent.extras
var smsm: Array<SmsMessage?>? = null
var sms_str = ""
if (bundle != null) { // Get the SMS message
val pdus = bundle["pdus"] as Array<Any>?
smsm = arrayOfNulls(pdus!!.size)
for (i in smsm.indices) {
smsm[i] =
SmsMessage.createFromPdu(pdus[i] as ByteArray)
sms_str += smsm[i]?.getMessageBody().toString()
val Sender = smsm[i]?.getOriginatingAddress()
//Check here sender is yours
val smsIntent = Intent("otp")
smsIntent.putExtra("message", sms_str)
LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent)
}
}
}}