点击按钮即可接收短信

问题描述 投票:0回答:1

我正在 Expo CLI 中使用 React Native 制作一个应用程序。在该应用程序上有很多产品,选择产品后将有一个“预订”按钮和一个时间段按钮,选择按钮后我(用户)将收到一条短信,即“您的产品已预订于(时间)时间段”。我怎样才能实现这个短信。

react-native sms
1个回答
0
投票

您需要安装 expo-sms npm 软件包才能使用此功能。

这里:https://docs.expo.dev/versions/latest/sdk/sms/

安装依赖

npx expo install expo-sms

示例:

async function sendSMSMessage() {
// Check if the device supports sending SMS
const isAvailable = await SMS.isAvailableAsync();
if (isAvailable) {
const { result } = await SMS.sendSMSAsync(
  ['+1234567890'], // phone numbers
  'Hello, this is a message from my Expo app!'
);
if (result === 'sent') {
  alert('Message sent!');
 } else {
  alert('Message send failed.');
 }
} else {
alert('SMS service is not available on this device.');
 }
}

import React from 'react';
import { Button } from 'react-native';
import * as SMS from 'expo-sms';

export default function App() {
  return (
    <Button title="Send SMS" onPress={sendSMSMessage} />
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.