react-native-incoming-call-detector
v1.2.3
Published
Detect incoming phone calls in React Native applications
Maintainers
Readme
📞 react-native-incoming-call-detector
A lightweight React Native library to detect incoming phone calls in your apps.
Easily listen for call state changes (RINGING, OFFHOOK, IDLE) and get the incoming phone number.
Installation
yarn add react-native-incoming-call-detectorOr
npm install react-native-incoming-call-detector⚙️ Android Setup
Add the following permission inside your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />Usage
import React, { useEffect } from 'react';
import { Alert, Platform, PermissionsAndroid } from 'react-native';
import {
startIncomingCallListener,
stopIncomingCallListener,
} from 'react-native-incoming-call-detector';
export default function App() {
useEffect(() => {
let subscription;
async function init() {
try {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
);
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
console.warn('📵 Phone state permission denied');
return;
}
}
subscription = await startIncomingCallListener(event => {
console.log('📞 Call Event:', event);
if (event.state === 'RINGING') {
Alert.alert('📞 Incoming Call', `Number: ${event.number || 'Unknown'}`);
} else if (event.state === 'OFFHOOK') {
console.log('✅ Call answered');
} else if (event.state === 'IDLE') {
console.log('❌ Call ended');
}
});
} catch (error) {
console.error('Error initializing call listener:', error);
}
}
init();
return () => {
if (subscription?.remove) subscription.remove();
stopIncomingCallListener();
};
}, []);
return null;
}
