Tree
Tree

Reputation: 31371

How to launch whatsapp and facebook messenger window from Flutter to point to a specific contact?

Using url_launcher I can open phone to call a number.

 var phone = "+123456789";
 launch("tel://$phone");

How can I do the same for facebook messenger and whatsapp?

Upvotes: 20

Views: 37419

Answers (6)

thyagoluciano
thyagoluciano

Reputation: 329

To open WhatsApp you can use this plugin: https://pub.dartlang.org/packages/flutter_launch

1. Add this to your package's pubspec.yaml file:

dependencies:
    flutter_launch: "^0.3.0"

2. Install it

$ flutter packages get

3. Import it

import 'package:flutter_launch/flutter_launch.dart';

4. Example:

await FlutterLaunch.launchWhatsapp(phone: "5534992019999", message: "Hello");

Complete example: https://pub.dartlang.org/packages/flutter_launch#-installing-tab-

Upvotes: 10

sultanmyrza
sultanmyrza

Reputation: 5412

import 'package:url_launcher/url_launcher.dart';

void _contactViaWhatsApp(context) async {
    String whatsAppUrl = "";

    String phoneNumber = 'your-phone-number';
    String description = "your-custom-message";

    if (Platform.isIOS) {
      whatsAppUrl =
          'whatsapp://wa.me/$phoneNumber/?text=${Uri.parse(description)}';
    } else {
      whatsAppUrl =
          'https://wa.me/+$phoneNumber?text=${Uri.parse(description)}';
    }

    if (await canLaunch(whatsAppUrl)) {
      await launch(whatsAppUrl);
    } else {
      final snackBar = SnackBar(
        content: Text("Install WhatsApp First Please"),
      );
      ScaffoldMessenger.of(context).showSnackBar(snackBar);
    }
  }

Upvotes: 0

Kamal Bunkar
Kamal Bunkar

Reputation: 1452

You can use Flutter URL lanucher plugin to launch whatsapp app. There you need to add conditions for android and iphone. You can read complete Flutter tutorial here.

for android

var whatsappURl_android = "whatsapp://send?phone="+whatsapp+"&text=hello";

for iphone

var whatappURL_ios ="https://wa.me/$whatsapp?text=${Uri.parse("hello")}";

first detect the phone OS version - android OR iOS

    if(Platform.isIOS){
    // for iOS phone only
    if( await canLaunch(whatappURL_ios)){
       await launch(whatappURL_ios, forceSafariVC: false);
    }else{
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: new Text("whatsapp no installed")));

    }

  }else{
    // android , web
    if( await canLaunch(whatsappURl_android)){
      await launch(whatsappURl_android);
    }else{
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: new Text("whatsapp no installed")));

    }


  }

Upvotes: 0

Felipe Sales
Felipe Sales

Reputation: 1139

Import package url_launcher:

 url_launcher: ^6.0.3

Import dependency:

import 'package:url_launcher/url_launcher.dart';

Put your url:

const _url = 'https://api.whatsapp.com/...';

Create your function:

void _launchURL() async => await canLaunch(_url) 
    ? await launch(_url) : throw 'Not found $_url';

Use, por example in Button:

FloatingActionButton(
  onPressed: _launchURL,
  ...
),

This works for me! :D

Upvotes: 3

Param Mittal
Param Mittal

Reputation: 99

we can use the flutter package https://pub.dev/packages/url_launcher

For sending with number and text --->

whatsapp://send?phone=XXXXXXXXX&text=Hellothere!

For sending only text --->

https://api.whatsapp.com/send?text=Hellothere!

Upvotes: 0

Tree
Tree

Reputation: 31371

I found the solution.

To open whatsapp:

var whatsappUrl ="whatsapp://send?phone=$phone";
await canLaunch(whatsappUrl)? launch(whatsappUrl):print("open whatsapp app link or do a snackbar with notification that there is no whatsapp installed");

To open messenger or facebook:

First get shortened url

If your facebook profile is facebook.com/yourName facebook url would be fb.me/yourName and messenger url would be m.me/yourName

then you do launch("http://$messengerUrl");

Facebook website will automatically open the link in the app even though it goes trough URL browser. If there is no app installed, it will go to the app/play store

Upvotes: 65

Related Questions