react-native-twilio-voice
This is a React Native wrapper for Twilio Programmable Voice SDK that lets you make and receive calls from your ReactNatvie App. This module is not curated nor maintained, but inspired by Twilio.
Twilio Programmable Voice SDK
- Android 2.0.0-beta15 (bundled within this library)
- iOS 2.0.0-beta13 (specified by the app's own podfile)
Installation
Before starting, we recommend you get familiar with Twilio Programmable Voice SDK.
It's easier to integrate this module into your react-native app if you follow the Quick start tutorial from Twilio, because it makes very clear which setup steps are required.
npm install react-native-twilio-voice --save
react-native link react-native-twilio-voice
iOS Installation
After you have linked the library with react-native link react-native-twilio-voice
check that libRNTwilioVoice.a is present under YOUR_TARGET > Build Phases > Link Binaries With Libraries. If it is not present you can add it using the + sign at the bottom of that list.
Edit your Podfile to include TwilioVoice framework
source 'https://github.com/cocoapods/specs'
source 'https://github.com/twilio/cocoapod-specs'
# min version for TwilioVoice to work
platform :ios, '8.1'
target <YOUR_TARGET> do
...
pod 'TwilioVoice', '=2.0.0-beta13'
...
end
run pod install from inside your project ios directory
CallKit
The current iOS part of this library works through CallKit. Because of this the call flow is much simpler than on Android as CallKit handles the inbound calls answering, ignoring, or rejecting.
Because of CallKit, the only event listeners present are "deviceReady", "connectionDidConnect", "connectionDidDisconnect", and "callRejected".
VoIP Service Certificate
Twilio Programmable Voice for iOS utilizes Apple's VoIP Services and VoIP "Push Notifications" instead of FCM. You will need a VoIP Service Certificate from Apple to receive calls.
Android Installation
Setup FCM
You must download the file google-services.json from the Firebase console.
It contains keys and settings for all your applications under Firebase. This library obtains the resource senderID for registering for remote GCM from that file.
NOTE: To use a specific play-service-gcm version, update the compile instruction in your App's android/app/build.gradle (replace 10.2.0 with the version you prefer):
...
buildscript {
...
dependencies {
classpath 'com.google.gms:google-services:3.0.0'
}
}
...
dependencies {
...
compile project(':react-native-twilio-voice')
compile ('com.google.android.gms:play-services-gcm:10.2.0') {
force = true;
}
}
// this plugin looks for google-services.json in your project
apply plugin: 'com.google.gms.google-services'
In your AndroidManifest.xml
.....
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<application ....>
....
<service
android:name="com.hoxfon.react.RNTwilioVoice.fcm.VoiceFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="com.hoxfon.react.RNTwilioVoice.fcm.VoiceFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
.....
In android/settings.gradle
...
include ':react-native-twilio-voice'
project(':react-native-twilio-voice').projectDir = file('../node_modules/react-native-twilio-voice/android')
Register module (in MainApplication.java)
import com.hoxfon.react.RNTwilioVoice.TwilioVoicePackage;
public class MainApplication extends Application implements ReactApplication {
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new TwilioVoicePackage()
);
}
};
....
}
Usage
import TwilioVoice from 'react-native-twilio-voice'
...
async function initTelephony() {
try {
const accessToken = await getAccessTokenFromServer()
const success = await TwilioVoice.initWithToken(accessToken)
} catch (err) {
console.err(err)
}
}
function initTelephonyWithUrl(url) {
TwilioVoice.initWithTokenUrl(url)
try {
TwilioVoice.configureCallKit({
appName: 'TwilioVoiceExample',
imageName: 'my_image_name_in_bundle',
ringtoneSound: 'my_ringtone_sound_filename_in_bundle'
})
} catch (err) {
console.err(err)
}
}
TwilioVoice.addEventListener('deviceReady', deviceReadyHandler)
TwilioVoice.addEventListener('deviceNotReady', deviceNotReadyHandler)
TwilioVoice.addEventListener('deviceDidReceiveIncoming', deviceDidReceiveIncomingHandler)
TwilioVoice.addEventListener('connectionDidConnect', connectionDidConnectHandler)
TwilioVoice.addEventListener('connectionDidDisconnect', connectionDidDisconnectHandler)
TwilioVoice.addEventListener('callRejected', callRejected)
...
TwilioVoice.connect({To: '+61234567890'})
TwilioVoice.disconnect()
TwilioVoice.accept()
TwilioVoice.reject()
TwilioVoice.ignore()
TwilioVoice.setMuted(mutedValue)
TwilioVoice.sendDigits(digits)
TwilioVoice.getActiveCall()
.then(incomingCall => {
if (incomingCall){
_deviceDidReceiveIncoming(incomingCall)
}
})
Credits
voice-quickstart-android
react-native-push-notification
voice-callkit-quickstart-objc
License
MIT