Read sms automatically to verify OTP

Reading incoming message automatically in android to verify OTP

Here I am going to explain how to read the incoming message in android automatically to verify OTP without 
closing the app.

Step 1. ReadSms.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

public class ReadSms extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent 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();
    
                       try
                       { 
     
                             if (senderNum.equals("TA-DOCOMO")) 
                             {
                                    
                                  Otp Sms = new Otp();
                                  Sms.recivedSms(message );
                             }
                       } catch(Exception e){}
                 }
            }

        } catch (Exception e) {}     
    }

}

here, "pdus" is a "protocol data unit", it is an industry standard for SMS.
mobile SMS applications always use PDUs to encode the contents of a SMS message...

Step 2. Otp.class

public class Otp extends Activity 
{

       static EditText OtpNumber;
       protected void onCreate(Bundle savedInstanceState) 
       {
       
              super.onCreate(savedInstanceState);
              setContentView(R.layout.otp);
              OtpNumber = (EditText) findViewById(R.id.txtName);
       }
      
       public void recivedSms(String message) 
       {
            try 
            {
                   OtpNumber.setText(message);
            } 
            catch (Exception e) {}     
             
       }
}   

Step 3. Add the below permissions and receiver to your manifest file

We need to give below run-time permissions for marshmallow OS :
1. RECEIVE_SMS
2. READ_SMS
3. SEND_SMS

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

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

Comments

  1. Hello Maitri it is a great code i have tried it out and it worked will I am presently using www.textlocal.in to send OTP's will suggest my customers your code.

    ReplyDelete
    Replies
    1. Hi Ananya,

      Thank you for the words.
      If you need any of my help for this code, I am here to help you any time.

      For any other samples if you want me to create, then you can contact me, I will make for you.

      Thanks once again.

      Delete
  2. Hi ,
    Nice tutorial , i have used the same code as above but its not working while i am generation otp please help.thanks in advance.

    ReplyDelete
  3. hello,

    This code is not working for me. I am sending sms from way2sms then try to fetch otp from it but it not working.

    Please send code on this email id "sonipari002@gmail.com".

    THanks in advance.

    ReplyDelete
  4. this code not worked for me ... any solution??

    ReplyDelete
  5. this code is not worked for me.

    ReplyDelete
  6. This code did not worked for me as well

    ReplyDelete
  7. Thanks for sharing this information.
    If you looking for OTP sms service provider, Visit our website www.nrtsms.com . We also provide six month free sms service for startup business...

    ReplyDelete

Post a Comment

Popular posts from this blog

FCM - Firebase cloud messanfing for Notification

Create shapes online - Android Shapes Generator