Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase OTP验证未发送短信_Android_Firebase_Authentication_Firebase Authentication_One Time Password - Fatal编程技术网

Android Firebase OTP验证未发送短信

Android Firebase OTP验证未发送短信,android,firebase,authentication,firebase-authentication,one-time-password,Android,Firebase,Authentication,Firebase Authentication,One Time Password,我正在尝试在我的android应用程序中使用firebase身份验证和Otp,但此代码不起作用。 它没有发送短信,我在评论后提到的toast部分也没有执行。 包com.example.esport public class otpverification extends AppCompatActivity { private Button verify; private EditText code; private String number,verification_code;

我正在尝试在我的android应用程序中使用firebase身份验证和Otp,但此代码不起作用。 它没有发送短信,我在评论后提到的toast部分也没有执行。 包com.example.esport

public class otpverification extends AppCompatActivity {
  private Button verify;
  private EditText code;
  private String number,verification_code;
  private FirebaseAuth mAuth;
  private PhoneAuthProvider.OnVerificationStateChangedCallbacks mcallBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otpverification);
        verify =findViewById(R.id.otpverifiaction_verify);
        code=findViewById(R.id.otpverification_otp);
        number=getIntent().getStringExtra("ph_number");
        Toast.makeText(getApplicationContext(),number,Toast.LENGTH_LONG).show();
//此toast也未在应用程序上执行 发送短信(号码)

mcallBack=新建PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
@凌驾
已完成验证的公共无效(PhoneAuthCredential PhoneAuthCredential){
}
@凌驾
public void onVerificationFailed(FirebaseException e){
}
@凌驾
public void onCodeSent(字符串s,PhoneAuthProvider.ForceResendingToken ForceResendingToken){
超级OnCondent(s,forceResendingToken);
验证码=s;
}
};
}
专用void sendsms(字符串编号){
PhoneAuthProvider.getInstance().verifyPhoneNumber(
数字,600,TimeUnit.SECONDS,任务执行器。主线程,mcallBack
);
}
公共无效验证(视图){
字符串输入_code=code.getText().toString();
if(验证码等于(“”)
{
verifyPhoneNumber(验证码、输入码);
}
}
专用void verifyPhoneNumber(字符串验证码、字符串输入码){
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(验证码、输入码);
使用电话(凭证)登录;
}
使用Phone(PhoneAuthCredential凭据)的公用无效登录{
mAuth.signInWithCredential(credential).addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful())
{
Toast.makeText(getApplicationContext(),“successful”,Toast.LENGTH_SHORT.show();
}
}
});
}
}
请发送错误信息给我 检查或填充从studio到firebase的SHA1密钥

电话号码活动

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)
}}
打开类OtpScreen:AppCompatActivity(),OnCompleteListener{
val请求\u ID\u多个\u权限=1
私有变量mvericationId:字符串?=null
私有变量mAuth:FirebaseAuth?=null
lateinit var mobile:字符串
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u otp\u屏幕)
mAuth=FirebaseAuth.getInstance()
mobile=intent.getStringExtra(“数字”)
sendVerificationCode(移动)
如果(检查并请求权限()){
//在授予权限的情况下,执行正常流程。
}
signInBt.setOnClickListener{
变量代码:String=otpEt.text.toString()
verifyVerificationCode(代码)
}
}
私人趣味sendVerificationCode(手机:字符串){
PhoneAuthProvider.getInstance().verifyPhoneNumber(
“+91美元移动电话”,
60,TimeUnit.SECONDS,TaskExecutors.MAIN_线程,mCallbacks
)
}
var mCallbacks=对象:PhoneAuthProvider.OnVerificationStateChangedCallbacks(){
覆盖验证完成(phoneAuthCredential:phoneAuthCredential){
var代码=phoneAuthCredential.smsCode
otpEt.setText(代码)
如果(代码!=null){
verifyVerificationCode(代码)
}
}
覆盖验证失败(p0:FirebaseException){
Toast.makeText(applicationContext,p0.message.toString(),Toast.LENGTH\u SHORT.show())
}
重写fun OnCodent(p0:String,p1:PhoneAuthProvider.ForceResendingToken){
恶性肿瘤(p0,p1)
mVerificationId=p0
}
}
private fun verifyVerificationCode(代码:String){//创建凭证
val credential=PhoneAuthProvider.getCredential(mvericationId!!,代码)
//为用户签名
使用PhoneAuthCredential登录(凭证)
}
使用PhoneAuthCredential的私人乐趣登录(凭据:PhoneAuthCredential){
mAuth!!.signInWithCredential(凭证)
.addOnCompleteListener(此,OnCompleteListener{task->
如果(任务成功){
var intent=intent(这是ProfileActivity::class.java)
星触觉(意图)
}
否则{
Toast.makeText(this,task.exception.toString(),Toast.LENGTH\u SHORT.show())
}
})
}
覆盖未完成的乐趣(p0:任务){
}
private val receiver:BroadcastReceiver=对象:BroadcastReceiver(){
覆盖接收(上下文:上下文?,意图:意图){
if(intent.action.equals(“otp”,ignoreCase=true)){
val message=intent.getStringExtra(“消息”)
otpEt.setText(消息)
}
}
}
打开趣味检查和请求权限():布尔值{
val permissionSendMessage=ContextCompat.checkSelfPermission(
这
Manifest.permission.SEND_短信
)
val接收=
ContextCompat.checkSelfPermission(这个,Manifest.permission.RECEIVE_SMS)
val readSMS=ContextCompat.checkSelfPermission(这是Manifest.permission.READ\u SMS)
val ListPermissionsEnded:MutableList=ArrayList()
if(receiveSMS!=已授予PackageManager.权限){
ListPermissionsNeed.add(Manifest.permission.RECEIVE\u MMS)
}
如果(readSMS!=已授予PackageManager.PERMISSION){
ListPermissionsNeed.add(Manifest.permission.READ\u SMS)
}
if(permissionSendMessage!=PackageManager.PERMISSION\u已授予){
ListPermissionsNeed.add(Manifest.permission.SEND\u SMS)
}
如果(!ListPermissionsNeed.isEmpty()){
ActivityCompat.requestPermissions(
这
ListPermissionsNeed.toTypedArray(),
请求\u ID\u多个\u权限
)
返回错误
}
返回真值
}
重写onResume(){
LocalBroadcastManager.getInstance(this.registerReceiver(接收方,意图过滤器(“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)
}}
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)
        }
    }
}}