Introduction to chat-uikit-react-native
chat-uikit-react-native is a React Native UI component library based on Tencent Cloud Chat SDK, providing common UI components such as session, chat, and group features. With these well-designed UI components, you can quickly build elegant, reliable, and scalable chat applications. The UI style developed based on React Native is more in line with the habits of overseas users and supports internationalization. You are welcome to integrate it.
The interface effect of chat-uikit-react-native is shown below:
Environment Requirements
React Native 0.75.0
JDK 17
Xcode version 14.0 or above
Android Studio
Configuring the development environment
If you are developing a React Native project for the first time, please refer to the steps on the React Native official website set-up-your-environment to configure your development environment. If you encounter any environment issues during project creation or compilation, you can run npx react-native doctor
for an environmental diagnosis.
Integrate chat-uikit-react-native
Step 1: Create a project (this step can be skipped if you already have a project)
1. Create a new React Native project.
npx @react-native-community/cli@latest init MyChatApp --version 0.75.0
2. After the project is created, go to the project directory.
Step 2. Integrate chat-uikit-react-native
Download chat-uikit-react-native via npm/yarn and use it in the project. You can also use it for secondary development. npm install @tencentcloud/chat-uikit-react-native react-native-image-picker react-native-document-picker react-native-video
yarn add @tencentcloud/chat-uikit-react-native react-native-image-picker react-native-document-picker react-native-video
Add device permissions
Add the following permissions to the android/app/src/main/AndroidManifest.xml file.
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<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_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add the following permission usage description to the info.plist file.
<key>NSCameraUsageDescription</key>
<string> we would like to use your camera</string>
<key>NSPhotoLibraryUsageDescription</key>
<string> we would like to use your photo library</string>
<key>NSMicrophoneUsageDescription</key>
<string>we would like to use your microphone</string>
Step 3: Set up navigation
Please install the React Navigation dependencies. Refer to the documentation React Navigation. npm install @react-navigation/native react-native-screens react-native-safe-area-context @react-navigation/native-stack
yarn add @react-navigation/native react-native-screens react-native-safe-area-context @react-navigation/native-stack
Step 4. Import chat-uikit-react-native
Note:
The following code does not contain SDKAppID, userID, and userSig, which should be replaced after obtaining the relevant information in Step 5.
To respect the emoji design copyright, the IM Demo/TUIKit project does not include large emoji elements. Before going live and for commercial use, please replace them with other emoji packs that you have designed or have the copyright for. The default yellow face emoji pack shown below is copyrighted by Tencent Cloud and can be licensed for a fee. To obtain authorization, you can Submit a ticket to contact us. Note:
The code below does not contain SDKAppID
, userID
, and userSig
, which should be replaced after obtaining the relevant information in Step 5.
Replace the content in App.tsx, or you can create a new component to introduce.
import React from 'react';
import {
View,
TouchableOpacity,
Text,
Image,
StyleSheet,
} from 'react-native';
import { NavigationContainer, useNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { UIKitProvider } from '@tencentcloud/chat-uikit-react-native';
import resources from '@tencentcloud/chat-uikit-react-native/i18n';
import { TUITranslateService } from '@tencentcloud/chat-uikit-engine';
import { TUILogin } from '@tencentcloud/tui-core';
import { ConversationListScreen, ChatScreen, ChatSettingScreen } from './Screens';
const LoginScreen = () => {
const navigation = useNavigation<any>();
TUITranslateService.provideLanguages(resources);
TUITranslateService.useI18n('en-US');
const Login = () => {
TUILogin.login({
SDKAppID: 0,
userID: 'test_1',
userSig: '',
useUploadPlugin: true,
framework: 'rn',
}).then(() => {
navigation.navigate('ConversationList');
});
}
return (
<View style={styles.container}>
<Image
style={styles.logo}
source={{uri:'https://web.sdk.qcloud.com/im/assets/images/tencent_rtc_logo.png'}}
/>
<TouchableOpacity style={styles.buttonContainer} onPress={Login}>
<Text style={styles.buttonText}>Log in</Text>
</TouchableOpacity>
</View>
);
};
const Navigation = () => {
const Stack = createNativeStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{ headerShown: false }}
initialRouteName="Login">
<Stack.Screen
name="Login"
component={LoginScreen} />
<Stack.Screen
name="ConversationList"
component={ConversationListScreen} />
<Stack.Screen
name="Chat"
component={ChatScreen} />
<Stack.Screen
name="ChatSetting"
component={ChatSettingScreen}/>
</Stack.Navigator>
</NavigationContainer>
);
};
const App = () => {
return (
<UIKitProvider>
<Navigation />
</UIKitProvider>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
},
logo: {
width: 232,
height: 80,
},
buttonContainer: {
width: '80%',
justifyContent: 'center',
alignItems: 'center',
paddingVertical: 11,
borderRadius: 5,
backgroundColor: '#2F80ED',
},
buttonText: {
fontSize: 18,
lineHeight: 24,
color: '#FFFFFF',
},
});
export default App;
Create a new Screens.tsx file in the same directory as the App.tsx file.
import React from 'react';
import { useNavigation } from '@react-navigation/native';
import { ConversationList, Chat, ChatSetting } from '@tencentcloud/chat-uikit-react-native';
export const ConversationListScreen = () => {
const navigation = useNavigation<any>();
const onPressConversation = () => {
navigation.navigate('Chat');
};
return (
<ConversationList onPressConversation={onPressConversation} />
);
};
export const ChatScreen = () => {
const navigation = useNavigation<any>();
const navigateBack = () => {
navigation.goBack();
};
const navigateToChatSetting = () => {
navigation.navigate('ChatSetting');
};
return (
<Chat
navigateBack={navigateBack}
navigateToChatSetting={navigateToChatSetting}
/>
);
};
export const ChatSettingScreen = () => {
const navigation = useNavigation<any>();
const navigateBack = () => {
navigation.goBack();
};
const navigateToChat = () => {
navigation.goBack();
};
const navigateToConversationList = () => {
navigation.navigate('ConversationList');
};
return (
<ChatSetting
navigateBack={navigateBack}
navigateToChat={navigateToChat}
navigateToConversationList={navigateToConversationList}
/>
);
};
Step 5: Obtain SDKAppID, userID, and userSig
Obtain the relevant parameters SDKAppID, userID, and the corresponding userSig from Login
:
SDKAppID
, can be obtained through Chat Console in Applications
:userID
Click to enter the Application you created above, you will see the Chat
product entry in the left sidebar, click to enter. After entering the Chat product sub-page, click Users
to enter the User Management page.
Click Create account
to pop up the account creation information filling box. If it is just a regular member, we recommend selecting the General
type.
For a better experience with messaging and other features, it is recommended that you create two user IDs (test_1, test_2).
Step 6: Compile and run the project
To compile and run the project, you need to use a real device or an emulator. It is recommended to use a real device. You can refer to the React Native official website running-on-device for connecting a real device for debugging. Replace SDKAppID, userID, userSig in App.tsx, then run the following command:
1. Enable Developer Mode on the phone and turn on the USB Debugging switch.
2. Connect the phone with a USB cable, it is recommended to choose the Transfering File option, do not choose the Charge Only option.
3. After confirming the phone is successfully connected, execute npm run android
to compile and run the project.
1. Connect the phone with a USB cable and open the project ios directory with Xcode.
2. Configure the signing information according to the running-on-device section on the React Native official website. 3. Go to the ios directory and install dependencies.
4. Go back to the root directory and execute npm run ios to compile and run the project.
Step 7: Send your first message
1. After the project starts, click Initiate Session in the top left corner.
2. Enter the Initiate Session window. In the search bar, enter the user ID created in Step 5 (test_2), select it and open the session.
3. Enter the message in the input box and click send.
Exchange and Feedback
FAQs
What to do when there is a runtime environment error?
You can run the following command for environmental diagnosis.
Documentation
Related to UIKit:
Was this page helpful?