How to send "Happy new year" SMS using react-native on Android
Tags: reactnative, react, android, ios
Published: 2021-01-01 11:41:17

How to send a simple "Happy new year" SMS using react-native on Android.

The code of the whole app build here is available at [https://github.com/Merlier/rn_example_send_sms](https://github.com/Merlier/rn_example_send_sms)

## Get started

Requirements:
* react-native >= 0.60

First just init a new react-native project:

```bash
$ npx react-native init rn_example_send_sms
```

Install the [react-native-sms](https://www.npmjs.com/package/react-native-sms) module:

```bash
$ npm install --save react-native-sms
```
Then add permissions in android/app/src/main/AndroidManifest.xml:

```
  <uses-permission android:name="android.permission.READ_SMS" />
  <uses-permission android:name="android.permission.WRITE_SMS" />
  <uses-permission android:name="android.permission.SEND_SMS" />
```

We will need the react-native-permissions to access SMS permission through asking the user.
So we 'll install the react-native-permissions module:

```bash
$ npm i --save react-native-permissions
```

# Send SMS

To send SMS we will code a "sendSMS" function which will call a "getSMSPermission" function to check and request SMS permissions. Then the "sendSMS" function will use the react-native-get-sms-android module to send sms throught the "autoSend" function.

You just have to modify you app.js like this:

```javascript
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * @format
 * @flow strict-local
 */

import React, {useState} from 'react';
import {
  StyleSheet,
  SafeAreaView,
  View,
  Button,
  Text,
  TextInput,
} from 'react-native';
import {check, request, RESULTS, PERMISSIONS} from 'react-native-permissions';
import SmsAndroid from 'react-native-get-sms-android';

const App: () => React$Node = () => {
  const [phoneNumber, setPhoneNumber] = useState('');
  const [message, setMessage] = useState('Happy new year!');

  const getSMSPermission = async () => {
    try {
      const checkResult = await check(PERMISSIONS.ANDROID.SEND_SMS);
      switch (checkResult) {
        case RESULTS.DENIED:
          const requestResult = await request(PERMISSIONS.ANDROID.SEND_SMS);
          return Promise.resolve(requestResult);
        case RESULTS.GRANTED:
          return Promise.resolve(checkResult);
        default:
          return Promise.reject();
      }
    } catch (err) {
      // console.log(err);
    }
  };

  const sendSMS = async () => {
    try {
      await getSMSPermission();
      SmsAndroid.autoSend(
        phoneNumber,
        message,
        (fail) => {
          console.log('Failed with this error: ' + fail);
        },
        (success) => {
          console.log('SMS sent successfully');
        },
      );
    } catch (err) {
      // console.log(err)
    }
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.form}>
        <Text style={styles.title}>Send SMS using react-native on Android</Text>
        <TextInput
          style={styles.textInput}
          placeholder={'Phone number'}
          onChangeText={setPhoneNumber}
          value={phoneNumber}
        />
        <TextInput
          style={styles.textInput}
          placeholder={'Message'}
          onChangeText={setMessage}
          value={message}
        />
        <Button onPress={sendSMS} title="Send SMS" />
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#eee',
  },
  form: {
    padding: 20,
  },
  title: {
    fontSize: 20,
    marginBottom: 20,
  },
  textInput: {
    backgroundColor: '#fff',
    marginBottom: 5,
  },
});

export default App;

```
Run the app:

```bash
$ npx react-native run-android
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hpv4m11q0cs2jig461aj.png)

Have fun
Happy new year!
:)