sms_autofill Unable to detect the OTP

Viewed 833

I have used the sms_autofill plugin to to auto-detect the OTP received on mobile and type it in the given PinFieldAutoFill, As per https://pub.dev/packages/sms_autofill#-readme-tab- I am using await SmsAutoFill().listenForCode; function before making my API request & when the request has got a 200 status code I'm navigating it to another page and in init I'm listening to the OTP SMS received by using await SmsAutoFill().listenForCode; I thought like maybe when the page is initialized it wont be listening again to any SMS so i did add a Timer also but still it was not receiving anything.

Dependency - sms_autofill: ^1.2.6

Code : Page1

Center(
        child: RaisedButton(
          child: Text('Register'),
          onPressed: () async {
            await SmsAutoFill().listenForCode;
            final signCode = await SmsAutoFill().getAppSignature;
            print(signCode);
            //Http request code
          },
        ),
      ),

Code : Page2

String appSignature;
  String otpCode;
  Timer timer;

  @override
  void codeUpdated() {
    setState(() {
      otpCode = code;
    });
  }

  @override
  void initState() {
    super.initState();
    listenOTP();
//    timer = Timer.periodic(Duration(seconds: 10), (Timer t){
//      print('OTP Listening');
//      listenOTP();
//    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "Code Received: $otpCode",
            ),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 50),
              child: PinFieldAutoFill(
                codeLength: 6,
                onCodeChanged: (val) {
                  print(val);
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  void listenOTP() async {
    print('listen for code');
    await SmsAutoFill().listenForCode;
  }
1 Answers

Maybe your sms format is not right. The sms format should be like:

ExampleApp: Your code is 123456 FA+9qCX9VSu //this is your app signature

if your message is different. You can follow this answer to handle any type of sms format otp https://stackoverflow.com/a/70076071/6067774

Related