NetEase Yunxin React Native SDK

react-native-netease-im

NetEase Yunxin React Native SDK

installation

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

Instructions

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

NeteaseIm.foo()

For detailed usage, please refer to App.tsx

initialization

  1. Add an initialization method to the project entry index.js
import { NeteaseIm } from "@kangfenmao/react-native-netease-im"

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

Note: The second parameter is the iOS push certificate name, which can be an empty string

  1. MainApplication.java Android system also need to onCreateadd initialization methods of
import com.kangfenmao.nim.NeteaseIm;

@Override
public void onCreate() {
  // ...
  NeteaseIm.init(this, "appKey");
}

event

const listeners: EmitterSubscription[] = []

listeners . push ( 
  // Connection status changed 
  NeteaseImEvent . addListener ( 'onConnectStatusChanged' ,  ( event : ConnectStatus )  => 
    console . log ( event ) 
  ) , 
  // Received a message 
  NeteaseImEvent . addListener ( 'onMessages' ,  ( messages : NIM . Message [] )  => 
    console . log ( messages ) 
  ) , 
  // Conversation list update 
  NeteaseImEvent . addListener ( 'onConversationsChanged' ,  ( conversations : NIM . Conversation []) =>
    console.log(conversations)
  )
)

// listeners.forEach((listener) => listener.remove())

API

login

Note: This method is used for the first login of the user

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

autoLogin

Remarks:

  1. Every time you start the App, you need to call this method for fast login, and fast login does not require a user name and token.
  2. This method has no return value and needs to monitor the connection status.
  3. When the user logs out, he must log in again and call this interface to log in successfully.
try {
  await NeteaseIm.autoLogin()
} catch (error) {
  console.log(error.message)
}

getLoggined

Check login status

const  logined  =  await  NeteaseIm . getLogined ( ) 
console . log ( 'Whether to log in:' ,  logined )

logout

Note: This method has no return value

NeteaseIm.logout()

sendMessage

Send text message

try  { 
  const  result  =  await  NeteaseIm . sendMessage ( 
    'kangfenmao' , 
    'This is a text message' , 
    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>

getMessage

Get a message

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

getHistoryMessages

Get historical news

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

Query the latest historical message when messageId is an empty string

getConversations

Get a list of recent conversations

await NeteaseIm.getConversations()

deleteConversation

Delete a single conversation

NeteaseIm.deleteConversation(sessionId, SessionType.P2P)

resetConversationUnreadCount

Reset session unread

NeteaseIm.resetConversationUnreadCount(sessionId, SessionTypeEnum.P2P)

getTotalUnreadCount

Get total unread

await  NeteaseIm . getTotalUnreadCount ( )

SDK version

NeteaseIm.sdkVersion

Push configuration

iOS push configuration

Modify index.jsthe Init method, and the second parameter passed Certificate Name:

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

Android push configuration

In MainApplication.javaintroducing head

import com.netease.nimlib.sdk.mixpush.MixPushConfig;

Modify onCreateinitialization methods is added MixPushConfig:

NeteaseIm.init(this, "appKey", mixPushConfig);

mixPushConfig configuration

MixPushConfig mixPushConfig = new MixPushConfig();

// Mi push 
mixPushConfig . XmAppId =  " " ;
mixPushConfig.xmAppKey = "";
mixPushConfig.xmCertificateName = "";

// Huawei 
mixPushConfig . HwAppId =  " " ;
mixPushConfig.hwCertificateName = "";

// Meizu 
mixPushConfig . MzAppId =  " " ;
mixPushConfig.mzAppKey = "";
mixPushConfig.mzCertificateName = "";

// OPPO
mixPushConfig.oppoAppId = "";
mixPushConfig.oppoAppKey = "";
mixPushConfig.oppoAppSercet = "";
mixPushConfig.oppoCertificateName = "";

// VIVO
mixPushConfig.vivoCertificateName = "";

Example

Clone the project to the local

run

yarn prepair

Modify example/env.json

yarn example ios

Contributing

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

Download Details:

Author: kangfenmao

Source Code: https://github.com/kangfenmao/react-native-netease-im

#react-native #react #mobile-apps

NetEase Yunxin React Native SDK
2.75 GEEK