网易云信 React Native SDK
npm install @kangfenmao/react-native-netease-im
import { NeteaseIm } from "@kangfenmao/react-native-netease-im"
NeteaseIm.foo()
详细使用方法参考 App.tsx
- 在项目入口 index.js 中添加初始化方法
import { NeteaseIm } from "@kangfenmao/react-native-netease-im"
NeteaseIm.init("appKey", "apnsCername")
注:第二个参数为iOS推送证书名,可为空字符串
- Android 系统还需要在 MainApplication.java 的
onCreate
方法中添加初始化方法
import com.kangfenmao.nim.NeteaseIm;
@Override
public void onCreate() {
// ...
NeteaseIm.init(this, "appKey");
}
const listeners: EmitterSubscription[] = []
listeners.push(
// 连接状态改变
NeteaseImEvent.addListener('onConnectStatusChanged', (event: ConnectStatus) =>
console.log(event)
),
// 收到消息
NeteaseImEvent.addListener('onMessages', (messages: NIM.Message[]) =>
console.log(messages)
),
// 会话列表更新
NeteaseImEvent.addListener('onConversationsChanged', (conversations: NIM.Conversation[]) =>
console.log(conversations)
)
)
// listeners.forEach((listener) => listener.remove())
备注:此方法用于用户第一次登录
try {
const result = await NeteaseIm.login('account', 'token')
console.log(result)
} catch (error) {
console.log(error.code, error.message)
}
备注:
- 每次启动 App 后都需要调用此方法用于快速登录,快速登录不需要用户名和 token。
- 此方法没有返回值,需要监听连接状态。
- 当用户退出后必须重新登录后调用此接口才能成功登录。
try {
await NeteaseIm.autoLogin()
} catch (error) {
console.log(error.message)
}
检查登录状态
const logined = await NeteaseIm.getLogined()
console.log('是否登录:', logined)
注意:这个方法没有返回值
NeteaseIm.logout()
发送文本消息
try {
const result = await NeteaseIm.sendMessage(
'kangfenmao',
'这是一条文本消息',
NimSessionTypeEnum.P2P,
false
)
console.log(result)
} catch (error) {
console.log({ code: error.code, message: error.message })
}
sendMessage(
account: string,
text: string,
type: NimSessionTypeEnum,
resend: boolean
): Promise<NIM.PromiseResult>
发送图片
try {
const result = await NeteaseIm.sendMessage(
'kangfenmao',
'图片路径',
NimSessionTypeEnum.P2P,
false
)
console.log(result)
} catch (error) {
console.log({ code: error.code, message: error.message })
}
sendImage(
account: string,
path: string,
type: NimSessionTypeEnum,
resend: boolean
): Promise<NIM.PromiseResult>
获取一条消息
getMessage(messageId: string, account: string, sessionType: SessionTypeEnum): Promise<NIM.Message>
获取历史消息
getHistoryMessages(
sessionId: string,
sessionType: string,
messageId: string,
limit: number,
asc: boolean
): Promise<NIM.Message[]>
messageId 为空字符串时查询最新的历史消息
获取最近会话列表
await NeteaseIm.getConversations()
删除单条会话
NeteaseIm.deleteConversation(sessionId, SessionType.P2P)
重置会话未读数
NeteaseIm.resetConversationUnreadCount(sessionId, SessionTypeEnum.P2P)
获取总未读数
await NeteaseIm.getTotalUnreadCount()
NeteaseIm.sdkVersion
修改 index.js
中的初始化方法,第二个参数传入证书名:
NeteaseIm.init("appKey", "apnsCertname")
在 MainApplication.java
头部引入
import com.netease.nimlib.sdk.mixpush.MixPushConfig;
修改 onCreate
方法中的初始化方法,加入 MixPushConfig
:
NeteaseIm.init(this, "appKey", mixPushConfig);
mixPushConfig 配置
MixPushConfig mixPushConfig = new MixPushConfig();
// 小米推送
mixPushConfig.xmAppId = "";
mixPushConfig.xmAppKey = "";
mixPushConfig.xmCertificateName = "";
// 华为
mixPushConfig.hwAppId = "";
mixPushConfig.hwCertificateName = "";
// 魅族
mixPushConfig.mzAppId = "";
mixPushConfig.mzAppKey = "";
mixPushConfig.mzCertificateName = "";
// OPPO
mixPushConfig.oppoAppId = "";
mixPushConfig.oppoAppKey = "";
mixPushConfig.oppoAppSercet = "";
mixPushConfig.oppoCertificateName = "";
// VIVO
mixPushConfig.vivoCertificateName = "";
将项目 clone 到本地
运行
yarn prepair
修改 example/env.json
yarn example ios
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT