How to read OTP and paste into application using Java code

Hi everyone,

Please help me in copying OTP from message my requirement is as follows

  1. App sends an OTP.
  2. Go to Notifications and drag the bar…
  3. Click on the sender’s name and open the message.
  4. Read OTP from the message and sent it back to the app.

Would be great help i am searching for many days for this but no help

Hi Sachin,
use the switching concept …you can handle the OTP.!!!
1.Before switching to another application i.e(message) you should know the Activity and package name of it.
2.Read the OTP message and store/write the OTP in Excel sheet.
3.Switch to main application with it Activity and package name.
4.Read the the OTP from excel sheet and write into application.




Try this

As there are two options to read OTP:

  1. Start the activity of the messaging app and then read the otp from there.
  2. Read the OTP from the notification tray.
    Below the function to read the OTP in Appium:

public static String readOTP()
//driver.startActivity(“”, “”);
String otp = driver.findElementByXPath("//*[contains(@text,‘is’)]").getText().split(“code:” )[0];
return otp;

I have done it the way it is asked i.e. using Notification panel to read OTP.
No need to remember the current running activity, no switching


  1. Open the notification panel by using
  2. Now loop through the textView class looking for text containing “OTP”
  3. extract the OTP from the text using string manipulation
  4. close the notification screen using
    driver.pressKey(new KeyEvent(AndroidKey.BACK));
  5. Enter the extracted OTP

To know more in detail you can go to my GitHub project here