@gathertown/uikit-react-native
v0.0.14
Published
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
Downloads
2,978
Readme
@sendbird/uikit-react-native
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
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 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)
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
version0.72
or higher, you don't need to install@sendbird/react-native-scrollview-enhancer
.
npm install @sendbird/uikit-react-native \
@sendbird/chat \
@sendbird/react-native-scrollview-enhancer \
date-fns \
react-native-safe-area-context \
@react-native-community/netinfo \
@react-native-async-storage/async-storage
Linking native modules
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.
<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, you must update permissions and run
pod install
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.
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
Using React Native CLI
You can use createNativeClipboardService
, createNativeNotificationService
, createNativeFileService
and createNativeMediaService
helper functions with below native modules.
npm install react-native-video \
react-native-permissions \
react-native-file-access \
react-native-image-picker \
react-native-document-picker \
react-native-create-thumbnail \
@react-native-clipboard/clipboard \
@react-native-camera-roll/camera-roll \
@react-native-firebase/app \
@react-native-firebase/messaging \
@bam.tech/react-native-image-resizer
npx pod-install
import * as ImageResizer from '@bam.tech/react-native-image-resizer';
import { CameraRoll } from '@react-native-camera-roll/camera-roll';
import Clipboard from '@react-native-clipboard/clipboard';
import RNFBMessaging from '@react-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.
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
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.
npm i @react-native-async-storage/async-storage
npx pod-isntall
import AsyncStorage from '@react-native-async-storage/async-storage';
import { SendbirdUIKitContainer } from '@sendbird/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
)
import { MMKV } from 'react-native-mmkv';
import { LocalCacheStorage, SendbirdUIKitContainer } from '@sendbird/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
.
See more details on here
The example below shows how to integrate using react-navigation
.
Create a fragments and screens
import { useNavigation, useRoute } from '@react-navigation/native';
import { useGroupChannel } from '@sendbird/uikit-chat-hooks';
import {
createGroupChannelCreateFragment,
createGroupChannelFragment,
createGroupChannelListFragment,
} from '@sendbird/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
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { SendbirdUIKitContainer, useConnection, useSendbirdChat } from '@sendbird/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 usinguseConnection()
hook. for more details about hooks, please refer to our docs