28
votes

I'm trying to send a text message to a WhatsApp contact from a react-native apps , i found that i can do it through Linking

Linking.openURL('whatsapp://send?text=hello');

the code above opens only whats app , i need to open a chat with a specific number is there a paramter i have to send like text ?!

3
whatsapp://send?abid=(ID)&text=helloelmeister
this opens only screen sharing and ask me to choose which account i want to share my message with ! i need to open a direct chat with specific userAhmed Ali
also can you show my the format of abid ?Ahmed Ali

3 Answers

61
votes

You can use this to send a message to specific number:

Linking.openURL('whatsapp://send?text=hello&phone=xxxxxxxxxxxxx')

3
votes

You can use this method to send whatsApp message direct to a number.

Example link: https://wa.me/919234567812?text=%7B0%7D+Balaji+CTest

export const sendWhatsAppMessage = link => {
 if (!isUndefined(link)) {
  Linking.canOpenURL(link)
   .then(supported => {
     if (!supported) {
      Alert.alert(
        'Please install whats app to send direct message to students via whats 
         app'
      );
    } else {
      return Linking.openURL(link);
    }
  })
  .catch(err => console.error('An error occurred', err));
} else {
  console.log('sendWhatsAppMessage -----> ', 'message link is undefined');
 }
};
3
votes

share message to whatsapp to a specific predefined number independent to platform

    sendWhatsApp = () => {
    let msg = 'type something';
    let phoneWithCountryCode = 'xxxxxxxxxx';

    let mobile = Platform.OS == 'ios' ? phoneWithCountryCode : '+' + phoneWithCountryCode;
    if (mobile) {
      if (msg) {
        let url = 'whatsapp://send?text=' + msg + '&phone=' + mobile;
        Linking.openURL(url).then((data) => {
          console.log('WhatsApp Opened');
        }).catch(() => {
          alert('Make sure WhatsApp installed on your device');
        });
      } else {
        alert('Please insert message to send');
      }
    } else {
      alert('Please insert mobile no');
    }
  }

Please Note: send + in front of phone with country if opening in android