background_sms 0.0.4 background_sms: ^0.0.4 copied to clipboard
The main purpose is to send sms from background and headless.You can send sms whatever the phone state is in app or background or headless.
import 'package:background_sms/background_sms.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
_getPermission() async => await [
Permission.sms,
].request();
Future<bool> _isPermissionGranted() async =>
await Permission.sms.status.isGranted;
_sendMessage(String phoneNumber, String message, {int? simSlot}) async {
var result = await BackgroundSms.sendMessage(
phoneNumber: phoneNumber, message: message, simSlot: simSlot);
if (result == SmsStatus.sent) {
print("Sent");
} else {
print("Failed");
}
}
Future<bool?> get _supportCustomSim async =>
await BackgroundSms.isSupportCustomSim;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Send Sms'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.send),
onPressed: () async {
if (await _isPermissionGranted()) {
if ((await _supportCustomSim)!)
_sendMessage("09xxxxxxxxx", "Hello", simSlot: 1);
else
_sendMessage("09xxxxxxxxx", "Hello");
} else
_getPermission();
},
),
),
);
}
}