flutter_sms icon indicating copy to clipboard operation
flutter_sms copied to clipboard

Android Messages app opens on call to `sendSMS`

Open johnwargo opened this issue 3 years ago • 2 comments

I created a new Flutter app today and added the permission_handler and flutter_sms packages. When the app sends the SMS message, the device's Messages app opens to the selected recipient with the message text in the input box ready to send. I have to tap the send button to send the SMS. Is this expected behavior?

Here's my Flutter environment:

Flutter 3.0.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision cd41fdd495 (9 days ago) • 2022-06-08 09:52:13 -0700
Engine • revision f15f824b57
Tools • Dart 2.17.3 • DevTools 2.12.2

and here's the code that sends the message:

Future<void> _sendSMS(String msg) async {
    bool result;
    String targetName;
    String targetPhone;
    List<String> recipients = [];

    // Can the device send SMS?
    result = await canSendSMS();
    if (!result) {
      logMessage("Device cannot send SMS messages");
      return;
    }
    log.info("Device can send SMS messages");

    // Do we have permission to send SMS?
    result = await Permission.sms.request().isGranted;
    if (!result) {
      logMessage("Permission denied");
      return;
    }
    log.info("Permission granted");

    // Populate the target name and phone number variables
    if (_isTest) {
      targetName = Strings.targetNameDev;
      targetPhone = Strings.targetPhoneDev;
    } else {
      targetName = Strings.targetNameProd;
      targetPhone = Strings.targetPhoneProd;
    }
    log.info("Sending message to $targetName ($targetPhone)");
    recipients.clear();
    recipients.add(targetPhone);
    try {
      String result = await sendSMS(
        message: msg,
        recipients: recipients,
        sendDirect: sendDirect,
      );
      log.info(result);
    } catch (error) {
      log.info(error.toString());
    }
  }

johnwargo avatar Jun 18 '22 00:06 johnwargo

I'm guessing this is due to some new security restriction on apps sending SMS messages. Can someone confirm?

johnwargo avatar Jun 19 '22 11:06 johnwargo

Not sure why, it could be a security restriction

However, take a look here: https://pub.dev/packages/flutter_sms#sending-direct With that approach (set sendDirect to true) you will not have that issue, tested it on Android 12 just now.

BRUHItsABunny avatar Jul 29 '22 05:07 BRUHItsABunny

Hello, im facing the same problem i want my app to not opening the mobile sms app, i want it to send the message direct from my flutter application, if you found the right way to do that please let me know ..

ghanayemomar avatar Jan 09 '23 17:01 ghanayemomar

@BRUHItsABunny that worked for me. thanks!

johnwargo avatar Jan 09 '23 21:01 johnwargo