0.14.0 • Published 5 years ago

nativescript-twilio v0.14.0

Weekly downloads
1
License
Apache-2.0
Repository
-
Last release
5 years ago

NativeScript Twilio

nativescript-twilio is a plugin that exposes the Twilio Voice SDK, the leading platform for Voice solutions.

Note: For now it only for making outbound calls, both for Android and iOS

Prerequisites / Requirements

Plugin installation on your Nativescript app

tns plugin add nativescript-twilio

Usage

Demo App

Setup

Running the Demo app

  • Clone the repo, cd src, and npm run demo.android or npm run demo.ios.

Integrating into your NativeScript app

  • On the main.ts or app.ts file, put this code in order to init Twilio:
  import * as application from 'tns-core-modules/application';
  import { initTwilio } from 'nativescript-twilio';
  import { TwilioAppDelegate } from 'nativescript-twilio/delegate';

  // The following endpoint should return the raw token in the request body
  const accessTokenUrl = 'http://yourserver/path/to/access-token';
  const accessTokenHeaders = {'Authorization': 'Token sometoken'};

  initTwilio(accessTokenUrl, accessTokenHeaders);

  if (application.ios) {
    // register twilio app delegate in order to receive incoming calls
    application.ios.delegate = TwilioAppDelegate;
  }
  • In some place in your code (i.e. in some UI component loaded event) you need to setUp the call listener, which will handle the call's connection events:
  import { setupCallListener, setupPushListener } from 'nativescript-twilio';

  // listener for inbound/outbound calls
  const callListener = {
    onConnectFailure(call, error) {
      dialogs.alert(`connection failure: ${error}`);
    },
    onConnected (call) {
      dialogs.alert('call connected');
    },
    onDisconnected (call) {
      dialogs.alert('disconnected');
    }
  };

  setupCallListener(callListener);

  // listener for push notifications (incoming calls)
  const pushListener = {
    onPushRegistered(accessToken, deviceToken) {
      dialogs.alert('push registration succeded');
    },
    onPushRegisterFailure (error) {
      dialogs.alert(`push registration failed: ${error}`);
    }
  };

  setupPushListener(pushListener);
  • On the component for making outbound calls, put the following code:
  import * as dialogs from 'tns-core-modules/ui/dialogs';
  import { getAccessToken, Twilio } from 'nativescript-twilio';

  const phoneNumber = '+1555365432';

  getAccessToken() // it will use the Twilio configuration set before
    .then((token) => {
      const twilio = new Twilio(token);

      const call = twilio.makeCall(phoneNumber);

      // example of muting the call after 10 seconds
      setTimeout(() => {
        console.log('Muting call after 10 seconds...');
        call.mute(true);
      }, 10000);

      // example of disconnecting the call after 30 seconds
      setTimeout(() => {
        console.log('Disconnecting call after 30 seconds...');
        call.disconnect();
      }, 30000);

    })

API

Functions

FunctionDescription
initTwilio(url: string, headers?: any)Initialize Twilio passing the endpoint to the access token backend
getAccessToken(): Promise<string>Ask the backend for an access token. Returns a Promise with the token retrieved
setupCallListener(listener: any)Setup the call listener, passing an object with onConnectFailure, onConnected and onDisconnected callbacks
setupPushListener(listener: any)Setup the push notifications listener, passing an object with onPushRegistered and onPushRegisterFailure callbacks
unregisterPushNotifications(token: string, deviceToken: string, callback?: (error: any) => void)Unregister push notifications (incoming calls)

Twilio Methods

MethodDescription
makeCall(senderPhoneNumber: any, phoneNumber: any, options?: any): CallMake an outbound call.
toggleAudioOutput(toSpeaker: boolean)iOS Only Set the audio session output to the speaker or not.

Call Methods

MethodDescription
mute(value: boolean)Mute the call.
disconnect()Hang-up the call.

License

Apache License Version 2.0, April 2018

0.14.0

5 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago