2

We can able to send a text message directly to specific contact, but I want to send a file to whatsapp contact using url launcher. How to achieve this in flutter.

Below is my code for sending only message.

String message = "sample text";
String androidURL = "https://wa.me/917390123456/?text=${Uri.parse(message)}";

if(await canLaunchUrl(Uri.parse(URL))){
    await launchUrl(Uri.parse(URL),mode: LaunchMode.externalApplication);
}

This is working fine for sending text message. How to send a file by using the same. Can someone help.

2 Answers2

2

To send a file, you can use the whatsapp://send?file=<file_url>" URL scheme. To send an image, you can use "whatsapp://send?text=&file=<file_url>" and the file format should be in jpeg or png format.

import 'package:url_launcher/url_launcher.dart';

// ...

void _sendFileToWhatsApp() async {
  var phoneNumber = "917390123456";
  var fileUrl = "https://example.com/file.pdf"; // Replace with the URL of the file you want to send
  var url = "whatsapp://send?phone=$phoneNumber&file=$fileUrl";

  if (await canLaunchUrl(url)) {
    await launchUrl(url);
  }
}
Gwhyyy
  • 7,554
  • 3
  • 8
  • 35
  • Thanks for your response @Gwhyyy . Can I able to send my local file by passing my file path instead of passing this server pdf url `var fileUrl = "https://example.com/file.pdf";` My expectation is to send local file which is available in my mobile using this URL launcher. Can you help me to find it out. – Venkat Pandi Jan 19 '23 at 14:52
2

check here. Share images or files _image1.path contains path of the file which is shared to the whatsapp. check here whatsapp_share

Future<void> shareFile() async {
await WhatsappShare.shareFile(
  text: 'Whatsapp share text',
  phone: '911234567890',
  filePath: [_image1.path, _image2.path],
);

}

Mahesh
  • 862
  • 5
  • 11
  • Thanks for your reply @Mahesh . I have tried this plugin, I can able to send file but the problem is, It is asking me to select the user instead of auto select user(given phone number) and opening into their account. Is that applicable only for business whatsapp account ? Please let me know if any code changes I need to do. – Venkat Pandi Jan 19 '23 at 14:45