消息处理接口

更新时间:2025-03-25 11:59:39

本文档介绍如何实现SDK长连接消息通道的事件监听,包括消息通道的连接、断开事件的订阅以及消息的接收和处理。

消息订阅

要接收消息,需要在通知中心注册一个监听器,监听名为CCPDidReceiveMessageNotification的通知。当应用内收到消息时,SDK将通过通知中心广播此事件。

消息特性

  • 消息通过阿里云自有通道发送。

  • 消息为一条包含标题和内容的透传信息。

  • 不会触发设备的铃声或震动。

  • 只有在应用在线时才能接收消息。

广播事件

广播名称

事件含义

CCPDidReceiveMessageNotification

通道收到透传消息

使用示例

以下订阅并处理接收到的消息代码示例:

Swift
Object C
// 订阅消息
NotificationCenter.default.addObserver(self, 
                                     selector: #selector(onMessageReceived(_:)), 
                                     name: NSNotification.Name("CCPDidReceiveMessageNotification"), 
                                     object: nil)

// 消息接收处理方法
@objc func onMessageReceived(_ notification: Notification) {
    guard let data = notification.object as? [String: Any],
          let title = data["title"] as? String,
          let content = data["content"] as? String else {
        return
    }
    print("Receive message title: \(title), content: \(content)")
}
// 订阅消息
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onMessageReceived:)
                                             name:@"CCPDidReceiveMessageNotification"
                                           object:nil];

// 消息接收处理方法
- (void)onMessageReceived:(NSNotification *)notification {
    NSDictionary *data = [notification object];
    NSString *title = data[@"title"];
    NSString *content = data[@"content"];
    
    NSLog(@"Receive message title: %@, content: %@.", title, content);
}

消息通道状态监听

通过订阅通道连接和断开的事件,获取消息通道的状态变化。

广播事件

广播名称

事件含义

CCPDidChannelConnectedSuccess

通道已建连

CCPDidChannelDisconnected

通道已断开

使用示例

Swift
Object C
// 注册通道连接成功通知监听器
NotificationCenter.default.addObserver(self,
                                     selector: #selector(onChannelConnected(_:)),
                                     name: NSNotification.Name("CCPDidChannelConnectedSuccess"),
                                     object: nil)

// 注册通道断开通知监听器  
NotificationCenter.default.addObserver(self,
                                     selector: #selector(onChannelDisconnected(_:)),
                                     name: NSNotification.Name("CCPDidChannelDisconnected"),
                                     object: nil)

                                     
// 通道连接成功处理方法                                     
@objc func onChannelConnected(_ notification: Notification) {
    print("Channel connected.")
}

// 通道断开处理方法
@objc func onChannelDisconnected(_ notification: Notification) {
    print("Channel disconnected.")
}
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onChannelConnected:)
                                             name:@"CCPDidChannelConnectedSuccess"
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(onChannelDisconnected:)
                                             name:@"CCPDidChannelDisconnected"
                                           object:nil];

- (void)onChannelConnected:(NSNotification *)notification {
    NSLog(@"Channel connected.");
}

- (void)onChannelDisconnected:(NSNotification *)notification {
    NSLog(@"Channel disconnected.");
}
  • 本页导读 (0)
  • 消息订阅
  • 消息特性
  • 广播事件
  • 使用示例
  • 消息通道状态监听
  • 广播事件
  • 使用示例
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

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