React Native 向特定的 whatsapp 号码发送消息

IT技术 reactjs react-native whatsapp
2021-05-15 10:05:47

我正在尝试从 react-native 应用程序向 WhatsApp 联系人发送短信,我发现我可以通过链接来完成

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

上面的代码只打开什么应用程序,我需要打开一个特定号码的聊天,我必须像文本一样发送参数吗?!

3个回答

您可以使用它向特定号码发送消息:

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

您可以使用此方法将 WhatsApp 消息直接发送到某个号码。

示例链接: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');
 }
};

将消息分享到 whatsapp 到独立于平台的特定预定义号码

    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');
    }
  }

请注意:如果在 android 中打开,请在手机前发送 + 国家