Firebase OTP未在API19中发送。它只适用于更高版本?

时间:2018-03-01 10:27:45

标签: android firebase firebase-authentication

实际上,我正在使用firebase手机身份验证服务。当我在棉花糖牛轧糖中运行应用程序时,棒棒糖它的工作正常。但是当我在KitKat中使用时,OTP没有发送。

mCallback = new PhoneAuthProvider.OnVerificationStateChangedCallbacks(){             @覆盖             public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential)             {

        }

        @Override
        public void onVerificationFailed(FirebaseException e)
        {

        }

        @Override
        public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            //super.onCodeSent(s, forceResendingToken);
            verification_code = s;
            Toast.makeText(getApplicationContext(),"Code sent to the number",Toast.LENGTH_SHORT).show();
            PhoneAuthProvider.ForceResendingToken mResendToken = forceResendingToken;
        }
    };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void send_sms(View view) {
    number = e1.getText().toString().trim();
    uname=e3.getText().toString();
     //new GetNumber().execute();

    if(TextUtils.isEmpty(number) && TextUtils.isEmpty(uname)) {
        e3.setError("please enter username");
        e1.setError("enter 10 digit mobile number");

    }
    else if(TextUtils.isEmpty(number))

    {
        e1.setError("enter 10 digit mobile number");
    }
    else if(TextUtils.isEmpty(uname))
    {
        e3.setError("please enter username");
    }
    else {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_PHONE_STATE}, 0);

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                number, 60, TimeUnit.SECONDS, this, mCallback


        );
        Toast.makeText(getApplicationContext(), "sending", Toast.LENGTH_SHORT).show();
    }
}
public void signInWithPhone(PhoneAuthCredential credential)
{
   Toast.makeText(getApplicationContext(),"checking verification code",Toast.LENGTH_SHORT).show();
   auth.signInWithCredential(credential)
           .addOnCompleteListener(new OnCompleteListener() {
               @Override
               public void onComplete(Task task) {
                   if(task.isSuccessful())
                   {
                       txt=e1.getText().toString();
                      new setUser().execute();

                       /*Toast.makeText(getApplicationContext(),"User signed in successfully",Toast.LENGTH_SHORT).show();
                       getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                               .putBoolean("isFirstRun", false).commit();

                       Intent homeIntent = new Intent(Register.this, MainActivity.class);
                       homeIntent.putExtra("user_id",str1);
                       Toast.makeText(getApplicationContext(),"Register :"+str1,Toast.LENGTH_SHORT).show();
                       startActivity(homeIntent);
                       finish();*/
                   }
                   else if(task.getException() instanceof FirebaseAuthUserCollisionException)
                   {
                       Toast.makeText(getApplicationContext(),"User already exist",Toast.LENGTH_SHORT).show();
                   }
                   else
                   {
                       Toast.makeText(getApplicationContext(),"Wrong code. please try again",Toast.LENGTH_SHORT).show();
                   }
               }
           });
}

public void Verify(View view) {
    String input_code = e2.getText().toString();
    if(TextUtils.isEmpty(input_code)) {
        e2.setError("please enter verification code");
        return;
    }
    else{
    Toast.makeText(getApplicationContext(),input_code,Toast.LENGTH_SHORT).show();

        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verification_code,input_code);
        signInWithPhone(credential);}
}

private class setUser extends AsyncTask<String,String,String>
{
    @Override
    protected String doInBackground(String... strings) {
        isr1 = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost("http://bringupwater.com/Android/R_Model_R/setUser.php");//YOUR PHP SCRIPT ADDRESS
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("name", uname));
            nameValuePairs.add(new BasicNameValuePair("number", number));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr1 = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());

        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr1, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr1.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error  converting result " + e.toString());
        }

    return result;
    }
   @Override
    protected void onPostExecute(String result)
    {
        str=result;
        str1 = str.replaceAll("[^\\d.]", "");
        Toast.makeText(getApplicationContext(),"User signed in successfully",Toast.LENGTH_SHORT).show();
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit()
                .putBoolean("isFirstRun", false).commit();

        Intent homeIntent = new Intent(Register.this, MainActivity.class);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        editor=pref.edit();

        editor.putString("user_id", str1);
        editor.commit();

        String st=pref.getString("user_id","");
        //homeIntent.putExtra("user_id",str1);
        Toast.makeText(getApplicationContext(),"Register :"+st,Toast.LENGTH_SHORT).show();
        startActivity(homeIntent);
        finish();
    }
}

public void onBackPressed() {
    if(!isUserBackButtonPressed){
        Toast.makeText(this,"Press Back Again to Exit", Toast.LENGTH_LONG).show();
        isUserBackButtonPressed = true;
    }
    else{
        if (Build.VERSION.SDK_INT >= 19) {
            finishAffinity();
            System.exit(0);
        }
        else
        {
            ActivityCompat.finishAffinity(this);
        }
    }

    new CountDownTimer(3000,1000){

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            isUserBackButtonPressed = false;
        }
    }.start();

0 个答案:

没有答案