Join us in San Francisco on the 29/30th of October for two days of developer workshops and technical talks

Add Client SDK to your application

Overview

In this guide you learn how to add the Nexmo Client SDK to your client-side JavaScript app.

Prerequisites

The Nexmo Client SDK requires Node.js and NPM.

To add the Nexmo Client SDK to your project

Open your terminal. If you have an existing app, navigate to its root. Otherwise, create a new directory for your project and add a default package.json by running:

npm init -y

Install the Client SDK package

Install the Nexmo Client SDK using npm:

npm install nexmo-client -s

Import the Client SDK into your code

If your application is using ES6 module syntax, you can import the client module near the top of your application code:

import NexmoClient from 'nexmo-client';

If your application will run on a single page, you can load the module in your HTML using a script tag:

<script src="./node_modules/nexmo-client/dist/nexmoClient.js"></script>

Be sure to check that the path to nexmoClient.js is correct for your project structure.

Using the Nexmo Client SDK in your app

Creating Users and JWTs

A JSON Web Token (JWT) is necessary to log in to your Nexmo Application. The Client SDK cannot manage users nor generate JWTs, so you must choose a method of handling it on the backend:

  • For onboarding or testing purposes, you can get your client-side app working before setting up a backend by generating a test JWT from the command line and hard-coding it in your client-side JavaScript.
  • For real world usage, you can deliver JWTs from the server using the Node or PHP backend SDKs, and set the jwt variable in your code by fetching that data:

    fetch('/getJwt')
      .then(results => results.json())
      .then(data => {
        jwt = data.token;
      })
      .catch(err => console.log(err));
    
  • Read more on generating JWTs in this article

Instantiate and log in the NexmoClient

No arguments are necessary to instantiate a new NexmoClient, but you will need to pass your JWT as the argument to login().

let nexmo = new NexmoClient()
  .login(jwt)
  .then(app => console.log('Logged in to app', app))
  .catch(err => console.log(err));

Conclusion

You added the Nexmo Client SDK to your client-side JavaScript app and logged in to a NexmoClient instance, which returned an Application object. You can now use Application.newConversation() to create a conversation, and then access the functionality of a Conversation.

Overview

In this guide you learn how to add the Nexmo Client SDK to your Android app.

Prerequisites

The Nexmo Client SDK requires a minimum Android API level of 23.

To add the Nexmo Client SDK to your project

Open you Android project

Open your Android project codebase in your IDE.

Add dependencies

To add the Nexmo Client SDK to your project, add the following dependency in your app level build.gradle file (usually app/build.gradle):

dependencies {
    implementation 'com.nexmo.android:client-sdk:1.0.0'
}

Add permissions

To use the In-App Voice features, add audio permissions using the following procedure:

  1. On your AndroidManifest.xml add the required permissions:

    <manifest ...>
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    
        <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    
        <application>
    
        </>
    </>
    
  2. For devices running Android version M (API level 23) or higher, you should add a request for the permissions required:

    android.Manifest.permission.READ_PHONE_STATE,
    
    android.Manifest.permission.RECORD_AUDIO,
    
    android.Manifest.permission.PROCESS_OUTGOING_CALLS
    

    Read more about requesting runtime permissions on Android here

Using NexmoClient in your App

Building NexmoClient

Make sure to build the NexmoClient instance before using it. The default build being:

NexmoClient.Builder().build(context)

Setting NexmoConnectionListener

Set NexmoConnectionListener that will notify you on any changes on the connection to the SDK and the availability of its functionality:

NexmoClient.get().setConnectionListener(new NexmoConnectionListener() {
    @Override
    public void onConnectionStatusChange(ConnectionStatus status, ConnectionStatusReason reason) {
        //...
        }
    });

Login NexmoClient

After initializing NexmoClient, you need log in to it, using a jwt user token. This is described in the topic on JWTs and ACLs.

