Skip to content

housinghq/react-native-call-detection

 
 

Repository files navigation

React Native Call Detection 🎉 🎊

npm version

This package helps to detect different call states like Incoming, Disconnected, Dialing and Connected for iOS. For android, this package will give the following states, Offhook, Incoming and Disconnected.

Installation

Add the package to your react-native project in the following way

yarn add react-native-call-detection

Link the current package to your react native project

react-native link react-native-call-detection

For Android:-

Just Verify that the following changes are present in the corresponding files

  • In MainApplication.java
+ import com.pritesh.calldetection.CallDetectionManager;
	@Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
+     new CallDetectionManager(MainApplication.this)
      );
    }
  };
  • In android/settings.gradle:
...
include ':app'
+ include ':react-native-call-detection'
+ project(':react-native-call-detection').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-call-detection/android')
  • In android/app/build.gradle:
dependencies {
    ...
    compile "com.facebook.react:react-native:+"  // From node_modules
+   compile project(':react-native-call-detection')
}

Usage

There are different hooks that you may get depending on the platform. So if

Its really easy to setup the package. Have a look at the following code snippet

import CallDetectorManager from 'react-native-call-detection'

startListenerTapped() {
	this.callDetector = new CallDetectorManager((event)=> {
	// For iOS event will be either "Connected",
	// "Disconnected","Dialing" and "Incoming"
	
	// For Android event will be either "Offhook",
	// "Disconnected" and "Incoming"
	

	if (event === 'Disconnected') {
	// Do something call got disconnected
	} 
	else if (event === 'Connected') {
	// Do something call got connected
	// This clause will only be executed for iOS
	} 
	else if (event === 'Incoming') {
	// Do something call got incoming
	}
	else if (event === 'Dialing') {
	// Do something call got dialing
	// This clause will only be executed for iOS
	} 
	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.
	// This clause will only be executed for Android
	} 
})
}

stopListenerTapped() {
	this.callDetector && this.callDetector.dispose();
}

Dont forget to call dispose when you don't intend to use call detection, as it will avoid memory leakages.

Example project can be used to test out the package. In the example project update the HomeComponent.js with the phone number to call

  callFriendTapped() {
  // Add the telephone num to call
    Linking.openURL('tel:5555555555')
      .catch(err => {
        console.log(err)
      });
  }

For any problems and doubt raise an issue.

Caveat

Since For android, there is no native support to detect call being disconnected, the callbackn with "Disconnected" event will be called only when the app comes in foreground.

About

react-native package to detect call states

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Makefile 59.8%
  • Java 32.2%
  • Objective-C 5.4%
  • C++ 2.0%
  • C 0.5%
  • JavaScript 0.1%