Example to Send WhatsApp Message from React Native App

Introduction

This is an Example to Send WhatsApp Message from React Native App. This post can be very helpful for you if you want to add the sharing facility to your app if you have seen our last post on React Native Share API to Share TextInput message you can see that it was providing all the possible solutions available in the device to share the input text but what if you just want to share the content on WhatsApp? So for that, we have made this post to share the content on WhatsApp.

We are going to use deep linking property and will be going to open WhatsApp using Linking component provided by React Native core library. You can also trigger Whatsapp Web in the browser to open WhatsApp in the Webview if the user has no WhatsApp installed.

You can also see:

  1. React Native Share API to Share TextInput message
  2. Share Facebook Post with URL from React Native App
  3. Tweet on Twitter with URL from React Native App
  4. Send Text SMS on Button Click in React Native

To Send WhatsApp Message from React Native App

Linking.openURL('whatsapp://send?text=' + this.state.msg + '&phone=91' + this.state.mobile_no);

In this example, we are taking the mobile number and message as input from the user, and then we will send the WhatsApp message. So let’s get started.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

This will make a project structure with an index file named App.js in your project directory.

Code

Open App.js in any code editor and replace the code with the following code.

App.js

// Example to Send WhatsApp Message from React Native App
// https://aboutreact.com/send-whatsapp-message/

// import React in our code
import React, {useState} from 'react';

// import all the components we are going to use
import {
  SafeAreaView,
  StyleSheet,
  View,
  Text,
  TouchableOpacity,
  TextInput,
  Linking,
} from 'react-native';

const App = () => {
  const [mobileNumber, setMobileNumber] = useState('');
  const [whatsAppMsg, setWhatsAppMsg] = useState(
    'Please follow https://aboutreact.com',
  );

  const initiateWhatsApp = () => {
    // Check for perfect 10 digit length
    if (mobileNumber.length != 10) {
      alert('Please insert correct WhatsApp number');
      return;
    }
    // Using 91 for India
    // You can change 91 with your country code
    let url =
      'whatsapp://send?text=' + 
       whatsAppMsg +
      '&phone=91' + mobileNumber;
    Linking.openURL(url)
      .then((data) => {
        console.log('WhatsApp Opened');
      })
      .catch(() => {
        alert('Make sure Whatsapp installed on your device');
      });
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.container}>
        <Text style={styles.titleText}>
          Example to Send WhatsApp Message from React Native App
        </Text>
        <Text style={styles.titleTextsmall}>
          Enter WhatsApp Number
        </Text>
        <TextInput
          value={mobileNumber}
          onChangeText={
            (mobileNumber) => setMobileNumber(mobileNumber)
          }
          placeholder={'Enter WhatsApp Number'}
          keyboardType="numeric"
          style={styles.textInput}
        />
        <Text style={styles.titleTextsmall}>
          WhatsApp Message
        </Text>
        <TextInput
          value={whatsAppMsg}
          onChangeText={
            (whatsAppMsg) => setWhatsAppMsg(whatsAppMsg)
          }
          placeholder={'WhatsApp Message'}
          style={styles.textInput}
        />
        <TouchableOpacity
          activeOpacity={0.7}
          style={styles.buttonStyle}
          onPress={initiateWhatsApp}>
          <Text style={styles.buttonTextStyle}>
            Send WhatsApp Message
          </Text>
        </TouchableOpacity>
      </View>
    </SafeAreaView>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    padding: 10,
  },
  titleText: {
    fontSize: 22,
    textAlign: 'center',
    fontWeight: 'bold',
  },
  titleTextsmall: {
    marginVertical: 8,
    fontSize: 16,
  },
  buttonStyle: {
    justifyContent: 'center',
    marginTop: 15,
    padding: 10,
    backgroundColor: '#8ad24e',
  },
  buttonTextStyle: {
    color: '#fff',
    textAlign: 'center',
  },
  textInput: {
    height: 40,
    borderColor: 'gray',
    borderWidth: 1,
    width: '100%',
    paddingHorizontal: 10,
  },
});

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

FAQ: How can I send WhatsApp messages directly from my react native application without opening the WhatsApp?
Ans: Sorry but this is not possible with this example. WhatsApp does not provide any plugin or API for that I have used a deep-linking property to send the WhatsApp message in this example.

Output Screenshots

Image   Image   Image

Output in Online Emulator

This is how you can Send WhatsApp Message from React Native App. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

22 thoughts on “Example to Send WhatsApp Message from React Native App”

  1. One of the developer told me that commercial use of whatapp API is not very straight forward and it involves lots of certification. On the opposite side your example look fairly easy to implement. Can you please suggest if any app is allowed to use whatsapp API or there are some restrictions around it.

    Reply
    • If you are talking about the WhatsApp API then yes it needs some permission and not legal without permission but in this eaxmple, we have simply used the concept of deep linking. Indirectly we are opening the web.whatsapp in the browser but due to deep-linking property in WhatsApp, it is opening it in the application. It is completely fair to use this as this feature is used by many bloggers or the websites to make the share option. Don’t worry just go for it. 🙂

      Reply
    • Hello, I tried to find some good posts related to that but nothing is worthy on the internet using PHP and MySQL and personally I won’t recommend you go for the PHP backend in case of the chat application as it needs continuous polling to the server which will drain the battery and waste the resources instead go for the node or another backend where you can find socket io as a better option for the chat.

      Reply
  2. Nice post Snehal. Thanks.
    Pls answer me this:
    I want users to share messages from my react web app to whatsapp contacts on their phones.
    Then I need to know how many contacts each user has shared my message with.

    How would I go about that?
    Thanks.

    Reply
  3. Nice post Snehal. Thanks.

    could you tell me if you have already made an application for sending whatsapp messages en masse, or would you recommend an application for that?

    would this system you made have the ability to import a phone list and send at the same time?

    thanks bro.

    Reply
  4. HI
    I have used
    const url = `whatsapp://send?phone=${+91xxxxxxxxxx}&text=${‘Hi’}`
    const supported = await Linking.canOpenURL(url);
    if (supported) {
    await Linking.openURL(url);
    } else {
    console.log(‘Whatsapp application not found’);
    }

    this works on ios after adding property in info.plist.
    But this is not working on android always showing “Whatsapp application not found”.
    Do we need to make any change in manifest file?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.