平台 | 版本 |
React Native | 0.75.0 及以上版本。 |
Android | Android Studio 3.5 及以上版本,App 要求 Android 4.1 及以上版本设备。 |
iOS | Xcode 11.0 及以上版本,真机调试请确保您的项目已设置有效的开发者签名。 |
Node | 18.0 及以上版本。 |
npx react-native doctor
进行环境诊断。npx react-native@latest init chatExample
npm react-native
创建的项目 Android gradle 版本用的是 8.8,如果您 Android gradle 版本低于 8.8,请打开 Android Studio 同步 gradle 版本。npm install @tencentcloud/chat tim-upload-plugin @react-native-community/netinfo --save
import TencentCloudChat from '@tencentcloud/chat';import TIMUploadPlugin from 'tim-upload-plugin';import NetInfo from '@react-native-community/netinfo';let options = {SDKAppID: 0 // 接入时需要将0替换为您的即时通信 IM 应用的 SDKAppID};// 创建 SDK 实例,`TencentCloudChat.create()`方法对于同一个 `SDKAppID` 只会返回同一份实例let chat = TencentCloudChat.create(options); // SDK 实例通常用 chat 表示chat.setLogLevel(0); // 普通级别,日志量较多,接入时建议使用// 注册腾讯云即时通信富媒体资源上传插件chat.registerPlugin({'tim-upload-plugin': TIMUploadPlugin});// 注册网络监听插件chat.registerPlugin({'chat-network-monitor': NetInfo});
let onSdkReady = function(event) {// 监听到 SDK ready 后可进行 API 调用};chat.on(TencentCloudChat.EVENT.SDK_READY, onSdkReady);
let onSdkNotReady = function(event) {// chat.login({userID: 'your userID', userSig: 'your userSig'});};chat.on(TencentCloudChat.EVENT.SDK_NOT_READY, onSdkNotReady);
let onMessageReceived = function(event) {// event.data - 存储 Message 对象的数组 - [Message]};chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);
let onConversationListUpdated = function(event) {console.log(event.data); // 包含 Conversation 实例的数组};chat.on(TencentCloudChat.EVENT.CONVERSATION_LIST_UPDATED, onConversationListUpdated);
chat.destroy();
let promise = chat.login({userID: 'your userID',userSig: 'your userSig',});promise.then(function(imResponse) {if (imResponse.data.repeatLogin === true) {// 标识账号已登录,本次登录操作为重复登录。console.log(imResponse.data.errorInfo);}}).catch(function(imError) {// 登录失败的相关信息console.warn('login error:', imError);});
npm run android
编译运行项目。npm run android
cd iospod install
npm run ios
编译运行项目。cd ../npm run ios
npm install react-native-image-picker --save
import {launchImageLibrary} from 'react-native-image-picker';// 1. 选择图片launchImageLibrary({mediaType: 'photo',selectionLimit: 1,}).then((result) => {const file = result.assets[0];// 2. 创建消息实例,接口返回的实例可以上屏let message = chat.createImageMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { file: file },// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});// 3. 发送图片let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});});
import {launchCamera} from 'react-native-image-picker';// 1. 拍照launchCamera({mediaType: 'photo',cameraType: 'back',}).then((result) => {const file = result.assets[0];// 2. 创建消息实例,接口返回的实例可以上屏let message = chat.createImageMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { file: file },// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});// 3. 发送图片let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});});
import {launchImageLibrary} from 'react-native-image-picker';// 1. 选择视频launchImageLibrary({mediaType: 'video',selectionLimit: 1,}).then((result) => {const file = result.assets[0];// 2. 创建消息实例,接口返回的实例可以上屏let message = chat.createVideoMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { file: file },// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});// 3. 发送视频let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});});
import {launchCamera} from 'react-native-image-picker';// 1. 拍摄视频launchCamera({mediaType: 'video',cameraType: 'back',}).then((result) => {const file = result.assets[0];// 2. 创建消息实例,接口返回的实例可以上屏let message = chat.createVideoMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { file: file },// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});// 3. 发送视频let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});});
npm install react-native-document-picker --save
import DocumentPicker from 'react-native-document-picker';// 1. 选择文件DocumentPicker.pick({type: [DocumentPicker.types.allFiles],}).then((result) => {const file = result[0];// 2. 创建消息实例,接口返回的实例可以上屏let message = chat.createFileMessage({to: 'user1',conversationType: TencentCloudChat.TYPES.CONV_C2C,payload: { file: file },// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});// 3. 发送文件let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});});
npm install react-native-audio-recorder-player --save
import AudioRecorderPlayer, {AVEncodingOption} from 'react-native-audio-recorder-player';// 记录录音时长let duration = 0;// 记录录音文件路径let uri = '';// 1. 开始录音const onStartRecord = async () => {await audioRecorderPlayer.startRecorder('test.aac',{VFormatIDKeyIOS: AVEncodingOption.aac,});audioRecorderPlayer.addRecordBackListener((e: any) => {duration = e.currentPosition;});};// 2. 结束录音const onStopRecord = async () => {uri = await audioRecorderPlayer.stopRecorder();audioRecorderPlayer.removeRecordBackListener();};// 3. 发送语音const file = {uri: uri,duration: duration,};let message = chat.createAudioMessage({to: 'user1',conversationType: 'C2C',payload: {file: file,},// React Native 不支持上传进度回调// onProgress: function(event) { console.log('file uploading:', event) }});let promise = chat.sendMessage(message);promise.then(function(imResponse) {// 发送成功console.log(imResponse);}).catch(function(imError) {// 发送失败console.warn('sendMessage error:', imError);});
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.RECORD_AUDIO" />
<key>NSCameraUsageDescription</key><string>我们需要访问您的相机以拍摄照片</string><key>NSPhotoLibraryUsageDescription</key><string>我们需要访问您的相册以选择照片</string><key>NSMicrophoneUsageDescription</key><string>我们需要访问您的麦克风以录制音频</string>
const file = {uri: 'xxx',fileName: 'xxx',fileSize: 1,type: 'xxx',width: 1,height: 1,};
const file = {uri: 'xxx',fileName: 'xxx',fileSize: 1,type: 'xxx',duration: 0,};
const file = {uri: 'xxx',name: 'xxx',size: 1,};
const file = {uri: 'xxx',fileName: 'xxx',fileSize: 1,duration: 0,};
npm run android
提示如图所示错误时,请在项目更目录下重新设置环境变量。export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/platform-tools
cd iosecho export NODE_BINARY=$(command -v node) > .xcode.env
本页内容是否解决了您的问题?