react-native-get-sms-android
react-native-get-sms-android copied to clipboard
Regex format?
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.
any update on this one? facing the same issue
same issue
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 ( <View> {!loading ? ( <FlatList showsVerticalScrollIndicator style={styles.list} data={SmsBody} renderItem={({item, index}) => { return ( <View key={item?._id}> <Text style={styles.listItems}>{item}</Text> </View> ); }} /> ) : ( <View style={styles.loaderParent}> <ActivityIndicator size={200} color={'#116A7B'} /> <Text>Fetching Sms</Text> </View> )} </View> ); };`