Coder Social home page Coder Social logo

android-flash-calling's Introduction

Verify Phone Numbers – Flash Call Verification

This tutorial will show you how to verify your users with no interaction required from the user in two minutes. This uses the Sinch Flash Call technology for Android, and lets you ensure that a user is in possession of a phone number by relying on the regular phone network.

For more Verification Tutorials

By the end of this tutorial, your app will look similar to this:

The following workflow summarizes how the app is operating.

##Setup

If you haven’t done so yet, set up a developer account with Sinch. You will need to create an app in the developer portal. Be sure to enable Verification as follows:

Hold on to the app key generated by this service; you will need it in a few minutes.

Note: In order to use the verification SDK, you should have some credits in your account. But don't worry, Sinch offers $2 in free credits when you verify your phone number.

Go to your Dashboard in the account tab and verify your phone number. You will receive a prompt SMS with your code verification.

##Flash Call Verification

Now, let's start coding. Create a new project in your IDE and choose VerificationActivity as a name of your LauncherActivity.

Download the Sinch Verification SDK at Verification SDK and add it to your project.

Sinch Flash Call Verification SDK requires a few permissions. Add the following to your AndroidManifest file:

	<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_CALL_LOG" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

In your activity_verification layout file, add the following Views:

	 <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:ems="10"
        android:id="@+id/phoneNumber"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Verify"
        android:id="@+id/verify"
        android:layout_marginTop="63dp"
        android:textColor="#ffffffff"
        android:layout_below="@+id/phoneNumber"
        android:layout_centerHorizontal="true" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" />

In onCreate method, we need to start the Flash Call Verification process when the user enters his or her phone number and clicks on the verify button.

```objective-c
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

numberPhone = (EditText) findViewById(R.id.phoneNumber);
verify = (Button) findViewById(R.id.verify);
progressBar = (ProgressBar) findViewById(R.id.progressBar);

verify.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String number = numberPhone.getText().toString();
        if(number.isEmpty()) {
            Toast.makeText(getApplicationContext(), "Phone number cannot be empty!",Toast.LENGTH_LONG).show();
       }
        else {
            showProgressDialog();
            startVerification(number);
        }
    }
});


<p>Now, add the <b>startVerification</b> method:</p>
```objective-c
	private void startVerification(String phoneNumber) {
        Config config = SinchVerification.config().applicationKey("your_app_key").context(getApplicationContext()).build();
        VerificationListener listener = new MyVerificationListener();
        verification = SinchVerification.createFlashCallVerification(config, phoneNumber, listener);
        verification.initiate();
    }

Create MyVerificationListener class, which implements VerificationListener interface. The VerificationListener provides callbacks during the verification process.

The following workflow describes the verification process:

	private class MyVerificationListener implements VerificationListener {
        @Override
        public void onInitiated() {}

        @Override
        public void onInitiationFailed(Exception e) {
            hideProgressDialog();
            if (e instanceof InvalidInputException) {
            	Toast.makeText(MainActivity.this,"Incorrect number provided",Toast.LENGTH_LONG).show();
            } else if (e instanceof ServiceErrorException) {
            	Toast.makeText(MainActivity.this,"Sinch service error",Toast.LENGTH_LONG).show();
            } else {
            	Toast.makeText(MainActivity.this,"Other system error, check your network state", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onVerified() {
            hideProgressDialog();
            new AlertDialog.Builder(VerificationActivity.this)
                           .setMessage("Verification Successful!")
                           .setPositiveButton("Done", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.cancel();
                                }
                            })
                            .show();
        }

        @Override
        public void onVerificationFailed(Exception e) {
            hideProgressDialog();
            if (e instanceof CodeInterceptionException) {
                Toast.makeText(VerificationActivity.this,"Intercepting the verification call automatically failed",Toast.LENGTH_LONG).show();
            } else if (e instanceof ServiceErrorException) {
                Toast.makeText(VerificationActivity.this, "Sinch service error",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(VerificationActivity.this,"Other system error, check your network state", Toast.LENGTH_LONG).show();
            }
        }
    }
}

Finally, add methods to show and hide the progressBar:

```objective-c
private void showProgressDialog() {
    progressBar.setVisibility(ProgressBar.VISIBLE);
}

private void hideProgressDialog() {
    progressBar.setVisibility(ProgressBar.INVISIBLE);
}
<p>Sinch Flash Call Verification has proven itself to be the fastest method to verify user phone number. Happy Verification!</p>

android-flash-calling's People

Contributors

carenvaleria avatar kwaimind avatar mahaben avatar mgpopolo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.