Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Sample React Native App
- * https://github.com/facebook/react-native
- *
- * @format
- * @flow
- */
- import React, { useEffect, useState } from 'react';
- import { Container, Header, Content, List, ListItem, Text, Body, Left } from 'native-base';
- import SmsAndroid from 'react-native-get-sms-android';
- import { PermissionsAndroid, Alert } from 'react-native';
- const App: () => React$Node = () => {
- const [smsListState, setSMSList] = useState([]);
- const mySMSList = []
- async function fetchSMS() {
- try {
- let granted = await PermissionsAndroid.request(
- PermissionsAndroid.PERMISSIONS.READ_SMS, {
- title: 'Read SMS',
- message: 'Need access to read sms',
- },
- );
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
- console.log('READ_SMS permissions granted', granted);
- /* List SMS messages matching the filter */
- var filter = {
- box: 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all
- // the next 4 filters should NOT be used together, they are OR-ed so pick one
- // read: 1, // 0 for unread SMS, 1 for SMS already read
- // _id: 1061, // specify the msg id
- address: 'NICA_ALERT', // sender's phone number
- // body: 'Your 166##24001 has been Debited', // content to match
- // the next 2 filters can be used for pagination
- indexFrom: 0, // start from index 0
- maxCount: 100, // count of SMS to return each time
- };
- await SmsAndroid.list(
- JSON.stringify(filter),
- (fail) => {
- console.log('Failed with this error: ' + fail);
- },
- (count, smsList) => {
- var arr = JSON.parse(smsList);
- setSMSList(arr)
- },
- );
- } else {
- Alert.alert('READ_SMS permissions denied');
- console.log('READ_SMS permissions denied');
- }
- } catch (err) {
- Alert.alert(err);
- }
- }
- useEffect(() => {
- fetchSMS()
- }, [])
- let smsforRender = smsListState.map((sms, index) =>
- (<ListItem>
- <Text>{index + 1}</Text>
- <Body>
- <Text>{sms.address}</Text>
- <Text note>{sms.body}</Text>
- </Body>
- </ListItem>))
- return (
- <Container>
- <Header />
- <Content>
- <List>
- {smsforRender}
- </List>
- </Content>
- </Container>
- );
- };
- export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement