Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8.  
  9. import React, { useEffect, useState } from 'react';
  10. import { Container, Header, Content, List, ListItem, Text, Body, Left } from 'native-base';
  11.  
  12. import SmsAndroid from 'react-native-get-sms-android';
  13. import { PermissionsAndroid, Alert } from 'react-native';
  14.  
  15. const App: () => React$Node = () => {
  16.  
  17. const [smsListState, setSMSList] = useState([]);
  18. const mySMSList = []
  19.  
  20. async function fetchSMS() {
  21. try {
  22. let granted = await PermissionsAndroid.request(
  23. PermissionsAndroid.PERMISSIONS.READ_SMS, {
  24. title: 'Read SMS',
  25. message: 'Need access to read sms',
  26. },
  27. );
  28. if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  29. console.log('READ_SMS permissions granted', granted);
  30.  
  31. /* List SMS messages matching the filter */
  32. var filter = {
  33. box: 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all
  34. // the next 4 filters should NOT be used together, they are OR-ed so pick one
  35. // read: 1, // 0 for unread SMS, 1 for SMS already read
  36. // _id: 1061, // specify the msg id
  37. address: 'NICA_ALERT', // sender's phone number
  38. // body: 'Your 166##24001 has been Debited', // content to match
  39. // the next 2 filters can be used for pagination
  40. indexFrom: 0, // start from index 0
  41. maxCount: 100, // count of SMS to return each time
  42. };
  43.  
  44. await SmsAndroid.list(
  45. JSON.stringify(filter),
  46. (fail) => {
  47. console.log('Failed with this error: ' + fail);
  48. },
  49. (count, smsList) => {
  50. var arr = JSON.parse(smsList);
  51. setSMSList(arr)
  52. },
  53. );
  54. } else {
  55. Alert.alert('READ_SMS permissions denied');
  56. console.log('READ_SMS permissions denied');
  57. }
  58. } catch (err) {
  59. Alert.alert(err);
  60. }
  61. }
  62.  
  63. useEffect(() => {
  64. fetchSMS()
  65. }, [])
  66.  
  67. let smsforRender = smsListState.map((sms, index) =>
  68. (<ListItem>
  69. <Text>{index + 1}</Text>
  70. <Body>
  71. <Text>{sms.address}</Text>
  72. <Text note>{sms.body}</Text>
  73. </Body>
  74. </ListItem>))
  75.  
  76. return (
  77. <Container>
  78. <Header />
  79. <Content>
  80. <List>
  81. {smsforRender}
  82. </List>
  83. </Content>
  84. </Container>
  85. );
  86. };
  87.  
  88. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement