63 lines
1.6 KiB
Dart
63 lines
1.6 KiB
Dart
// ignore_for_file: avoid_print
|
|
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:send_message/send_message.dart' show sendSMS;
|
|
import 'package:simcards/sim_card.dart';
|
|
import 'package:simcards/simcards.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
class SmsService {
|
|
final Simcards _simcards = Simcards();
|
|
|
|
Future<void> sendVerificationSms({
|
|
required BuildContext context,
|
|
required String destinationNumber,
|
|
required String message,
|
|
}) async {
|
|
try {
|
|
await _simcards.requestPermission();
|
|
|
|
bool permissionGranted = await _simcards.hasPermission();
|
|
if (!permissionGranted) {
|
|
print("Permission denied." );
|
|
return;
|
|
}
|
|
|
|
List<SimCard> simCardList = await _simcards.getSimCards();
|
|
if (simCardList.isEmpty) {
|
|
print("No SIM detected." );
|
|
return;
|
|
}
|
|
|
|
await _sendSms(destinationNumber, message, simCardList.first);
|
|
|
|
} catch (e) {
|
|
print("Error in SMS process: $e");
|
|
}
|
|
}
|
|
|
|
|
|
Future<void> _sendSms(
|
|
String destinationNumber, String message, SimCard selectedSim) async {
|
|
if (Platform.isAndroid) {
|
|
try {
|
|
var uuid = const Uuid();
|
|
String uniqueId = uuid.v4();
|
|
|
|
String smsMessage = uniqueId;
|
|
String result = await sendSMS(
|
|
message: smsMessage,
|
|
recipients: [destinationNumber],
|
|
sendDirect: true,
|
|
);
|
|
print("SMS send result: $result. Sent via ${selectedSim.displayName} (Note: OS default SIM isused).");
|
|
|
|
} catch (e) {
|
|
print("Error sending SMS: $e");
|
|
}
|
|
} else {
|
|
print("SMS sending is only supported on Android.");
|
|
}
|
|
}
|
|
} |