fluttercommunity / flutter_sms

A Flutter plugin to Send SMS and MMS on iOS and Android. If iMessage is enabled it will send as iMessage on iOS. This plugin must be tested on a real device on iOS. Maintainer: @rodydavis
https://fluttercommunity.github.io/flutter_sms/
MIT License
241 stars 176 forks source link

Android Messages app opens on call to `sendSMS` #67

Closed johnwargo closed 1 year ago

johnwargo commented 2 years ago

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 commented 2 years ago

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

BRUHItsABunny commented 1 year ago

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.

ghanayemomar commented 1 year ago

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 ..

johnwargo commented 1 year ago

@BRUHItsABunny that worked for me. thanks!