SMS integrated with new ui
This commit is contained in:
102
lib/api/services/send_sms_service.dart
Normal file
102
lib/api/services/send_sms_service.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:send_message/send_message.dart' show sendSMS;
|
||||
import 'package:simcards/sim_card.dart';
|
||||
import 'package:simcards/simcards.dart';
|
||||
|
||||
// This enum provides detailed status back to the UI layer.
|
||||
enum PermissionStatusResult { granted, denied, permanentlyDenied, restricted }
|
||||
|
||||
class SmsService {
|
||||
final Simcards _simcards = Simcards();
|
||||
|
||||
/// Handles the requesting of SMS and Phone permissions.
|
||||
/// Returns a detailed status: granted, denied, or permanentlyDenied.
|
||||
Future<PermissionStatusResult> handleSmsPermission() async {
|
||||
var smsStatus = await Permission.sms.status;
|
||||
var phoneStatus = await Permission.phone.status;
|
||||
|
||||
// Check initial status
|
||||
if (smsStatus.isGranted && phoneStatus.isGranted) {
|
||||
return PermissionStatusResult.granted;
|
||||
}
|
||||
if (smsStatus.isPermanentlyDenied || phoneStatus.isPermanentlyDenied) {
|
||||
return PermissionStatusResult.permanentlyDenied;
|
||||
}
|
||||
if (smsStatus.isRestricted || phoneStatus.isRestricted) {
|
||||
return PermissionStatusResult.restricted;
|
||||
}
|
||||
|
||||
// Request permissions if not granted
|
||||
print("Requesting SMS and Phone permissions...");
|
||||
await [Permission.phone, Permission.sms].request();
|
||||
|
||||
// Re-check status after request
|
||||
smsStatus = await Permission.sms.status;
|
||||
phoneStatus = await Permission.phone.status;
|
||||
|
||||
if (smsStatus.isGranted && phoneStatus.isGranted) {
|
||||
return PermissionStatusResult.granted;
|
||||
}
|
||||
if (smsStatus.isPermanentlyDenied || phoneStatus.isPermanentlyDenied) {
|
||||
return PermissionStatusResult.permanentlyDenied;
|
||||
}
|
||||
if (smsStatus.isRestricted || phoneStatus.isRestricted) {
|
||||
return PermissionStatusResult.restricted;
|
||||
}
|
||||
|
||||
// If none of the above, it's denied
|
||||
return PermissionStatusResult.denied;
|
||||
}
|
||||
|
||||
/// Tries to send a single verification SMS.
|
||||
/// This should only be called AFTER permissions have been granted.
|
||||
Future<bool> sendVerificationSms({
|
||||
required BuildContext context,
|
||||
required String destinationNumber,
|
||||
required String message,
|
||||
}) async {
|
||||
try {
|
||||
List<SimCard> simCardList = await _simcards.getSimCards();
|
||||
if (simCardList.isEmpty) {
|
||||
print("No SIM card detected.");
|
||||
return false;
|
||||
}
|
||||
return await _sendSms(destinationNumber, message, simCardList.first);
|
||||
} catch (e) {
|
||||
print("An error occurred in the SMS process: $e");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Private function to perform the SMS sending action.
|
||||
Future<bool> _sendSms(
|
||||
String destinationNumber, String message, SimCard selectedSim) async {
|
||||
if (Platform.isAndroid) {
|
||||
try {
|
||||
String smsMessage = message;
|
||||
String result = await sendSMS(
|
||||
message: smsMessage,
|
||||
recipients: [destinationNumber],
|
||||
sendDirect: true,
|
||||
);
|
||||
print("Background SMS send attempt result: $result");
|
||||
|
||||
if (result.toLowerCase().contains('sent')) {
|
||||
print("Success: SMS appears to have been sent.");
|
||||
return true;
|
||||
} else {
|
||||
print("Failure: SMS was not sent. Result: $result");
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error attempting to send SMS directly: $e");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
print("SMS sending is only supported on Android.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user