Replace the token so as to authenticate the relevant user:

    NexmoClient.get().login(token, requestListener)

After the login succeeds, the logged in user is available via NexmoClient.get().getUser().

Conclusion

You added the Nexmo Client SDK to your Android app, initialized it, and logged in to a NexmoClient instance.

You can now use NexmoClient.get() in your app, and then use additional NexmoClient functionality.

Overview

In this guide you learn how to add the Nexmo Client SDK to your iOS app.

Prerequisites

To use the Nexmo SDK for iOS, you need to have the following installed:

  • Xcode 10 or later
  • iOS 10 or later

Add the SDK to your iOS Project

Open Xcode with your iOS project.

You can either install the Nexmo Client SDK directly, or via CocoaPods.

CocoaPods

  1. Open your project's PodFile. If you don't have one already, open a terminal and run the following commands:

    $ cd 'Project Dir'
    $ pod init
    

    Where Project Dir is the path to the parent directory of the PodFile.

  2. Under your target add the NexmoClient pod. Replace TargetName with your actual target name.

    target 'TargetName' do
        pod 'NexmoClient'
    end
    

    Make sure the pod file has the public CocoaPod specs repository source.

  3. Install the Pod by opening a terminal and running the following command:

    $ cd 'Project Dir'
    $ pod update
    

    Where Project Dir is the path to the parent directory of the PodFile.

  4. Open the xcworkspace with Xcode and disable bitcode for your target.

  5. In your code, import the NexmoClient library:

    Swift:

    import NexmoClient  
    

    Objective-C:

    #import <NexmoClient/NexmoClient.h>;
    

Frameworks

  1. Download the Nexmo Client SDK and add it to your project.

  2. Open the xcworkspace with Xcode and disable bitcode for your target.

  3. In your code, import the NexmoClient library:

    Swift:

    import NexmoClient  
    

    Objective-C:

    #import <NexmoClient/NexmoClient.h>;
    

Add permissions

To use the in-app voice features, you need to add audio permissions:

  1. In your Info.plist add a new row with 'Privacy - Microphone Usage Description' and a description for using the microphone. For example, Audio Calls.

  2. In your code add a request for Audio Permissions:

    Swift

    import AVFoundation
    
    func askAudioPermissions() {
        AVAudioSession.sharedInstance().requestRecordPermission { (granted:Bool) in
            NSLog("Allow microphone use. Response: %d", granted)
        }
    }
    

    Objective-C:

    #import <AVFoundation/AVAudioSession.h>
    
    - (void)askAudioPermissions {
        if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)])
        {
            [[AVAudioSession sharedInstance] requestRecordPermission: ^ (BOOL granted)
            {
            NSLog(@"Allow microphone use. Response: %d", granted);
            }];
        }
    }
    

AppDelegate is the best place to do this.

Using NXMClient in your app

Login

  1. Create a NXMClient object and login with a jwt user token. If necessary, you can read more about generating the JWT.

    Swift:

    let client = NXMClient(token: "your token")
    client?.setDelegate(self)
    client?.login()
    

    Objective-C:

    NXMClient *client = [[NXMClient alloc] initWithToken:@"your token"];
    [client setDelegate:self];
    [client login];
    

    Note that self should implement the NXMClientDelegate protocol.

  2. On a successful login, the following delegate method is called with NXMConnectionStatusConnected:

    Swift:

    func connectionStatusChanged(_ status: NXMConnectionStatus, reason: NXMConnectionStatusReason)
    

    Objective-C:

    - (void)connectionStatusChanged:(NXMConnectionStatus)status reason:(NXMConnectionStatusReason)reason;
    

Get current user info

After the login succeeds, the logged in user will be available via:

Swift:

let user = client.user

Objective-C:

NXMUser *user = client.user;

Conclusion

You added the Nexmo Client SDK to your iOS app, and logged in to a NXMClient instance. You can now use the NXMClient client in your app, and use the Nexmo Client SDK functionality.