0

I have a Forgot Password page. i want to when the otp message comes it reads the otp number and verify.But in my case OTP message reads but i want only OTP number. Like example My OTP message is :- 9563 is your OTP for Reset Password for your app. Treat this as a confidential. i read this message by the help of this class.but i want to get only OTP number like (9563).not the whole message.Can anyone tell me how can i do this ??

This is the class by which i read the otp message.

    public class IncominMsg extends BroadcastReceiver {

    final SmsManager smsManager = SmsManager.getDefault();
    Context mContext;
    Object[] pdusObj;
    String message;


    @Override
    public void onReceive(Context context, Intent intent) {


        final Bundle bundle = intent.getExtras();


        try {
            if (bundle != null) {
                pdusObj = (Object[]) bundle.get("pdus");
                SmsMessage[] smsMessages = new SmsMessage[pdusObj.length];

                for (int i = 0; i < pdusObj.length; i++) {
                    smsMessages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);

                    message = smsMessages[i].getDisplayMessageBody();
                    Log.e("message..", message);
                }


                Pattern generalOtpPattern = Pattern.compile(message);
                Matcher generalOtpMatcher = generalOtpPattern.matcher(smsMessages[0].getMessageBody().toString());

                if (generalOtpMatcher.find()) {
                    String otp = generalOtpMatcher.group(1);//this is only your OTP code


                }


            }


        } catch (Exception e) {

        }
    }


}

I get the message and print in the logcat.but I want only OTP number not the whole message.

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
LoveAndroid
  • 146
  • 1
  • 10

1 Answers1

0

Try this it may be help to you

public static String GENERAL_OTP_TEMPLATE = "Your verification code: (.*).";

SmsMessage[] message = new SmsMessage[objectArray.length];
for (int i = 0; i < objectArray.length; i++) {
 message[i] = SmsMessage.createFromPdu((byte[]) objectArray[i]);

}
Pattern generalOtpPattern = Pattern.compile(GENERAL_OTP_TEMPLATE);
Matcher generalOtpMatcher = generalOtpPattern.matcher(message[0].getMessageBody().toString());

if (generalOtpMatcher.find()) {
   String otp = generalOtpMatcher.group(1);//this is only your OTP code

}

make GENERAL_OTP_TEMPLATE is as per you message content and than after check it.

Abhishek Patel
  • 4,280
  • 1
  • 24
  • 38
  • Dear sir,This code is not working. i update my code please check if i am doing some thing wrong. – LoveAndroid Sep 09 '16 at 06:27
  • sorry ! i did not get you. can you please explain me ?? – LoveAndroid Sep 09 '16 at 06:32
  • ok..let's try.. If your message content like `9563 is your OTP for Reset Password for your app. Treat this as a confidential` than your `GENERAL_OTP_TEMPLATE` is like `(.*) is your OTP for Reset Password for your app. Treat this as a confidential` , here (.*) means any type of OTP means that is dynamic part. – Abhishek Patel Sep 09 '16 at 06:33
  • and change this line `Pattern generalOtpPattern = Pattern.compile(GENERAL_OTP_TEMPLATE); ` instead of `Pattern generalOtpPattern = Pattern.compile(message);` – Abhishek Patel Sep 09 '16 at 06:35
  • happy to know..always wc @LoveAndroid – Abhishek Patel Sep 09 '16 at 07:07
  • One more help i want to send the data(OTP number) in the activity.i tried shared prefernce and interface but it did not work.how can i set the otp number on the edittext and verify it. – LoveAndroid Sep 09 '16 at 07:30
  • for that you can Use `LocalBroadcastManager` to send OTP in your `Activity` please refer https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html or http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager – Abhishek Patel Sep 09 '16 at 07:58
  • @ Abhishek Patel Dear sir i need your help . if you are free then give me answer asap. my problem is that when i send the otp to another number by your code it takes the last OTP it received on the device.but when i send the OTP to another number i want that it does not take the OTP of the last message in the phone .how can i stop this to take OTP of the last message when i send the OTP to another device. please help me. – LoveAndroid Sep 12 '16 at 07:52