first you install the library
npm i react-native-read-sms
Then go to project in android studio and paste these lines in 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" />
startReadSMS = async () => { const hasPermission = await ReadSms.requestReadSMSPermission(); if(hasPermission) { ReadSms.startReadSMS((status, sms, error) => { if (status == "success") { console.log("Great!! you have received new sms:", sms); } }); } else { console.log("hi"); } }
and then call this function on a onpress method of a button or simply call this function do that it will show a pop of allowing the permission
Method 2
import React from 'react'; import { View, StyleSheet, Text, PermissionsAndroid, Button } from 'react-native'; import * as ReadSms from 'react-native-read-sms/ReadSms'; // camera permission for react native const requestCameraPermission = async () => { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.READ_SMS, { title: 'Camera Permission', message: 'App needs camera permission', buttonNeutral: "Ask Me Later", buttonNegative: "Cancel", buttonPositive: "OK" }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('Camera permission given'); } else { console.log('Camera permission denied'); } } catch (err) { console.warn(err); } };
In this method weβll call the PermissionAndroid of react-native then write a function then call the function on a onpress method
<Button title="request permissions" onPress={requestCameraPermission} /> </View>
then you can use that thing
Β
Method3
npm i react-native-sms
npm i react-native-get-sms-android
Add permissions to yourΒ
android/app/src/main/AndroidManifest.xml
Β file.<uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" />
import com.react.SmsPackage; import com.tkporter.sendsms.SendSMSPackage; import android.content.Intent; @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //probably some other stuff here SendSMSPackage.getInstance().onActivityResult(requestCode, resultCode, data); }
import com.tkporter.sendsms.SendSMSPackage; import com.react.SmsPackage;