70 lines
2.2 KiB
Dart
70 lines
2.2 KiB
Dart
// ignore_for_file: avoid_print
|
|
import 'dart:io';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:simcards/sim_card.dart';
|
|
import 'package:simcards/simcards.dart';
|
|
import 'package:flutter_sms/flutter_sms.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 {
|
|
// Generate a unique ID
|
|
var uuid = const Uuid();
|
|
String uniqueId = uuid.v4(); // Generates a random v4 UUID
|
|
|
|
// The message will be the unique ID
|
|
String smsMessage = uniqueId;
|
|
|
|
// flutter_sms sends the message using the device's default SMS app.
|
|
// It does not programmatically select a SIM card. The 'selectedSim'
|
|
// parameter from the dialog will not be used to choose the sending SIM.
|
|
String result = await sendSMS(
|
|
message: smsMessage,
|
|
recipients: [destinationNumber],
|
|
sendDirect: true, // This attempts to send directly without opening the messaging app.
|
|
// It requires more permissions and might not work on all devices/Androidversions.
|
|
// If false, it will open the default SMS app.
|
|
);
|
|
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.");
|
|
}
|
|
}
|
|
} |