1

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

下面是我的代码,

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

4

1 回答 1

1

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

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

于 2015-10-29T23:25:24.330 回答