React Native Send sms within the app
Asked Answered
A

4

5

I want to send sms to multiple numbers without opening to default messaging app. I try to use react-native-sms-x but its not maintained and my project just stuck at compiling. Also I used react-native-sms but it open default Messaging App filled with one user number and message body and had to click send button of it too.

Article answered 27/6, 2018 at 18:22 Comment(1)
do you have a answer for this? I'm looking for the same function as well, thank you!Fujio
M
7
import { Linking,Platform  } from "react-native";

    const url = (Platform.OS === 'android')
    ? 'sms:919999999999?body=your message'
    : 'sms:919999999999'
    Linking.canOpenURL(url).then(supported => {
      if (!supported) {
        console.log('Unsupported url: ' + url)
      } else {
        return Linking.openURL(url)
      }
    }).catch(err => console.error('An error occurred', err))
Marcela answered 31/8, 2018 at 7:22 Comment(1)
I think it will open default sms app filled with given sms number and message.Article
T
5

After a lot of research and trials in the react app... I have found this library working fine and reached the goals to send a message without going into the default message environment.

var phoneNumbers = {
        "addressList": ["+911212121212", "+911212121212"]
      };
    var message = "This is automated test message"
    SmsAndroid.autoSend(
        phoneNumbers,
        message,
        (fail) => {
            console.log('Failed with this error: ' + fail);
        },
        (success) => {
            console.log('SMS sent successfully');
        },
    );

I hope it helps you. Do not forget to upvote

Tindle answered 26/5, 2020 at 7:10 Comment(1)
This is the best library I've ever worked, just to ask your experience is it helped to you ?Tindle
A
1

From Now Just For Android I use react-native-sms-android Here is my Code for Sending sms to multiple users:

import Asms from "react-native-sms-android";
type Props = {};
export default class App extends Component<Props> {
  constructor(Props) {
        super(Props);
        this.state = { FileNumbers: ['687867867867','8575774433'], Message: 
          "gjjgjgj" };
         }

  sendingSms = (Receivers, Messagex) => {
           try {
                Receivers.map(
                async Numbers =>
                  await Asms.sms(Numbers, Messagex, "sendDirect", (err,message) 
                    => {
                    if (err) {
                      console.log(err);
                    } else {
                      console.log(message);
                    }
                  })
              );
            } catch (e) {
              alert("" + e);
            }
          };

      render() {
        return (
          <View style={styles.container}>
            <TextInput
              style={{
                height: 40,
                borderColor: "gray",
                borderWidth: 1,
                width: "90%"
              }}
              onChangeText={Message => this.setState({ Message })}
              value={this.state.Message}
            />
            <Button
              title="SEND"
              onPress={() =>
                this.sendingSms(this.state.FileNumbers, this.state.Message)
              }
            />

          </View>
        );
      }
    }
Article answered 28/6, 2018 at 16:53 Comment(0)
I
0

In my case, I want to send the SMS through the app with native without any NPM library

My react native version is 0.73.4 and I'm using kotlin

under

android/app/src/main/java/com/your_project_name

Create a folder

  1. smsSend (folder name)

  2. create two file

a. SmsModule.kt

b. SmsModulePackage.kt

Under android/app/src/main/java/com/your_project_name/smsSend/SmsModule.kt

package com.your_project_name

import android.telephony.SmsManager
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod

class SmsModule(reactContext: ReactApplicationContext) : 
ReactContextBaseJavaModule(reactContext)      
    {

override fun getName(): String {
    return "SmsModule"
}

@ReactMethod
 fun sendSms(message: String, recipient: String, promise: Promise) {
     try {
         val smsManager = SmsManager.getDefault()
          smsManager.sendTextMessage(recipient, null, message, null, null)
         promise. Resolve("SMS send successful")
    } catch (e: Exception) {
        promise.reject("SMS_ERROR", e.message)
    }
 }
}

under android/app/src/main/java/com/onestack/smsSend/SmsModulePackage.kt

package com.onestack

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager

class SmsModulePackage : ReactPackage {
 override fun createNativeModules(reactContext: ReactApplicationContext): 
 List<NativeModule> {
    return listOf(SmsModule(reactContext))
} 

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
    return emptyList()
}
}

app.js

const {SmsModule} = NativeModules;
-----------
const smsSend = async () => {
try {
  const granted = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.SEND_SMS,
    {
      title: 'SMS Permission',
      message: 'This app needs access to send SMS messages.',
      buttonPositive: 'OK',
    },
  );
  if (granted === PermissionsAndroid.RESULTS.GRANTED) {
    console.log('SEND_SMS permission granted');
    try {
      const response = await SmsModule.sendSms(
        'type your SMS here',
        'recipient contact number here',
      );
      console.log('SMS sent:', response);
    } catch (error) {
      console.error('Error sending SMS:', error);
    }
  } else {
    console.log('SEND_SMS permission denied');
  }
} catch (err) {
  console. Warn(err);
}
};

Please subscribe my YouTube Page

Impractical answered 2/5, 2024 at 5:42 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.