Skip to main content

Sign in with Phone Number using Digits

Today we are going to implement "Sign in with Phone Number" using Digits.
I'm using Android Studio for development. So first we need to download and install Fabric plugin.

Go to https://fabric.io and create account. After that you need to download Fabric plugin.



Once you download and install plugin, you will see Fabric plugin icon in Android Studio.


1. Now create a sample project with one activity.
2. Click on that Fabric plugin button and you will see login page.
3. Enter username and password and login to Fabric.It will detect if any of it's kit installed in project or not. 
4. Then click on install button of Digits Kit. Apply the changes in your project.

After that it will add some data to your project as well as in AndroidManifest.
That's it you are successfully integrate Digits kit in your Android project.


Here is the activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    android:gravity="center"    tools:context=".MainActivity">

    <com.digits.sdk.android.DigitsAuthButton        android:id="@+id/auth_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Click"/>

</LinearLayout>


MainActivity.java

public class MainActivity extends AppCompatActivity {

    DigitsAuthButton click;
    private AuthCallback authCallback;
    private static final String TAG = "MainActivity";
    // Note: Your consumer key and secret should be obfuscated in your source code before shipping.    private static final String TWITTER_KEY = "Your Twitter Key";
    private static final String TWITTER_SECRET = "Your Twitter Secret Key";


    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(this, new TwitterCore(authConfig), new Digits());
        setContentView(R.layout.activity_main);

        click = (DigitsAuthButton) findViewById(R.id.auth_button);
        click.setCallback(new AuthCallback() {
            @Override            public void success(DigitsSession digitsSession, String s) {
                Log.d(TAG, "Digit string: "+s.toString());
                Log.d(TAG, "Digit session: "+digitsSession.getAuthToken().toString());
                Toast.makeText(MainActivity.this, "digit string: "+s.toString(),Toast.LENGTH_LONG).show();
            }

            @Override            public void failure(DigitsException e) {
                Log.d(TAG, "Oops Digits issue" +e.getMessage());
                Toast.makeText(MainActivity.this, "digit string: "+e.getMessage(),Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.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);
    }

}

After this you need to add some permissions in AndroidManifest.xml

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

if you want to allow Digits to prefill the phone number then you need to add below permissions.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
And this sample project will run only on real devices.

Screenshots:




Comments

Popular posts from this blog

How to check internet connectivity in Android app using BroadcastReceiver

So let's get started with How to check internet connectivity in Android using BroadcastReceiver. For this post I'm using Kotlin language. Well lots of people are using simple method to get network connectivity using ConnectivityManager. But using that approach we will get network state only when we are calling that method. So how would I know that network state changed. This will achieved using BroadcastReceiver.  What is BroadcastReceiver? BroadcastReceiver uses Publish-Subscribe pattern. Android app can send and receive messages from Android system and other apps.  Android system sends broadcast when various system events occurred like device charging, network state changes. Apps can register to receive specific broadcasts. When a broadcast is sent, the system automatically routes broadcasts to apps that have subscribed to receive that particular type of broadcast. So for this scenario we need to add some changes in AndroidManifest.xml file - ...

RxJava introduction for Android developer

RxJava is Java implementation of Reactive Extension. Basically it’s a library that composes asynchronous events by following Observer Pattern. You can create asynchronous data stream on any thread, transform the data and consumed it by an Observer on any thread. The library offers operators like map, combine, merge, filter and lot more that can be applied onto data stream.  Below are the list of schedulers available and their brief introduction. - Schedulers.io()  – This is used to perform non CPU-intensive operations like making network calls, reading disc / files, database operations etc., This maintains pool of threads. 

 - AndroidSchedulers.mainThread()  – This provides access to android Main Thread / UI Thread. Usually operations like updating UI, user interactions happens on this thread. We shouldn’t perform any intensive operations on this thread as it makes the app glitchy or ANR dialog can be thrown. 

 - Schedulers.newThre...

Get domain name from URL in android

Sample Code: String url="http://sports.in.msn.com/football-world-cup-2014/world-cup-animals-5"; String hostName=getDomainName(url); private CharSequence getDomainName(String shareURL) throws URISyntaxException {    URI uri = new URI(shareURL);    String domain = uri.getHost();     return domain; } above method return host name from given URL. You can get more from URL using below methods. uri.getProtocol(); uri.getAuthority(); uri.getHost(); uri.getPort(); uri.getPath(); uri.getQuery(); uri.getFile(); Output: protocol = http authority = sports.in.msn.com host = sports.in.msn.com port = -1 path = /football-world-cup-2014/world-cup-animals-5 query = null filename = /football-world-cup-2014/world-cup-animals-5