diff --git a/lib/api/services/send_sms_service.dart b/lib/api/services/send_sms_service.dart index 76f122a..5c83042 100644 --- a/lib/api/services/send_sms_service.dart +++ b/lib/api/services/send_sms_service.dart @@ -1,92 +1,70 @@ // 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 sendVerificationSms({ - required BuildContext context, - required String destinationNumber, - required String message, - }) async { - try { - // Request permission to access SIM cards - await _simcards.requestPermission(); + Future 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; - } + bool permissionGranted = await _simcards.hasPermission(); + if (!permissionGranted) { + print("❌ Permission denied." ); + return; + } - List simCardList = await _simcards.getSimCards(); - if (simCardList.isEmpty) { - print("❌ No SIM detected."); - return; - } + List simCardList = await _simcards.getSimCards(); + if (simCardList.isEmpty) { + print("❌ No SIM detected." ); + return; + } - // If only one SIM card is available, send SMS directly - if (simCardList.length == 1) { - await _sendSms(destinationNumber, message, simCardList.first); - return; - } + await _sendSms(destinationNumber, message, simCardList.first); - // Show a dialog to let the user select a SIM card - int? selectedSimSlot = await showDialog( - context: context, - builder: (BuildContext dialogContext) { - int? tempSelection; - return AlertDialog( - title: const Text("Select SIM to send SMS"), - content: StatefulBuilder( - builder: (BuildContext context, StateSetter setState) { - return DropdownButton( - hint: const Text("Choose SIM"), - value: tempSelection, - onChanged: (int? newValue) { - setState(() { - tempSelection = newValue; - }); - }, - items: List.generate(simCardList.length, (index) { - final sim = simCardList[index]; - return DropdownMenuItem( - value: index, - child: Text(sim.displayName ?? "SIM ${index + 1}"), - ); - }), - ); - }, - ), - actions: [ - TextButton( - onPressed: () { - Navigator.pop(dialogContext, tempSelection); - }, - child: const Text("OK"), - ), - ], - ); - }, - ); + } catch (e) { + print("⚠️ Error in SMS process: $e"); + } + } - if (selectedSimSlot == null) { - print("⚠️ SIM not selected."); - return; - } - // Send SMS using the selected SIM card - await _sendSms(destinationNumber, message, simCardList[selectedSimSlot]); - } catch (e) { - print("⚠️ Error sending SMS: $e"); - } - } + Future _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 - Future _sendSms( - String destinationNumber, String message, SimCard selectedSim) async { - // SMS Logic Here - print("✅ SMS sent via ${selectedSim.displayName}"); - } + // 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."); + } + } } \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 8b2ecee..f6fd46e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -307,6 +307,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.2" + flutter_sms: + dependency: "direct main" + description: + name: flutter_sms + sha256: "2fe5f584f02596343557eeca56348f9b82413fefe83a423fab880cdbdf54d8d8" + url: "https://pub.dev" + source: hosted + version: "2.3.3" flutter_svg: dependency: "direct main" description: @@ -955,7 +963,7 @@ packages: source: hosted version: "3.1.4" uuid: - dependency: transitive + dependency: "direct main" description: name: uuid sha256: a5be9ef6618a7ac1e964353ef476418026db906c4facdedaa299b7a2e71690ff diff --git a/pubspec.yaml b/pubspec.yaml index f21de16..82dd879 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -62,6 +62,8 @@ dependencies: showcaseview: ^2.0.3 package_info_plus: ^4.2.0 simcards: ^0.0.1 + flutter_sms: ^2.3.3 + uuid: ^4.5.1 # jailbreak_root_detection: "^1.1.6"