tencent cloud

Feedback

React Native

Last updated: 2024-11-15 11:53:46

    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
    Node.js version 18+
    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.
    cd MyChatApp

    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
    yarn
    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
    Android
    iOS
    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
    yarn
    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.
    
    
    
    App.tsx
    Screens.tsx
    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 } from './Screens';
    
    const config = {
    SDKAppID: 0, // Your SDKAppID
    userID: 'test-1', // Login UserID
    userSig: '', // Your userSig
    }
    
    const LoginScreen = () => {
    const navigation = useNavigation<any>();
    // Init localization
    TUITranslateService.provideLanguages(resources);
    TUITranslateService.useI18n('en-US');
    // Login
    const Login = () => {
    TUILogin.login({
    ...config,
    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.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 } 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();
    };
    return (
    <Chat navigateBack={navigateBack} />
    );
    };

    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).
    
    userSig can be generated in real-time using the development tools provided by the console. For development tools, click Chat Console > Development Tools > UserSig Tools > Signature (UserSig) Generator.
    

    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:
    Android
    iOS
    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.
    npm run android
    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.
    cd ios
    pod install
    4. Go back to the root directory and execute npm run ios to compile and run the project.
    cd ../
    npm run ios

    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

    Join Telegram Technical Support Group or WhatsApp Communication Group for professional engineer support to solve your problems.

    FAQs

    What to do when there is a runtime environment error?

    You can run the following command for environmental diagnosis.
    npx react-native doctor

    Documentation

    Related to UIKit:

    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support