how to send an SMS without users interaction in flutter?

This is the old lib that I've used before,

Link: sms_maintained

note: this is a fork of a previous flutter sms library which was not maintained any more and hence the name. this one is currently active and working.

But as of 2021, I've switched to telephony and quite happy with it.


Actually to send an SMS programatically, you'll need to implement a platform channel and use SMSManager to send SMS.

Example:

Android Part:

First add appropriate permissions to AndroidManifest.xml.

<uses-permission android:name="android.permission.SEND_SMS" />

Then in your MainActivity.java:

package com.yourcompany.example;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
  private static final String CHANNEL = "sendSms";

  private MethodChannel.Result callResult;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
    new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
            new MethodChannel.MethodCallHandler() {
              @Override
              public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                if(call.method.equals("send")){
                   String num = call.argument("phone");
                   String msg = call.argument("msg");
                   sendSMS(num,msg,result);
                }else{
                  result.notImplemented();
                }
              }
            });
  }

  private void sendSMS(String phoneNo, String msg,MethodChannel.Result result) {
      try {
          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage(phoneNo, null, msg, null, null);
          result.success("SMS Sent");
      } catch (Exception ex) {
          ex.printStackTrace();
          result.error("Err","Sms Not Sent","");
      }
  }

}

Dart Code:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(new MaterialApp(
    title: "Rotation Demo",
    home: new SendSms(),
  ));
}


class SendSms extends StatefulWidget {
  @override
  _SendSmsState createState() => new _SendSmsState();
}

class _SendSmsState extends State<SendSms> {
  static const platform = const MethodChannel('sendSms');

  Future<Null> sendSms()async {
    print("SendSMS");
    try {
      final String result = await platform.invokeMethod('send',<String,dynamic>{"phone":"+91XXXXXXXXXX","msg":"Hello! I'm sent programatically."}); //Replace a 'X' with 10 digit phone number
      print(result);
    } on PlatformException catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Material(
      child: new Container(
        alignment: Alignment.center,
        child: new FlatButton(onPressed: () => sendSms(), child: const Text("Send SMS")),
      ),
    );
  }
}

Hope this helped!

** Note:

1.The example code dosen't show how to handle permission on android devices with version 6.0 and above. If using with 6.0 implement the right permission invoking code.

2.The example also dosen't implement choosing sim incase of dual sim handsets. If no default sim is set for sms on dual sim handsets, sms might not be sent.

Tags:

Dart

Flutter