sendSMS method

  1. @override
Future<String?> sendSMS({
  1. required String message,
  2. required List<String> recipients,
})
override

Must be implemented by platform-specific classes

Implementation

@override
Future<String?> sendSMS({
  required String message,
  required List<String> recipients,
}) async {
  try {
    String numbers = '';

    /// Use semicolon for iOS, comma for others in URL scheme fallback
    final separator = Platform.isIOS ? ';' : ',';
    numbers = recipients.isNotEmpty && recipients.length > 1
        ? recipients.join(separator)
        : recipients.first;

    if (Platform.isIOS &&
        !Platform.environment.containsKey('SIMULATOR_DEVICE_NAME')) {
      try {
        final result = await _channel.invokeMethod<String>('sendSMS', {
          'message': message,
          'recipients': recipients,
        });
        debugPrint('SMS send result: $result');
        return result;
      } on PlatformException catch (e) {
        debugPrint('Failed to send SMS: ${e.message}');
        return 'Not Sent';
      }
    } else {
      /// Fallback: launch SMS app with URI scheme (for Android or iOS simulator)
      final encodedMessage = Uri.encodeComponent(message);
      String uriString = 'sms:$numbers?body=$encodedMessage';
      Uri smsUri = Uri.parse(uriString);

      if (await canLaunchUrl(smsUri)) {
        await launchUrl(smsUri);
        return 'Sent';
      } else {
        debugPrint('Could not launch SMS app');
        return 'Not Sent';
      }
    }
  } on PlatformException catch (e) {
    throw Exception('Failed to send SMS: ${e.message}');
  }
}