Skip to content

HarmonyOS 从零接入雁帛IM ​

本页面向 ArkTS Stage 工程。DevEco、Harmony SDK 与目标 ABI 按 HAR 交付清单匹配, 先完成 接入前准备。完成后,你可以安装 HAR、连接账号、监听事件并发送 第一条文字消息。

1. 准备接入信息 ​

向服务端负责人领取 Server URL、App ID、当前用户 ID 和一个对端测试用户 ID。 正式环境还需要应用自己的后台提供短期 XHIM 凭证。

2. 安装 HAR ​

当前移动交付基线为 0.1.0-dev.97;取得匹配的完整 HAR 和清单。 独立 HarmonyOS Starter 已完成完整固定 HAR 空白构建与模拟器三项生命周期检查。此包为 arm64,可信 HTTPS 正向消息、 历史、换号、进程重启及真机签名仍待验收;不能用生命周期结果替代收发。 把 xhim.har 放入模块的 libs/,在 oh-package.json5 中添加:

json5
{
  "dependencies": {
    "@xihansoftware/xhim": "file:./libs/xhim.har"
  }
}

执行 ohpm install。完整 HAR 应同时包含 ArkTS 类型、N-API 与目标设备需要的 Native Runtime;不要只复制 Index.ets。

3. 配置网络权限 ​

json5
{
  "module": {
    "requestPermissions": [
      { "name": "ohos.permission.INTERNET" },
      { "name": "ohos.permission.GET_NETWORK_INFO" }
    ]
  }
}

正式环境使用 HTTPS/WSS,不在应用中加入“忽略证书”开关。

4. 连接当前账号 ​

Development Server 已开启测试登录时:

ts
import { XHIMClient } from '@xihansoftware/xhim'

const client = await XHIMClient.connect(
  getContext(this),
  'https://im-test.example.com',
  currentUserId
)

正式环境增加业务凭证提供器:

ts
import {
  XHIMAuthentication,
  XHIMClient
} from '@xihansoftware/xhim'

const client = await XHIMClient.connect(
  getContext(this),
  'https://im.example.com',
  currentUserId,
  {
    appId: 'your-app',
    authentication: XHIMAuthentication.business(
      (): Promise<string> => accountApi.fetchXHIMCredential()
    )
  }
)

把 Client 放在账号级 Store 中,不要在每个 Page 中重复连接。

5. 订阅并确认就绪 ​

ts
import {
  XHIMClientState,
  XHIMStateChangedEvent
} from '@xihansoftware/xhim'

const stopEvents = client.onEvent((event): void => {
  if (event instanceof XHIMStateChangedEvent) {
    accountStore.updateConnectionState(event.state)
  }
  messageStore.handleXHIMEvent(event)
})
accountStore.updateConnectionState(client.state())

accountStore 和 messageStore 是宿主账号层与消息层:前者仅在 XHIMClientState.READY 时允许发送,后者在就绪时查询受影响页面。订阅后读取 当前状态,不用固定延时。connect 返回后仍需检查实时连接/同步状态。

就绪后获取单聊会话并发送:

ts
const direct = await client.directConversation('bob')
await client.sendText(direct.conversationId, '你好,雁帛IM')

发送完成表示 Outbox 受理,最终状态通过消息查询与事件取得。收到变化后重新查询, Page 不自行维护 WebSocket 或消息数据库。正式 Provider 与设备身份见 账号认证。

6. 页面退出、登出与换号 ​

普通 Page 销毁时只取消自己的监听:

ts
stopEvents()

用户真正登出或切换账号时:

ts
await client.logout()
await client.shutdown()

7. 常用功能入口 ​

任务方法
查询会话client.conversations(...)
查询消息client.messages(...)
标记已读client.markConversationRead(...)
查询用户资料client.currentUserProfile()
查询好友和群组client.friendships(...) / client.groups(...)
创建群client.createGroup(...)
查询在线状态client.queryPresence(...)

完整参数、返回值与错误见 SDK API Reference。

8. 二次开发建议 ​

把 Client 和长期事件监听放在账号 Store;Repository 负责查询,ArkUI Page 只渲染 不可变结果。需要自定义消息 Renderer 或页面结构时查看 客户端二次开发指南。

9. 常见问题 ​

安装后找不到 Native Library ​

当前 HAR 不包含目标设备 ABI,或打包时漏掉 Native 文件。更换完整发行包,不要 手工复制其他版本 .so。

Development 可以连接,Production 不可以 ​

正式环境必须使用业务凭证提供器和可信 HTTPS/WSS,不能只传 User ID。

收到事件但页面没变化 ​

确认 messageStore.handleXHIMEvent 会重新查询当前会话,而不是只记录日志。

发布验收 ​

验证 HAP 签名、真实设备网络/权限、双账号收发、断网、前后台和换号;HAR 中的 Native 必须来自同一发行包。完整清单见 上线检查。

XHIM 客户端 SDK 与服务端文档