priteshrnandgaonkar / react-native-call-detection

react-native package to detect call states
MIT License
191 stars 110 forks source link

phone number is not getting #118

Open FebinPrimeCrown opened 6 months ago

FebinPrimeCrown commented 6 months ago

import React, { useEffect, useState } from 'react'; import { PermissionsAndroid, StyleSheet, Text, TouchableHighlight, View } from 'react-native'; import CallDetectorManager from 'react-native-call-detection';

const App = () => { const [featureOn, setFeatureOn] = useState(false); const [incoming, setIncoming] = useState(false); const [number, setNumber] = useState(null); const [callLog, setCallLog] = useState(null); // New state to hold call log messages

useEffect(() => { askPermission(); }, []);

const askPermission = async () => { try { const permissions = await PermissionsAndroid.requestMultiple([ PermissionsAndroid.PERMISSIONS.READ_CALL_LOG, PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE, ]); console.log('Permissions are:', permissions); } catch (err) { console.warn(err); } };

const startListenerTapped = () => { setFeatureOn(true); callDetector = new CallDetectorManager( (event, incomingNumber) => { console.log(event, incomingNumber); if (event === 'Disconnected') { // Do something call got disconnected setIncoming(false); setNumber(null); setCallLog(Call disconnected); } else if (event === 'Incoming') { // Do something call got incoming setIncoming(true); setNumber(incomingNumber || 'Unknown'); // Use 'Unknown' if incomingNumber is empty setCallLog(Incoming call from ${incomingNumber || 'Unknown'}); } else if (event === 'Offhook') { // Device call state: Off-hook. // At least one call exists that is dialing, // active, or on hold, // and no calls are ringing or waiting. setIncoming(true); setNumber(incomingNumber || 'Unknown'); // Use 'Unknown' if incomingNumber is empty console.log(Call picked from ${incomingNumber}); setCallLog(Call picked from ${incomingNumber || 'Unknown'}); // Clear the "Incoming call" message once call is picked setIncoming(false); } else if (event === 'Missed') { // Do something call got missed if (incomingNumber === number) { console.log(Missed call from ${incomingNumber}); setCallLog(Missed call from ${incomingNumber || 'Unknown'}); // Clear the "Incoming call" message once call is missed setIncoming(false); } } else if (event === 'Declined') { // Do something call got declined setIncoming(false); setNumber(incomingNumber || 'Unknown'); // Use 'Unknown' if incomingNumber is empty console.log(Call declined from ${incomingNumber}); setCallLog(Call declined from ${incomingNumber || 'Unknown'}); } }, true, () => {}, { title: 'Phone State Permission', message: 'This app needs access to your phone state in order to react and/or to adapt to incoming calls.', }, ); };

const stopListenerTapped = () => { callDetector && callDetector.dispose(); setFeatureOn(false); setIncoming(false); setCallLog(null); // Clear call log when stopping listener };

return (

Call Detection Service {featureOn ? `ON` : `OFF`}
  </TouchableHighlight>
  {callLog && (
    <Text style={{ fontSize: 16, color: 'black', marginTop: 10 }}>
      {callLog}
    </Text>
  )}
  {incoming && (
    <Text style={{ fontSize: 20, color: 'red', marginTop: 20 }}>
      {number ? `Incoming call from ${number}` : `Incoming call`}
    </Text>
  )}
</View>

); };

export default App;

const styles = StyleSheet.create({ body: { backgroundColor: '#fff', justifyContent: 'center', alignItems: 'center', flex: 1, }, text: { padding: 20, fontSize: 20, fontWeight: '700', color: '#fff', }, });