如何主动向客户端发送自定义消息

更新时间:2025-04-28 05:35:54

本文将为您介绍在消息对话中如何主动向客户端发送自定义消息。

功能描述

在消息对话场景中,服务端主动推送业务信息能够为用户提供及时、精准、个性化的服务体验,减少用户主动查询信息的繁琐操作,节省时间成本。对企业而言,这种方式可提升信息触达率和用户参与度,促进业务目标实现(如提高销售量、增加活跃度),并通过基于用户行为数据和偏好的精准推送,实现精细化运营,提升用户满意度和忠诚度

应用场景

服务端主动推送消息可以应用在电商、教育以及金融等多个领域。以电商场景为例:在用户与电商客服的消息对话过程中,当用户浏览某类商品但未下单时,服务端可推送该商品的限时折扣、库存预警等信息。例如,用户在咨询某品牌手机的性能时,服务端推送该手机当前正在进行的满减活动信息,以及该机型库存仅剩 3 台的预警,刺激用户下单购买。此外,在购物节期间,服务端还能向用户推送专属优惠券、热门商品推荐等内容,引导用户消费。

功能实现

服务端发送自定义消息

SendMessageChatText - 向IM客户端发送消息

package com.aliyun.rtc;

import com.aliyun.tea.*;

public class SampleSendMessagetChatText {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     * 
     * @throws Exception
     */
    public static com.aliyun.ice20201109.Client createClient() throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("yourak")
                // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("yoursk");
        // Endpoint 请参考 https://api.aliyun.com/product/ICE
        config.endpoint = "ice.cn-shanghai.aliyuncs.com";
        return new com.aliyun.ice20201109.Client(config);
    }


    private static void sendMessageChatText() throws Exception {
        com.aliyun.ice20201109.Client client =  createClient();

        com.aliyun.ice20201109.models.SendMessageChatTextRequest request = new com.aliyun.ice20201109.models.SendMessageChatTextRequest()
            .setAIAgentId("youragentid")
            .setSessionId("yoursessionid")
            .setText("yourtext")
            .setReceiverId("receiverid")
            .setType("announcement") // announcement or custom
            .setNeedArchiving(true);
            try {
                com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
                
                client.sendMessageChatTextWithOptions(request, runtime);
            } catch (TeaException error) {
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            } catch (Exception _error) {
                TeaException error = new TeaException(_error.getMessage(), _error);
                System.out.println(error.getMessage());
                com.aliyun.teautil.Common.assertAsString(error.message);
            }   
    }
    
    public static void main(String[] args) throws Exception {
        sendMessageChatText();
    }
}

客户端处理自定义消息

您需要对服务端发送的自定义消息进行解析,示例代码如下:

Android
iOS
Web
//实现ARTCAIChatEngine.IARTCAIChatEngineCallback中的回调

 @Override
public void onReceivedCustomMessage(String  text) {
    // 这里处理收到当前智能体发过来的自定义消息
}

// 创建消息对话engine实例
let engine: ARTCAIChatEngineInterface = {
    return ARTCAICallEngineFactory.createChatEngine()
}()

// 给通话引擎设置回调
self.engine.delegate = self

// 实现回调接口
public func onReceivedCustomMessage(text: String) {
    // 在这里处理自定义消息
    debugPrint("onReceivedAgentCustomMessage:\(text)")
}

engine.on('receivedCustomMessage', (text) => {
  // 在这里处理自定义消息
  console.log('receivedCustomMessage', text);
});
  • 本页导读 (1)
  • 功能描述
  • 应用场景
  • 功能实现
  • 服务端发送自定义消息
  • 客户端处理自定义消息
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

可以解答问题、推荐解决方案等