10

I know about the package flutter_sms which is used for reading, writing and sending SMS.

But as I can see it will ask from user SEND_SMS permission also which is not needed and also google doesn't allow asking for this permission if your app does not solely depend on this feature.

Can anyone help me with Reading OTP from SMS with flutter?

Or can help with writing the functionality natively for both IOS and Android and integrating it with flutter.

8
  • You can't read OTP without using SMS permission in your Manifest.
    – CopsOnRoad
    Mar 30, 2019 at 11:29
  • Yes but why to ask for SEND Permission when you are not going to require it. It create BAD impression in that case. So I cant use that library
    – CodeGeek
    Mar 30, 2019 at 13:01
  • Your title says to read OTP, I commented you need SMS permission to read OTP.
    – CopsOnRoad
    Mar 30, 2019 at 14:11
  • I know that we need SMS permission to Read OTP but I have specified below what actually I mean to say for that title
    – CodeGeek
    Mar 30, 2019 at 14:26
  • Can anyone help me with Reading OTP from SMS with flutter? -- Not possible without SMS permission. Or can help with writing the functionality natively for both IOS and Android and integrating it with flutter -- Again not possible without having SMS permission. You can only send SMS without the permission but to read it you need SMS permission else fall back to ContentProvider
    – CopsOnRoad
    Mar 30, 2019 at 14:39

3 Answers 3

5

Check out this plugin. https://pub.dev/packages/sms_autofill#-readme-tab-

For iOS, this package is not needed as the SMS autofill is provided by default, but not for Android, that's where this package is useful.

No permission to read SMS messages is asked to the user as there no need thanks to SMSRetriever API.

You have two widgets at your disposable for autofill an SMS code, PinFieldAutoFill and TextFieldPinAutoFill.

Just before you sent your phone number to the backend, you need to let know the plugin that it need to listen for the SMS with the code.

To do that you need to do:

await SmsAutoFill().listenForCode;

This will listen for the SMS with the code during 5 minutes and when received, autofill the following widget.

6
  • try to provide a short hint about your solution. link wont help you to reach.
    – Agilanbu
    Dec 10, 2019 at 8:43
  • The example is really confusing, can anyone help? Jun 4, 2020 at 9:15
  • For the code to be receive, it need to follow some rules as describe here: developers.google.com/identity/sms-retriever/verify Dec 30, 2020 at 16:13
  • 1
    this guy just copied the package description, I am here because the package description doesn't specify how to read SMS on iOS. Jan 4, 2021 at 12:15
  • iPhone auto-retrieve OTP and suggest you on top of your keyboard. Jan 5, 2021 at 13:30
1

By looking at the package you pointed out, 2 things come from the repo

  1. it's not maintained anymore
  2. it's only for android.

Possible solutions

  1. look at this fork - https://pub.dartlang.org/packages/sms_maintained
  2. assuming you don't need ios
  3. you said you don't want to have to require permission for sending. https://pub.dartlang.org/packages/sms_receiver
  4. what kind of OTP you are trying to implement, there are several packages for phone authentication, if it's firebase:

overall I think you could go with the sms_receiver and listen to specific messages, but it seems you have to listen again after every message received which is odd, but I guess for a purpose of authentication is ok, since you need the sms after the user clicked on some login button probably.

2
  • No, I am not using firebase and I will check with all the information your provided and will revert you.
    – CodeGeek
    Mar 31, 2019 at 6:41
  • i've got error when using sms_maintained. i build both android and ios app. when i run in ios, it gives me error [!] No podspec found for sms_maintained in .symlinks/plugins/sms_maintained/ios. how to make my ios app can running well? Jul 26, 2019 at 7:56
0

I have used this plugin for Reading OTP from SMS automatically

Check out this plugin. https://pub.dev/packages/sms_otp_auto_verify

First you have include sms_otp_auto_verify: ^1.2.2 in pubspec.yaml

No need of any permission add in the manifest file

Here get signature code (by calling _getAppSignature) first which is need for server for include code in your SMS format (incoming SMS), for example:

<#> 12345 is your OTP to Sign-In to Your app name. It's valid for 30 mins. Don't share it with anyone. xyZaBcD2EFg

  ///Controller
  TextEditingController textController;

      @override
      void initState() {
        super.initState();
        /// State initialization code here..
     textController = TextEditingController();
        _getAppSignature();
        _startListeningSms();
      }

     @override
      void dispose() {
        super.dispose();
        ///stopListening
        SmsRetrieved.stopListening();
      }

 /// Get signature code
  _getAppSignature() async {
    String signature = await SmsRetrieved.getAppSignature();
    print("App Hash Key:  $signature");
  }

///Here ListeningSms
_startListeningSms() async {
    String otp = await SmsRetrieved.startListeningSms();
    if (otp.isNotEmpty || otp != null) {
      textController.text= otp.split(" ")[1];
    }
  }

Not the answer you're looking for? Browse other questions tagged or ask your own question.