在Android中使用Sinch SMS Verification验证收到的OTP时,错误的回调被触发

时间:2015-10-29 10:35:40

标签: android verification otp sinch sms-verification

我正在我的应用程序中集成Sinch SMS Verification API,其中一半已经完成。到目前为止,我能够为给定的手机号码发送和生成OTP请求/响应,并在该手机号码上接收。但是,当我尝试验证接收到OTP时,错误的Callback被触发了。

以下是我的代码,

public class MainActivity extends AppCompatActivity implements OnClickListener, VerificationListener {

    private EditText editTextVerify;
    private Button buttonVerify;

    private Config config;
    private Verification verification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextVerify = (EditText) findViewById(R.id.editTextVerify);
        buttonVerify = (Button) findViewById(R.id.buttonVerify);
        buttonVerify.setOnClickListener(this);

        config = SinchVerification.config().applicationKey("5xxxxx9c-xxc-4***-a***-76xxxxxb1xx1").context(getApplicationContext()).build();
        verification = SinchVerification.createSmsVerification(config, "91xx**xx**", this);
        verification.initiate();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        System.out.println("...button clicked...");
        verification.verify(editTextVerify.getText().toString().trim());
    }

    @Override
    public void onInitiated() {
        // TODO Auto-generated method stub
        System.out.println("...calls onInitiated...");
    }

    @Override
    public void onInitiationFailed(Exception e) {
        // TODO Auto-generated method stub
        if (e instanceof InvalidInputException) {
            // Incorrect number provided
            System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
        } else if (e instanceof ServiceErrorException) {
            // Sinch service error
            System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
        } else {
            // Other system error, such as UnknownHostException in case of network error
            System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
        }
    }

    @Override
    public void onVerified() {
        // TODO Auto-generated method stub
        System.out.println("...callls onVerified this....");
    }

    @Override
    public void onVerificationFailed(Exception e) {
        // TODO Auto-generated method stub
        if (e instanceof InvalidInputException) {
            // Incorrect number or code provided
            System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
        } else if (e instanceof CodeInterceptionException) {
            // Intercepting the verification code automatically failed, input the code manually with verify()
            System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
        } else if (e instanceof IncorrectCodeException) {
            // The verification code provided was incorrect
            System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
        } else if (e instanceof ServiceErrorException) {
            // Sinch service error
            System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
        } else {
            // Other system error, such as UnknownHostException in case of network error
            System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
        }
    }

}

每当我在文本框中输入收到的OTP或错误的OTP时单击下面的“提交”按钮,将触发此回调

@Override
public void onInitiationFailed(Exception e) {
    // TODO Auto-generated method stub
    if (e instanceof InvalidInputException) {
        // Incorrect number provided
        System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
    } else if (e instanceof ServiceErrorException) {
        // Sinch service error
        System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
    } else {
        // Other system error, such as UnknownHostException in case of network error
        System.out.println("....onInitiationFailed exception... " + e.getLocalizedMessage());
    }
}

理想情况下,它应该在Callback

之下触发
@Override
public void onVerificationFailed(Exception e) {
    // TODO Auto-generated method stub
    if (e instanceof InvalidInputException) {
        // Incorrect number or code provided
        System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
    } else if (e instanceof CodeInterceptionException) {
        // Intercepting the verification code automatically failed, input the code manually with verify()
        System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
    } else if (e instanceof IncorrectCodeException) {
        // The verification code provided was incorrect
        System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
    } else if (e instanceof ServiceErrorException) {
        // Sinch service error
        System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
    } else {
        // Other system error, such as UnknownHostException in case of network error
        System.out.println("....onVerificationFailed exception... " + e.getLocalizedMessage());
    }
}

推荐链接: https://www.sinch.com/docs/verification/android/#verificationlistener

1 个答案:

答案 0 :(得分:1)

我会尝试在 onInitiationFailed 方法之后立即放置 onVerified 回调。这应该允许OTP响应通过 onVerificationFailed 回调。

以下是验证监听器的文档: http://download.sinch.com/docs/verification/android/latest/reference/index.html