Вопрос

We've had to code an OTP based authentication. I have seen some apps, like my bank's app, which when it sends the OTP also then immediately does a quick popup of the SMS that has just arrived, so I can see the OTP without leaving the app. I just memorize the number, close the popup, and get on with the login inside that app.

How do they do that? Is there some iOS/Android spec I should be looking at, which allows us to similarly popup the OTP without the user having to go to the SMS screen, then come back to our app? Thanks!

EDIT: I have very useful Android suggestions. Now looking for iOS variations of these recommendations. Understand iOS has much more stringent sandboxing limitations, so the "listener" may be more complex?

Это было полезно?

Решение

Here is step by step description to achieve your requirement

  1. Declare receiver in AndroidManifest

    <receiver android:name=".IncomingSms">   
     <intent-filter>
          <action android:name="android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
    </receiver>
    

    2 Give read SMS permission in AndroidManifest

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

Complete code for AndroidManifest.xml File :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidexample.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidexample.broadcastreceiver.BroadcastNewSms"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.androidexample.broadcastreceiver.IncomingSms">   
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

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


</manifest>

Complete code for IncomingSms.java file :

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


                   // Show Alert
                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, 
                                 "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();

                } // end for loop
              } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }
    }    
}

register above broadcast receiver in your activity and you will get your output

Другие советы

For android you need to use SMSListener as pointed out by @rushabh. You can check at a great example here

Some Tips to achieve your mention task for your App.
Step - 1 create a Login Activity with necessary field like username , password and otp and Login Button.

Step - 2 When user fill the username and password make a web service call. with input params (username and password) authenticate the values if true means send your OTP number as response else response error message.

Step -3 if response is number means create AlertBuilder for Pop window to show your OTP number in same Activity.

Step - 4 user saw the OTP in Login Activity itself and enters the OTP in opt area i.e (EditText).

Step - 5 When user tap the login Button authenticate the OTP value. and proceed to next Activity.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top