@gathertown/uikit-react-native
Version:
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
433 lines (347 loc) • 14.6 kB
Markdown
# /uikit-react-native
[](https://reactnative.dev/)
[](https://www.typescriptlang.org/)
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
> React-Native based UI kit based on sendbird javascript SDK
## Before getting started
This section shows the prerequisites you need to check to use Sendbird UIKit for React-Native.
### Requirements
- Nodejs 14 or newer
- Watchman
- JDK 11 or newer
- XCode
- Android Studio
More details, please see https://reactnative.dev/docs/environment-setup
<br/>
## Getting started
This section gives you information you need to get started with Sendbird UIKit for React-Native.
### Try the sample app
Our [sample app](/sample) has all the core features of Sendbird UIKit for React-Native.
Download the app from our GitHub repository to get an idea of what you can build with the actual UIKit before building your own project.
### Create a project
You can get started by creating a project. (highly recommended to use typescript)
```sh
npx react-native init ChatApp --template=react-native-template-typescript
```
### Install UIKit for React-Native
UIKit for React-Native can be installed through either `yarn` or `npm`
**Install dependencies**
> Note: If you are using `react-native` version `0.72` or higher, you don't need to install `/react-native-scrollview-enhancer`.
```sh
npm install /uikit-react-native \
/chat \
/react-native-scrollview-enhancer \
date-fns \
react-native-safe-area-context \
-native-community/netinfo \
-native-async-storage/async-storage
```
**Linking native modules**
```sh
npx pod-install
```
### Getting permissions
Client apps must acquire permission from users to get access to their media library and save files to their mobile storage.
Once the permission is granted, users can send images and videos to other users and save media files.
**Android**
Add the following permissions to your `android/app/src/main/AndroidManifest.xml` file.
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.your.app">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
</manifest>
```
**iOS**
Add the following permission usage descriptions to your `info.plist` file.
| Key | Type | Value |
| --------------------------------------------------- | ------ | --------------------------------------------------------------- |
| Privacy - Camera Usage Description | string | $(PRODUCT_NAME) would like to use your camera |
| Privacy - Media Library Usage Description | string | $(PRODUCT_NAME) would like access to your photo library |
| Privacy - Photo Library Usage Description | string | $(PRODUCT_NAME) would like access to your photo library |
| Privacy - Photo Library Additions Usage Description | string | $(PRODUCT_NAME) would like to save photos to your photo library |
> **NOTE**: If you use [react-native-permissions](https://github.com/zoontek/react-native-permissions#ios), you must update permissions and run `pod install`
<br/>
## Implementation guide
### Install a native modules
In order to use the APIs and features of the native module in the UIKit for React Native, you need to implement the platform service interfaces that Sendbird provides.
After implementing all the interfaces, you can set them as props of `SendbirdUIKitContainer` to use the native module features across Sendbird UIKit for React-Native.
```tsx
const App = () => {
return (
<SendbirdUIKitContainer
appId={'APP_ID'}
chatOptions={{
localCacheStorage: AsyncStorage,
}}
platformServices={{
file: FileService,
notification: NotificationService,
clipboard: ClipboardService,
media: MediaService,
}}
>
{/* ... */}
</SendbirdUIKitContainer>
);
};
```
In order to implement the interfaces to your React Native app more easily, we provide various helper functions for each interface.
> **NOTE**: Helper function is not required! You can implement it with native modules you're using.
> More details about PlatformService interfaces, please see [here](https://sendbird.com/docs/uikit/v3/react-native/core-components/provider/platformserviceprovider)
**Using React Native CLI**
You can use `createNativeClipboardService`, `createNativeNotificationService`, `createNativeFileService` and `createNativeMediaService` helper functions with below native modules.
```sh
npm install react-native-video \
react-native-permissions \
react-native-file-access \
react-native-image-picker \
react-native-document-picker \
react-native-create-thumbnail \
-native-clipboard/clipboard \
-native-camera-roll/camera-roll \
-native-firebase/app \
-native-firebase/messaging \
.tech/react-native-image-resizer
npx pod-install
```
```ts
import * as ImageResizer from ' .tech/react-native-image-resizer';
import { CameraRoll } from '-native-camera-roll/camera-roll';
import Clipboard from '-native-clipboard/clipboard';
import RNFBMessaging from '-native-firebase/messaging';
import * as CreateThumbnail from 'react-native-create-thumbnail';
import * as DocumentPicker from 'react-native-document-picker';
import * as FileAccess from 'react-native-file-access';
import * as ImagePicker from 'react-native-image-picker';
import * as Permissions from 'react-native-permissions';
import Video from 'react-native-video';
const NativeClipboardService = createNativeClipboardService(Clipboard);
const NativeNotificationService = createNativeNotificationService({
messagingModule: RNFBMessaging,
permissionModule: Permissions,
});
const NativeFileService = createNativeFileService({
fsModule: FileAccess,
permissionModule: Permissions,
imagePickerModule: ImagePicker,
mediaLibraryModule: CameraRoll,
documentPickerModule: DocumentPicker,
});
const NativeMediaService = createNativeMediaService({
VideoComponent: Video,
thumbnailModule: CreateThumbnail,
imageResizerModule: ImageResizer,
});
```
**Using Expo CLI**
You can use `createExpoClipboardService`, `createExpoNotificationService`, `createExpoFileService` and `createExpoMediaService` helper functions with below expo modules.
```sh
expo install expo-image-picker \
expo-document-picker \
expo-media-library \
expo-file-system \
expo-clipboard \
expo-notifications \
expo-av \
expo-video-thumbnails \
expo-image-manipulator
```
```ts
import * as ExpoAV from 'expo-av';
import * as ExpoClipboard from 'expo-clipboard';
import * as ExpoDocumentPicker from 'expo-document-picker';
import * as ExpoFS from 'expo-file-system';
import * as ExpoImageManipulator from 'expo-image-manipulator';
import * as ExpoImagePicker from 'expo-image-picker';
import * as ExpoMediaLibrary from 'expo-media-library';
import * as ExpoNotifications from 'expo-notifications';
import * as ExpoVideoThumbnail from 'expo-video-thumbnails';
const ExpoNotificationService = createExpoNotificationService(ExpoNotifications);
const ExpoClipboardService = createExpoClipboardService(ExpoClipboard);
const ExpoFileService = createExpoFileService({
fsModule: ExpoFS,
imagePickerModule: ExpoImagePicker,
mediaLibraryModule: ExpoMediaLibrary,
documentPickerModule: ExpoDocumentPicker,
});
const ExpoMediaService = createExpoMediaService({
avModule: ExpoAV,
thumbnailModule: ExpoVideoThumbnail,
imageManipulator: ExpoImageManipulator,
fsModule: ExpoFS,
});
```
### Local caching (required)
You can implement Local caching easily.
```shell
npm i -native-async-storage/async-storage
npx pod-isntall
```
```tsx
import AsyncStorage from '-native-async-storage/async-storage';
import { SendbirdUIKitContainer } from '/uikit-react-native';
const App = () => {
return <SendbirdUIKitContainer chatOptions={{ localCacheStorage: AsyncStorage }}>{/* ... */}</SendbirdUIKitContainer>;
};
```
Or you can use storage you are using instead of `AsyncStorage` (e.g. [`react-native-mmkv`](https://github.com/mrousavy/react-native-mmkv))
```tsx
import { MMKV } from 'react-native-mmkv';
import { LocalCacheStorage, SendbirdUIKitContainer } from '/uikit-react-native';
const mmkvStorage = new MMKV();
const localCacheStorage: LocalCacheStorage = {
async getAllKeys() {
return mmkvStorage.getAllKeys();
},
async setItem(key: string, value: string) {
return mmkvStorage.set(key, value);
},
async getItem(key: string) {
return mmkvStorage.getString(key) ?? null;
},
async removeItem(key: string) {
return mmkvStorage.delete(key);
},
};
const App = () => {
return <SendbirdUIKitContainer chatOptions={{ localCacheStorage }}>{/* ... */}</SendbirdUIKitContainer>;
};
```
### Integration with navigation library
Now you can create a screen and integrate it with a navigation library like [`react-navigation`](https://reactnavigation.org/).
See more details on [here](https://st.sendbird.com/docs/uikit/v3/react-native/introduction/screen-navigation)
The example below shows how to integrate using `react-navigation`.
**Create a fragments and screens**
```tsx
import { useNavigation, useRoute } from '-navigation/native';
import { useGroupChannel } from '/uikit-chat-hooks';
import {
createGroupChannelCreateFragment,
createGroupChannelFragment,
createGroupChannelListFragment,
} from '/uikit-react-native';
const GroupChannelListFragment = createGroupChannelListFragment();
const GroupChannelCreateFragment = createGroupChannelCreateFragment();
const GroupChannelFragment = createGroupChannelFragment();
const GroupChannelListScreen = () => {
const navigation = useNavigation<any>();
return (
<GroupChannelListFragment
onPressCreateChannel={(channelType) => {
// Navigate to GroupChannelCreate function.
navigation.navigate('GroupChannelCreate', { channelType });
}}
onPressChannel={(channel) => {
// Navigate to GroupChannel function.
navigation.navigate('GroupChannel', { channelUrl: channel.url });
}}
/>
);
};
const GroupChannelCreateScreen = () => {
const navigation = useNavigation<any>();
return (
<GroupChannelCreateFragment
onCreateChannel={async (channel) => {
// Navigate to GroupChannel function.
navigation.replace('GroupChannel', { channelUrl: channel.url });
}}
onPressHeaderLeft={() => {
// Go back to the previous screen.
navigation.goBack();
}}
/>
);
};
const GroupChannelScreen = () => {
const navigation = useNavigation<any>();
const { params } = useRoute<any>();
const { sdk } = useSendbirdChat();
const { channel } = useGroupChannel(sdk, params.channelUrl);
if (!channel) return null;
return (
<GroupChannelFragment
channel={channel}
onChannelDeleted={() => {
// Navigate to GroupChannelList function.
navigation.navigate('GroupChannelList');
}}
onPressHeaderLeft={() => {
// Go back to the previous screen.
navigation.goBack();
}}
onPressHeaderRight={() => {
// Navigate to GroupChannelSettings function.
navigation.navigate('GroupChannelSettings', { channelUrl: params.channelUrl });
}}
/>
);
};
```
**Register screens to navigator**
```tsx
import { NavigationContainer } from '-navigation/native';
import { createNativeStackNavigator } from '-navigation/native-stack';
import { SendbirdUIKitContainer, useConnection, useSendbirdChat } from '/uikit-react-native';
const RootStack = createNativeStackNavigator();
const SignInScreen = () => {
const { connect } = useConnection();
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Pressable
style={{
width: 120,
height: 30,
backgroundColor: '#742DDD',
alignItems: 'center',
justifyContent: 'center',
}}
onPress={() => connect('USER_ID', { nickname: 'NICKNAME' })}
>
<Text>{'Sign in'}</Text>
</Pressable>
</View>
);
};
const Navigation = () => {
const { currentUser } = useSendbirdChat();
return (
<NavigationContainer>
<RootStack.Navigator screenOptions={{ headerShown: false }}>
{!currentUser ? (
<RootStack.Screen name={'SignIn'} component={SignInScreen} />
) : (
<>
<RootStack.Screen name={'GroupChannelList'} component={GroupChannelListScreen} />
<RootStack.Screen name={'GroupChannelCreate'} component={GroupChannelCreateScreen} />
<RootStack.Screen name={'GroupChannel'} component={GroupChannelScreen} />
</>
)}
</RootStack.Navigator>
</NavigationContainer>
);
};
const App = () => {
return (
<SendbirdUIKitContainer
appId={APP_ID}
platformServices={{
file: FileService,
notification: NotificationService,
clipboard: ClipboardService,
media: MediaService,
}}
>
<Navigation />
</SendbirdUIKitContainer>
);
};
```
> You can use sendbird sdk using `useSendbirdChat()` hook, and you can connect or disconnect using `useConnection()` hook.
> for more details about hooks, please refer to our [docs](https://sendbird.com/docs/uikit/v3/react-native/core-components/hooks)