/react-native-netease-im

网易云信 React Native SDK

Primary LanguageJavaMIT LicenseMIT

react-native-netease-im

网易云信 React Native SDK

安装

npm install @kangfenmao/react-native-netease-im

使用方法

import { NeteaseIm } from "@kangfenmao/react-native-netease-im"

NeteaseIm.foo()

详细使用方法参考 App.tsx

初始化

  1. 在项目入口 index.js 中添加初始化方法
import { NeteaseIm } from "@kangfenmao/react-native-netease-im"

NeteaseIm.init("appKey", "apnsCername")

注:第二个参数为iOS推送证书名,可为空字符串

  1. 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())

API

login

备注:此方法用于用户第一次登录

try {
  const result = await NeteaseIm.login('account', 'token')
  console.log(result)
} catch (error) {
  console.log(error.code, error.message)
}

autoLogin

备注:

  1. 每次启动 App 后都需要调用此方法用于快速登录,快速登录不需要用户名和 token。
  2. 此方法没有返回值,需要监听连接状态。
  3. 当用户退出后必须重新登录后调用此接口才能成功登录。
try {
  await NeteaseIm.autoLogin()
} catch (error) {
  console.log(error.message)
}

getLoggined

检查登录状态

const logined = await NeteaseIm.getLogined()
console.log('是否登录:', logined)

logout

注意:这个方法没有返回值

NeteaseIm.logout()

sendMessage

发送文本消息

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>

sendImage

发送图片

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

获取一条消息

getMessage(messageId: string, account: string, sessionType: SessionTypeEnum): Promise<NIM.Message>

getHistoryMessages

获取历史消息

getHistoryMessages(
  sessionId: string,
  sessionType: string,
  messageId: string,
  limit: number,
  asc: boolean
): Promise<NIM.Message[]>

messageId 为空字符串时查询最新的历史消息

getConversations

获取最近会话列表

await NeteaseIm.getConversations()

deleteConversation

删除单条会话

NeteaseIm.deleteConversation(sessionId, SessionType.P2P)

resetConversationUnreadCount

重置会话未读数

NeteaseIm.resetConversationUnreadCount(sessionId, SessionTypeEnum.P2P)

getTotalUnreadCount

获取总未读数

await NeteaseIm.getTotalUnreadCount()

SDK version

NeteaseIm.sdkVersion

推送配置

iOS 推送配置

修改 index.js 中的初始化方法,第二个参数传入证书名:

NeteaseIm.init("appKey", "apnsCertname")

Android 推送配置

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 = "";

Example

将项目 clone 到本地

运行

yarn prepair

修改 example/env.json

yarn example ios

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT