Vishal Randive
Vishal Randive

Reputation: 21

Facebook account kit - auto insert OTP code on OTP screen UI, android

I am using Facebook AccountKit for OTP verification and it is working absolutely fine.

Requirement: To set OTP code on OTP verification screen automatically by reading incoming SMS.

Looking for: Is there any way that I can set text to OTP verification screen programmatically?

I didn't find any answer while searching for same whether Facebook gives customization on OTP verification screen as they give same on first screen where user can put country code and mobile number.

Upvotes: 2

Views: 3037

Answers (4)

activesince93
activesince93

Reputation: 1736

Facebook provides two boolean for auto-reading OTP message.

AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE
                        , AccountKitActivity.ResponseType.TOKEN);

 // Add these code
 configurationBuilder.setReadPhoneStateEnabled(true);
 configurationBuilder.setReceiveSMS(true);

You also need to add these two permissions in AndroidManifest.xml file:

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

For API >= 23: Check if above permissions granted or not.

Update:

As @markus suggested please review the SMS permission updates.

Upvotes: 3

Aman Jain
Aman Jain

Reputation: 1

Add these 2 lines to your Android Manifest.xml file in manifest tag:

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

Define new variables in your MainActivity AppCompatActivity:

private static final String TAG = "MapActivity";
private static final String mREAD_SMS = Manifest.permission.READ_SMS;
private static final String mRECEIVE_SMS = Manifest.permission.RECEIVE_SMS;
private static final int SMS_PERMISSION_REQUEST_CODE = 1234;

Define a new private void method called getSmsPermission(){}

private void getSmsPermission(){
    Log.d(TAG, "getSmsPermission: getting sms permissions");
    String[] permissions = {Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_SMS};

    if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
            mREAD_SMS) == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                mRECEIVE_SMS) == PackageManager.PERMISSION_GRANTED){
            //onActivityResult();
        }else{
            ActivityCompat.requestPermissions(this,
                    permissions,
                    SMS_PERMISSION_REQUEST_CODE);
        }
    }else{
        ActivityCompat.requestPermissions(this,
                permissions,
                SMS_PERMISSION_REQUEST_CODE);
    }
}

call getSmsPermission(); method in method called startLoginSystem(){} or where you configured your account kit.

private void startLoginSystem() {

    Intent intent = new Intent(MainActivity.this,AccountKitActivity.class);
    AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
            new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE,
                    AccountKitActivity.ResponseType.TOKEN);

    intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,configurationBuilder.build());
    startActivityForResult(intent,REQUEST_CODE);

    //Asks for Sms Permission
    getSmsPermission();

    configurationBuilder.setReadPhoneStateEnabled(true);
    configurationBuilder.setReceiveSMS(true);
}

This will look like this: https://drive.google.com/open?id=120sQ-pc4x1X1termHxkd7IKKRhQVI916

Upvotes: 0

Mahmoud Osman
Mahmoud Osman

Reputation: 487

Try to use Runtime Permission.

Just add in dependencies in build.gradle

implementation 'com.github.florent37:runtime-permission:(lastest version)'

Then You need to add these two permissions in AndroidManifest.xml file:

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

then Call this method in yourActivity:

  askPermission(this, Manifest.permission.READ_CONTACTS, Manifest.permission.READ_SMS)
            .ask(new PermissionListener() {

                @Override
                public void onAccepted(RuntimePermission runtimePermission, List<String> accepted) {

                }

                @Override
                public void onDenied(RuntimePermission runtimePermission, List<String> denied, List<String> foreverDenied) {
                    //the list of denied permissions
                    for (String permission : denied) {

                    }
                    //permission denied, but you can ask again, eg:

            /*
            new AlertDialog.Builder(MainActivity.this)
                    .setMessage("Please accept our permissions")
                    .setPositiveButton("yes", (dialog, which) -> { result.askAgain(); })
                    .setNegativeButton("no", (dialog, which) -> { dialog.dismiss(); })
                    .show();
            */

                    //the list of forever denied permissions, user has check 'never ask again'
                    for (String permission : foreverDenied) {

                    }
                    // you need to open setting manually if you really need it
                    //runtimePermission.goToSettings();
                }
            });

Upvotes: 0

Mohammed Fathi
Mohammed Fathi

Reputation: 1475

AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(LoginType.PHONE
                        , AccountKitActivity.ResponseType.TOKEN);

 // Add these two lines of code
 configurationBuilder.setReadPhoneStateEnabled(true);
 configurationBuilder.setReceiveSMS(true);

then in the manifiest.xml you should add

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

then for devices that runs android M or higher you should ask for theses permissions in runtime : You only need to ask for android.permission.RECEIVE_SMS

Upvotes: 0

Related Questions