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.
CodePudding user response:
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);
}
}
CodePudding user response:
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],
);
}