如何在React Native中以编程方式发送短信?

5
我是React Native新手。我想在我的Android应用程序中添加一个发送短信的组件,但是我尝试了几个不同的组件,每次都遇到一些错误。有没有人成功地完成了这个?请帮帮我。
这是我最后得到的错误。 enter image description here

请添加您的错误和代码。 - Hamza Zafeer
谢谢你的回复。我编辑了我的问题并附上了错误的图像链接。 - themiremad
2个回答

4
我推荐使用这个网站来发送短信:https://github.com/anarchicknight/react-native-communications 关于命名冲突错误,尝试执行以下步骤:rm -rf node_modules && npm i,然后清除缓存并重启打包器:npm start -- --reset-cache 如果这样还不起作用,请确保SMS包的版本与你的包中的react-native版本相匹配。

4
react-native-communications(2.2.1版本)无法自动发送短信,它只能弹出一个填好内容的对话框,等待用户手动发送。 - Nir O.
1
是的,完全正确。我已经安装了它,因为这个答案而浪费了2个小时才找到。 - Leogout
我认为react-native-communications有一个缺点,因此它不能在后台工作,是吗? - MojtabaRezaeimehr

-4

试试这段代码

    String strSmsBody = "Hello";
    //sms recipient id
    String strReceipents = "abc@gmail.com"; 
    SmsManager sms = SmsManager.getDefault();
    List<String> messages = sms.divideMessage(strSmsBody);
    for (String message : messages) {
        sms.sendTextMessage(strReceipents, null, message, PendingIntent.getBroadcast(
                this, 0, new Intent(ACTION_SMS_SENT), 0), null);
    }

不要忘记在清单文件中添加这行代码。
        <uses-permission android:name="android.permission.SEND_SMS" />

谢谢您,但我想要一个适用于React Native框架的解决方案。我正在使用React Native框架构建一个Android应用程序。 - themiremad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接