Send SMS Automatically using Flutter
To send SMS automatically using Flutter, you can make use of the sms
package which provides the necessary functionalities for sending SMS messages. Here is how you can achieve this:
1. Add the sms package to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
sms: ^2.0.0
2. Import the necessary package:
import 'package:sms/sms.dart';
3. Sending an SMS:
void sendSMS() {
SmsSender sender = new SmsSender();
String address = "";
String message = "";
SmsMessage smsMessage = new SmsMessage(address, message);
sender.sendSms(smsMessage);
}
In the above code, replace <recipient_phone_number>
with the phone number of the recipient and <your_message>
with the message you want to send. This code will send the SMS message to the specified phone number automatically.
4. Requesting SMS permission:
For Android devices, you need to add the SMS permission in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.SEND_SMS"/>
5. Example Usage:
import 'package:flutter/material.dart';
import 'package:sms/sms.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Send SMS'),
),
body: Center(
child: RaisedButton(
onPressed: () {
sendSMS();
},
child: Text('Send SMS'),
),
),
),
);
}
void sendSMS() {
SmsSender sender = new SmsSender();
String address = "";
String message = "";
SmsMessage smsMessage = new SmsMessage(address, message);
sender.sendSms(smsMessage);
}
}
In the above example, a simple Flutter app is created with a button labeled “Send SMS”. Upon pressing the button, the sendSMS()
function will be called to send the SMS message using the values provided. Make sure to replace <recipient_phone_number>
and <your_message>
with appropriate values.
That’s it! You have now successfully implemented the automatic sending of SMS in Flutter.