briankabiro / react-native-get-sms-android

React Native module to get messages on an Android device
MIT License
135 stars 68 forks source link

Regex format? #81

Open mushroomgenie opened 3 years ago

mushroomgenie commented 3 years ago

Is there any particular format in which we must write the regex expression? Because whenever I put bodyRegex, I get an empty array as response.

rajanverma-me commented 2 years ago

any update on this one? facing the same issue

ALIAHSANRST commented 1 year ago

same issue

ALIAHSANRST commented 1 year ago

Hi i done some research and found out javaScript has Regular Expressions Which are basically if this Regex format is not working for you. I have made a custom fucntion use Regular Expressions I needed Regex to get sms which contains city , area and description change according to your needs

`const LocalSms = () => { const [SmsCount, setSmsCount] = useState(); const [SmsBody, setSmsBody] = useState([]);

const [sms, setSms] = useState([]); const [loading, setLoading] = useState(true);

useEffect(() => { const fetch = async () => { await requestSmsPermission(); read(); }; fetch(); }, []);

useFocusEffect( React.useCallback(() => { const fetch = async () => { await requestSmsPermission(); read(); }; fetch(); return () => fetch(); }, [read]), );

async function requestSmsPermission() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.READ_SMS, { title: 'SMS Permission', message: 'This app needs access to your SMS messages to function properly.', buttonNegative: 'Cancel', buttonPositive: 'OK', }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('SMS permission granted'); } else { console.log('SMS permission denied'); } } catch (err) { console.warn(err); } }

const filter = { box: 'inbox', maxCount: 10, };

const read = () => { console.log('reading for read()'); SmsAndroid.list( JSON.stringify(filter), fail => console.log('Failed with error:', fail), (count, smsList) => { const parsedData = JSON.parse(smsList); console.log('parsedData', parsedData); validate(parsedData); setSmsCount(count); setLoading(false); }, ); };

function validate(body) { const regex = new RegExp('(city)|(area)|(description)'); const tempSms = []; body.forEach(bodyData => { if (regex.test(bodyData.body)) { console.log('bodyData.body=', bodyData.body); tempSms.push(bodyData.body); } setSmsBody(tempSms); }); }

return (

{!loading ? ( { return ( {item} ); }} /> ) : ( Fetching Sms )}

); };`