Message handling API
This document describes how to implement an event listener for the persistent connection message channel of the software development kit (SDK). This listener lets you subscribe to channel connection and disconnection events, and to receive and handle messages.
Message subscription
To receive messages, you can register a listener in the notification center. The listener must listen for the CCPDidReceiveMessageNotification notification. When your application receives a message, the SDK broadcasts this event through the notification center.
Message attributes
Messages are sent through a proprietary Alibaba Cloud channel.
The message is a pass-through message that contains a title and content.
The message does not trigger the device's ringtone or vibration.
Messages are received only when the application is online.
Broadcast events
Broadcast name | Event description |
CCPDidReceiveMessageNotification | The channel received a pass-through message. |
Examples
The following examples show how to subscribe to and handle messages:
// Subscribe to messages
NotificationCenter.default.addObserver(self,
selector: #selector(onMessageReceived(_:)),
name: NSNotification.Name("CCPDidReceiveMessageNotification"),
object: nil)
// Handle received messages
@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)")
}// Subscribe to messages
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onMessageReceived:)
name:@"CCPDidReceiveMessageNotification"
object:nil];
// Handle received messages
- (void)onMessageReceived:(NSNotification *)notification {
NSDictionary *data = [notification object];
NSString *title = data[@"title"];
NSString *content = data[@"content"];
NSLog(@"Receive message title: %@, content: %@.", title, content);
}Listen for message channel status
You can subscribe to channel connection and disconnection events to monitor the message channel status.
Broadcast events
Broadcast name | Event Definition |
CCPDidChannelConnectedSuccess | The channel is connected. |
CCPDidChannelDisconnected | The channel is disconnected. |
Examples
// Register a listener for successful channel connections
NotificationCenter.default.addObserver(self,
selector: #selector(onChannelConnected(_:)),
name: NSNotification.Name("CCPDidChannelConnectedSuccess"),
object: nil)
// Register a listener for channel disconnections
NotificationCenter.default.addObserver(self,
selector: #selector(onChannelDisconnected(_:)),
name: NSNotification.Name("CCPDidChannelDisconnected"),
object: nil)
// Handle successful channel connections
@objc func onChannelConnected(_ notification: Notification) {
print("Channel connected.")
}
// Handle channel disconnections
@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.");
}