邀请机制

说明:以下所有方法都位于SignallingService

邀请他人

 /**
     * 邀请他人加入频道
     * 该接口用于邀请对方加入频道,邀请者必须是创建者或者是频道中成员。如果需要对离线成员邀请,可以打开离线邀请开关并填写推送信息,被邀请者上线后通过离线通知接收到该邀请。
     * @param paramBuilder InviteParamBuilder {@link InviteParamBuilder}
     * @return InvocationFuture<Void>
     */
    InvocationFuture<Void> invite(InviteParamBuilder paramBuilder);
/**
 * 邀请别人
 */
private void inviteOther() {
    if (channelFullInfo == null) {
        Toast.makeText(this, "请先创建频道或加入频道", Toast.LENGTH_SHORT).show();
        return;
    }
    String account = edtInviteAccount.getText().toString();
    if (TextUtils.isEmpty(account)) {
        Toast.makeText(this, "请输入对方帐号", Toast.LENGTH_SHORT).show();
        return;
    }
    invitedRequestId = System.currentTimeMillis() + "_id";
    InviteParamBuilder param = new InviteParamBuilder(channelFullInfo.getChannelId(), account, invitedRequestId);
    param.offlineEnabled(true);
    service.invite(param).setCallback(new RequestCallback<Void>() {
        @Override
        public void onSuccess(Void param) {
            Toast.makeText(this, "邀请成功 :channelId = " + channelFullInfo.getChannelId() + ", requestId = " + invitedRequestId, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed(int code) {
            Toast.makeText(this, "邀请失败 :code = " + code, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onException(Throwable exception) {
             Toast.makeText(this, "邀请异常 :exception = " + exception, Toast.LENGTH_SHORT).show();
        }
    });
}

取消邀请

/**
     * 取消邀请
     * @param paramBuilder InviteParamBuilder {@link InviteParamBuilder}
     * @return InvocationFuture<Void>
     */
    InvocationFuture<Void> cancelInvite(InviteParamBuilder paramBuilder);

参数InviteParamBuilder中的 requestId 需要传入邀请时生成的id。

/**
 * 取消邀请别人
 */
private void cancelInviteOther() {
    if (channelFullInfo == null) {
        Toast.makeText(this, "请先创建频道或加入频道", Toast.LENGTH_SHORT).show();
        return;
    }
    String account = edtInviteAccount.getText().toString();
    if (TextUtils.isEmpty(account)) {
        Toast.makeText(this, "请输入对方帐号", Toast.LENGTH_SHORT).show();
        return;
    }
    InviteParamBuilder param = new InviteParamBuilder(channelFullInfo.getChannelId(), account, invitedRequestId);
    param.offlineEnabled(true);
    service.cancelInvite(param).setCallback(new RequestCallback<Void>() {
        @Override
        public void onSuccess(Void param) {
           Toast.makeText(this, "取消邀请成功 :channelId = " + channelFullInfo.getChannelId() + ", requestId = " + invitedRequestId, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed(int code) {
            Toast.makeText(this, "取消邀请失败 :code = " + code, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onException(Throwable exception) {
            Toast.makeText(this, "取消邀请异常 :exception = " + exception, Toast.LENGTH_SHORT).show();
        }
    });
}

接受邀请

   /**
     * 接受对方邀请,但并不代表加入了频道 。
     * @param paramBuilder InviteParamBuilder {@link InviteParamBuilder}
     * @return InvocationFuture<Void>
     */
    InvocationFuture<Void> acceptInvite(InviteParamBuilder paramBuilder);


   /**
     * 接受对方邀请并自动加入频道
     * @param paramBuilder InviteParamBuilder 
     * @param selfUid      自己在频道中对应的uid,大于零有效,无效时服务器会分配随机唯一的uid, 也可以自己生成,但要保证唯一性
     * @return InvocationFuture<ChannelFullInfo>
     */
    InvocationFuture<ChannelFullInfo> acceptInviteAndJoin(InviteParamBuilder paramBuilder, long selfUid);

以上两个接口有一个比较特殊的错误码:10201 , 参考错误码。建议将回调设置成RequestCallbackWrapper,并通过返回的相应code 去处理相关的逻辑 ,code = 200 为成功。如果用link RequestCallback那么错误码将在link RequestCallback#onFailed(int code)返回。

/**
 * 接受对方的的邀请并加入频道
 */
private void acceptInvite() {
    enableClick(false);    InviteParamBuilder inviteParam = new InviteParamBuilder(invitedEvent.getChannelBaseInfo().getChannelId(),
                                                            invitedEvent.getFromAccountId(),
                                                            invitedEvent.getRequestId());
    NIMClient.getService(SignallingService.class).acceptInviteAndJoin(inviteParam, selfUid).setCallback(
            new RequestCallbackWrapper<ChannelFullInfo>() {

                @Override
                public void onResult(int code, ChannelFullInfo channelFullInfo, Throwable throwable) {
                    enableClick(true);                    //参考官方文档中关于api以及错误码的说明
                    if (code == ResponseCode.RES_SUCCESS) {
                        ToastHelper.showToast(MainActivity.this, "接收邀请成功");
                        channelInfo = channelFullInfo.getChannelBaseInfo();                
                        } else {
                        ToastHelper.showToast(MainActivity.this, "接收邀请返回的结果 , code = " + code +
                                                                 (throwable == null ? "" : ", throwable = " +
                                                                                           throwable.getMessage()));
                    }
                    beInviteContainer.setVisibility(View.GONE);
                }
            });
}

拒绝邀请

    /**
     * 拒绝对方邀请
     * @param paramBuilder InviteParamBuilder {@link InviteParamBuilder}
     * @return InvocationFuture<Void>
     */
    InvocationFuture<Void> rejectInvite(InviteParamBuilder paramBuilder);

以上两个接口有一个比较特殊的错误码:10201 , 参考错误码。建议将回调设置成RequestCallbackWrapper,并通过返回的相应code 去处理相关的逻辑 ,code = 200 为成功。如果用link RequestCallback那么错误码将在link RequestCallback#onFailed(int code)返回。

/**
 * 拒绝对方的邀请
 */
private void rejectInvite(String customInfo) { 
    InviteParamBuilder inviteParam = new InviteParamBuilder(invitedEvent.getChannelBaseInfo().getChannelId(),
                                                            invitedEvent.getFromAccountId(),
                                                            invitedEvent.getRequestId());
    if (!TextUtils.isEmpty(customInfo)) {
        inviteParam.customInfo(customInfo);
    }
    NIMClient.getService(SignallingService.class).rejectInvite(inviteParam);
}

直接呼叫

    /**
     *直接呼叫 , 用于用户新开一个频道并邀请对方加入频道。该接口为组合接口,等同于用户先创建频道,然后加入频道并邀请对方
     * @param callParamBuilder CallParamBuilder {@link CallParamBuilder}
     * @return InvocationFuture<ChannelFullInfo>
     */
    InvocationFuture<ChannelFullInfo> call(CallParamBuilder callParamBuilder)

返回请注意处理相应错误码, 参考错误码。建议将回调设置成RequestCallbackWrapper,并通过返回的相应code 去处理相关的逻辑 ,code = 200 为成功。如果用link RequestCallback那么错误码将在link RequestCallback#onFailed(int code)返回。

/**
 * 呼叫对方
 */
private void call() {
    String account = editAccount.getText().toString().trim();
    if (TextUtils.isEmpty(account)) {
        ToastHelper.showToast(this, "请输入对方帐号");
        return;
    }
    tvCallingHint.setText("正在呼叫" + account);
    tvCallingHint.setVisibility(View.VISIBLE);
    String requestId = String.valueOf(System.currentTimeMillis());
    CallParamBuilder paramBuilder = new CallParamBuilder(ChannelType.VIDEO, account, requestId);
    paramBuilder.selfUid(selfUid);
    NIMClient.getService(SignallingService.class).call(paramBuilder).setCallback(
            new RequestCallbackWrapper<ChannelFullInfo>() {

                @Override
                public void onResult(int code, ChannelFullInfo channelFullInfo, Throwable throwable) {
                    //参考官方文档中关于api以及错误码的说明
                    if (code == ResponseCode.RES_SUCCESS) {
                        channelInfo = channelFullInfo.getChannelBaseInfo();
                        ToastHelper.showToast(MainActivity.this, "邀请成功,等待对方接听");
                    } else {
                        tvCallingHint.setVisibility(View.GONE);
                        ToastHelper.showToast(MainActivity.this, "邀请返回的结果 , code = " + code +
                                                                 (throwable == null ? "" : ", throwable = " +
                                                                                           throwable.getMessage()));
                    }
                }
            });


}
}