概述
RTM 呼叫邀请包括以下场景:
- 主叫发送邀请和取消邀请
- 被叫接收邀请和拒绝邀请

RTM SDK呼叫邀请主要分为四个模块:发送、取消、接收、拒绝呼叫邀请。
RTM SDK是以发信令的形式来实现呼叫邀请的基本逻辑,收到邀请接通后的动作,以及逻辑RTM SDK不做处理,这里需要根据自己的业务逻辑来实现。
适用场景
- app呼叫邀请的响铃功能
- 拨打语音电话呼叫邀请功能
- 需要同步状态的呼叫场景
实现流程
在一个完整的呼叫过程中,主叫和被叫的呼叫邀请状态分别由 LocalInvitation 和 RemoteInvitation 来定义。

发送呼叫邀请
发送呼叫邀请的步骤:
- 获取
RtmCallManager实例 - 设置
setEventListener呼叫邀请监听器 - 主叫调用
createLocalInvitation创建LocalInvitation,获取LocalInvitation对象,此时LocalInvitation对象生命周期开始 - 主叫调用
sendLocalInvitation发送呼叫邀请。被叫收到onRemoteInvitationReceived回调,获取RemoteInvitation对象,此时RemoteInvitation对象生命周期开始。主叫收到onLocalInvitationReceivedByPeer回调。
发送呼叫邀请的示例代码如下:
rtmClient.callKit.sendLocalInvitation(localInvitation, object : ResultCallback<Void> {
override fun onSuccess(resp: Void?) {
runMainThread {
rtmClient.localInvitations[localInvitation.calleeId] = localInvitation
result.success(hashMapOf(
"errorCode" to 0
))
}
}
取消呼叫邀请
主叫调用 cancelLocalInvitation 取消呼叫邀请。被叫收到 onRemoteInvitationCanceled 回调,此时 RemoteInvitation 生命周期结束。

主叫收到 onLocalInvitationCanceled 回调,此时LocalInvitation对象 生命周期结束。
override
fun onRemoteInvitationCanceled(remoteInvitation: RemoteInvitation) {
remoteInvitations.remove(remoteInvitation.callerId)
sendClientEvent("onRemoteInvitationCanceled", hashMapOf(
"remoteInvitation" to hashMapOf(
"callerId" to remoteInvitation.callerId,
"content" to remoteInvitation.content,
"channelId" to remoteInvitation.channelId,
"state" to remoteInvitation.state,
"response" to remoteInvitation.response
)
))
}
接受呼叫邀请
被叫从 onRemoteInvitationReceived 回调获取 RemoteInvitation 对象并调用 acceptRemoteInvitation 接受呼叫邀请。被叫收到 onRemoteInvitationAccepted 回调,此时 RemoteInvitation 对象生命周期结束。主叫收到 onLocalInvitationAccepted 回调,此时 LocalInvitation 对象生命周期结束。

rtmClient.callKit.acceptRemoteInvitation(remoteInvitation, object : ResultCallback<Void> {
override fun onSuccess(resp: Void?) {
runMainThread {
rtmClient.remoteInvitations.remove(remoteInvitation.callerId)
result.success(hashMapOf(
"errorCode" to 0
))
}
}
override fun onFailure(code: ErrorInfo) {
runMainThread {
result.success(hashMapOf("errorCode" to code.getErrorCode()))
}
}
});
拒绝呼叫邀请
被叫从 onRemoteInvitationReceived 回调获取 RemoteInvitation对象 并调用 refuseRemoteInvitation 拒绝呼叫邀请。被叫收到 onRemoteInvitationRefused 回调,此时 RemoteInvitation 对象生命周期结束。主叫收到 onLocalInvitationRefused 回调,此时 LocalInvitation 对象生命周期结束。

Future<void> refuseRemoteInvitation(Map<dynamic, dynamic> arguments) async {
final res = await _callNative("refuseRemoteInvitation", arguments);
if (res["errorCode"] != 0)
throw ARRtmClientException(
"refuseRemoteInvitation failed errorCode:${res['errorCode']}",
res['errorCode']);
}
API参考
API 详见呼叫邀请 API 文档呼叫邀请管理。
示例项目
我们在 GitHub 提供一个开源的示例项目 ,你可以前往下载体验并参考源代码。

