# Miscellaneous

## Adding the Sinch library to React Native application

With an extra layer of [NativeModule](https://reactnative.dev/docs/native-platform),
you can embed the Sinch Android library into your React Native application.
Note that by doing this the SDK will only work on React Native apps running on
Android devices. To support other platforms you'll need to implement a
NativeModule for each platform separately (including platform-specific Sinch SDKs).

To add the Sinch library to a React Native application:

1. Copy the Sinch library `.aar` file to
`your-react-native-app/android/app/libs/` (create the `libs` folder if needed).
2. Edit `your-react-native-app/android/app/build.gradle` and add
`implementation fileTree(dir: "libs", include: ["*.aar"])` under `dependencies`.
3. To access the Sinch SDK API from React Native, follow the
[Android Native Module Guide](https://reactnative.dev/docs/turbo-native-modules-introduction).


Example `SinchModule` to start a client for a given user ID:

```java
public class SinchModule extends ReactContextBaseJavaModule implements SinchClientListener {

    static final String APP_KEY = "<YOUR_APP_KEY>";
    static final String APP_SECRET = "<YOUR_APP_SECRET>";
    static final String ENVIRONMENT = "ocra.api.sinch.com";

    private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
    private static final String TAG = SinchModule.class.getSimpleName();

    private SinchClient mSinchClient;
    private String mUserId;

    public SinchModule(@Nullable ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @NonNull
    @Override
    public String getName() {
        return "SinchModule";
    }

    @ReactMethod
    public void createClient(String userId) {
        mainThreadHandler.post(() -> {
            mUserId = userId;
            mSinchClient = SinchClient.builder()
                    .context(getReactApplicationContext())
                    .userId(userId)
                    .applicationKey(APP_KEY)
                    .environmentHost(ENVIRONMENT)
                    .build();
            mSinchClient.addSinchClientListener(this);
            mSinchClient.start();
            Log.d(TAG, "Starting SinchClient");
        });
    }

    @Override
    public void onClientStarted(SinchClient sinchClient) {
        Log.d(TAG, "onClientStarted called");
    }

    @Override
    public void onClientFailed(SinchClient sinchClient, SinchError sinchError) {
        Log.e(TAG, "onClientFailed called: " + sinchError.getMessage());
    }

    @Override
    public void onLogMessage(int i, String s, String s1) {
    }

    @Override
    public void onPushTokenRegistered() {
        Log.d(TAG, "onPushTokenRegistered called");
    }

    @Override
    public void onPushTokenRegistrationFailed(SinchError sinchError) {
        Log.e(TAG, "onPushTokenRegistrationFailed called: " + sinchError.getMessage());
    }

    @Override
    public void onCredentialsRequired(ClientRegistration clientRegistration) {
        clientRegistration.register(JWT.create(APP_KEY, APP_SECRET, mUserId));
    }

    @Override
    public void onUserRegistered() {
        Log.d(TAG, "onUserRegistered called");
    }

    @Override
    public void onUserRegistrationFailed(SinchError sinchError) {
        Log.e(TAG, "onUserRegistrationFailed called: " + sinchError.getMessage());
    }
}
```

After
[registering the module](https://reactnative.dev/docs/turbo-native-modules-introduction#4-write-your-native-platform-code)
you can call `createClient` from JavaScript:

```javascript
const { SinchModule } = NativeModules;
const MainScreen = ({ navigation }) => {
  const onPress = () => {
    SinchModule.createClient('myUserId');
  };
  return (
    <Button title="LOGIN" onPress={onPress} />
  );
};
```

Notes:

- When editing native Java files, rerun `npx react-native run-android` to test changes.
- This example is an entry point to interacting with the Sinch SDK from React Native.
For more complete use cases that handle errors and callbacks, see the `samples`
folder in the Sinch SDK archive.


## Minimum Requirements

We officially support the 3 latest major Android versions. You can try older versions but there are no guarantees it will work as expected.

### Running Sinch SDK on Android KitKat

The minimum API level on which Android Sinch SDK can run is 19 (Android KitKat). Because this version is the last one without default built-in support for TLSv1.1 and TLSv1.2 (see [Default configuration for different Android versions](https://developer.android.com/reference/javax/net/ssl/SSLSocket#default-configuration-for-different-android-versions)), which is required by the `ocra.api.sinch.com` endpoint, if you want to run the SDK on that specific API you have to patch the security provider:

```java
/**
 * Patches default security provider. This method should only be used on KitKat devicec
 * @param context
 */
public static void patchProvider(Context context){
    try {
        SSLContext.getInstance("TLSv1.2");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    try {
        ProviderInstaller.installIfNeeded(mContext.getApplicationContext());
    } catch (GooglePlayServicesRepairableException e) {
        e.printStackTrace();
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
}
```

Note:
Google Play services must be available on the device.

## Note on Sinch SDK Size

The *Sinch Android SDK aar* library includes a common Java library of approximately 900 KB, and four binaries containing the architectures *armv7*, *arm64*, *x86*, and *x86_64*, which size varies from 6.2 MB for *armv7* to 11.1 MB for *x86_64*, with the *armv64* having 10.2 MB in size. Combined and compressed in the *aar* it yields over 17 MB in size. To reduce the footprint, it's highly recommended to use the *Android App Bundle* publishing format (see [https://developer.android.com/platform/technology/app-bundle](https://developer.android.com/platform/technology/app-bundle)). In that case, only the common Java code and target architecture binary will be included in the final package.

As an example, the rough estimation of typical *arm64* distribution will have 900 KB + 10.2 MB yielding slightly over 11 MB before compression (which is hard to estimate, but can be assumed within 1x-2x range).
You can always check your app download and install size in the Google Play Console ([https://support.google.com/googleplay/android-developer/answer/9302563](https://support.google.com/googleplay/android-developer/answer/9302563))

## Restrictions on User IDs

User IDs **must not** be longer than **255** bytes, **must** only contain URL-safe characters, and be restricted to the following character set:

```text
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjiklmnopqrstuvwxyz0123456789-_=
```

If you need to use *User IDs* containing characters outside the allowed set above, you could consider *base64*-encoding the raw *User IDs* using a URL-safe base64 alphabet as described in [https://tools.ietf.org/html/rfc4648#section-5](https://tools.ietf.org/html/rfc4648#section-5)). Note how the allowed character set overlaps with the URL-safe base64 alphabet, but doesn't allow characters in the **non**-URL-safe alphabet. For example, `/` (forward slash) and `+` (plus sign).

## Encryption Export Regulations

Please check the Summary of U.S. Export Controls Applicable to Commercial Encryption Products and ensure that the application is registered for the Encryption Regulations, if applicable. It can be found under this [link](https://www.bis.doc.gov/index.php/policy-guidance/encryption).

## Local Database File

Sinch SDK needs a local database file to operate properly. It's located in the local application storage - *sinch/db/* subfolder relative to the *context.getFilesDir()*. The application **must not** delete this folder.

## Statistics

The Sinch SDK client uploads statistics to the Sinch servers at the end of a call, a call failure, or a similar event. The statistics are used for the monitoring of network status, call quality, and other aspects regarding the general quality of the service.

Some of the information **is not** anonymous and may be associated with the User ID call participants.

The statistics upload is done by the client in the background.

## Third-Party libraries and Copyright Notices

All Third Party Libraries and Copyright notices: [Sinch Terms of Service](https://www.sinch.com/terms-of-service/).