Cancel Media Message Sending
Feature Overview
The ZIM SDK supports canceling messages containing local media files that are currently being sent. After a successful cancellation, the message status will change to "Failed to Send".
Note
- Supported from version 2.22.0.
- Only messages containing local media files of the following message types can be canceled:
Multiple(10)
,Image(11)
,File(12)
,Audio(13)
,Video(14)
.
Cancel the Media Message Being Sent
When a user sends a message containing a local media file, due to the time required for file upload, the file upload can be terminated by calling cancelSendingMessage during this period.
Note
When a multi item messages containing both ordinary text and media files is canceled, the entire message (including ordinary text) will be canceled.
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onMessageSentStatusChanged(ZIM zim, ArrayList<ZIMMessageSentStatusChangeInfo> infos) {
// Message sent status changed
}
})
// User A sends a message, taking a single-chat file message as an example
String conversationID = "xxx" ; // Conversation ID
ZIMFileMessage message = new ZIMFileMessage("/storage/emulated/0/Android/data/packagename/picture/xxx.zip");
ZIMMessageSendConfig sendConfig = new ZIMMessageSendConfig();
zim.sendMessage(message, conversationID, ZIMConversationType.PEER, sendConfig, new ZIMMessageSentCallback() {
@Override
public void onMessageAttached(ZIMMessage message) {}
@Override
public void onMediaUploadingProgress(ZIMMediaMessage message, long currentFileSize, long totalFileSize) {
// Cancel sending when the file is being uploaded
zim.cancelSendingMessage(message, new ZIMSendingMessageCancelConfig(), new ZIMSendingMessageCancelledCallback() {
@Override
public void onSendingMessageCancelled(ZIMError errorInfo) {}
});
}
});
zim.setEventHandler(new ZIMEventHandler() {
@Override
public void onMessageSentStatusChanged(ZIM zim, ArrayList<ZIMMessageSentStatusChangeInfo> infos) {
// Message sent status changed
}
})
// User A sends a message, taking a single-chat file message as an example
String conversationID = "xxx" ; // Conversation ID
ZIMFileMessage message = new ZIMFileMessage("/storage/emulated/0/Android/data/packagename/picture/xxx.zip");
ZIMMessageSendConfig sendConfig = new ZIMMessageSendConfig();
zim.sendMessage(message, conversationID, ZIMConversationType.PEER, sendConfig, new ZIMMessageSentCallback() {
@Override
public void onMessageAttached(ZIMMessage message) {}
@Override
public void onMediaUploadingProgress(ZIMMediaMessage message, long currentFileSize, long totalFileSize) {
// Cancel sending when the file is being uploaded
zim.cancelSendingMessage(message, new ZIMSendingMessageCancelConfig(), new ZIMSendingMessageCancelledCallback() {
@Override
public void onSendingMessageCancelled(ZIMError errorInfo) {}
});
}
});
- (void)zim:(ZIM *)zim messageSentStatusChanged:(NSArray<ZIMMessageSentStatusChangeInfo *> *)infos{
// Message sent status changed
}
NSString *conversationID = @"xxx" ; // Conversation ID
// User A sends a message, taking a single-chat file message as an example
ZIMFileMessage *message = [[ZIMFileMessage alloc] init];
//Enter the local path in UTF-8 format
//Here, the address of a file selected by the file selector is used as an example
message.fileLocalPath = @"/private/var/mobile/Containers/Shared/AppGroup/D5144D14-3FE8-4C6C-8527-01F368B8E49E/File Provider Storage/xxx.zip";
ZIMMessageSendConfig *sendConfig = [[ZIMMessageSendConfig alloc]init];
ZIMMessageSendNotification *notification = [[ZIMMessageSendNotification alloc] init];
notification.onMessageAttached = ^(ZIMMessage * _Nonnull message) {
// Developers can listen to this callback to execute the business logic before message sending
};
notification.onMediaUploadingProgress = ^(ZIMMediaMessage * _Nonnull message, unsigned long long currentFileSize, unsigned long long totalFileSize) {
// Cancel sending when the file is being uploaded
ZIMSendingMessageCancelConfig *cancelConfig = [[ZIMSendingMessageCancelConfig alloc]init];
[[ZIM getInstance] cancelSendingMessage:message config:cancelConfig callback:^(ZIMError * _Nonnull errorInfo) {
}];
};
[[ZIM getInstance] sendMessage:message toConversationID:conversationID conversationType:ZIMConversationTypePeer config:sendConfig notification:notification callback:^(ZIMMessage * _Nonnull message, ZIMError * _Nonnull errorInfo) {
}];
- (void)zim:(ZIM *)zim messageSentStatusChanged:(NSArray<ZIMMessageSentStatusChangeInfo *> *)infos{
// Message sent status changed
}
NSString *conversationID = @"xxx" ; // Conversation ID
// User A sends a message, taking a single-chat file message as an example
ZIMFileMessage *message = [[ZIMFileMessage alloc] init];
//Enter the local path in UTF-8 format
//Here, the address of a file selected by the file selector is used as an example
message.fileLocalPath = @"/private/var/mobile/Containers/Shared/AppGroup/D5144D14-3FE8-4C6C-8527-01F368B8E49E/File Provider Storage/xxx.zip";
ZIMMessageSendConfig *sendConfig = [[ZIMMessageSendConfig alloc]init];
ZIMMessageSendNotification *notification = [[ZIMMessageSendNotification alloc] init];
notification.onMessageAttached = ^(ZIMMessage * _Nonnull message) {
// Developers can listen to this callback to execute the business logic before message sending
};
notification.onMediaUploadingProgress = ^(ZIMMediaMessage * _Nonnull message, unsigned long long currentFileSize, unsigned long long totalFileSize) {
// Cancel sending when the file is being uploaded
ZIMSendingMessageCancelConfig *cancelConfig = [[ZIMSendingMessageCancelConfig alloc]init];
[[ZIM getInstance] cancelSendingMessage:message config:cancelConfig callback:^(ZIMError * _Nonnull errorInfo) {
}];
};
[[ZIM getInstance] sendMessage:message toConversationID:conversationID conversationType:ZIMConversationTypePeer config:sendConfig notification:notification callback:^(ZIMMessage * _Nonnull message, ZIMError * _Nonnull errorInfo) {
}];
void onMessageSentStatusChanged(zim::ZIM *zim, const std::vector<ZIMMessageSentStatusChangeInfo> &messageSentStatusChangeInfoList{
// Message sent status changed
}
// User A sends a message, taking a single-chat file message as an example
zim::ZIMMediaMessage *message = nullptr;
auto fileMessage = zim::ZIMFileMessage();
// Enter the local path in UTF-8 format
fileMessage.fileLocalPath = "D:\\file\\files.zip";
// If a network URL is entered here, the SDK will pass through the path without ZIM background service processing. If both the network URL and the local path are entered, the SDK will prioritize the network URL that you want to use.
fileMessage.fileDownloadUrl = "";
zim::ZIMMessageSendConfig sendConfig;
sendConfig.priority = zim::ZIM_MESSAGE_PRIORITY_MEDIUM;
message = &fileMessage;
auto notification = std::make_shared<zim::ZIMMessageSendNotification>(
[=](const std::shared_ptr<zim::ZIMMessage> &message) {
// Developers can listen to this callback to execute the business logic before message sending
},
[=](const std::shared_ptr<zim::ZIMMediaMessage> &message,
unsigned long long currentFileSize,
unsigned long long totalFileSize) {
// Cancel sending when the file is being uploaded
zim::ZIMSendingMessageCancelConfig cancelConfig;
zim_->cancelSendingMessage(message, cancelConfig, [=](const zim::ZIMError &errorInfo) {
});
});
zim_->sendMessage(message, receiver_id, zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER, sendConfig, notification,
[=](const std::shared_ptr<zim::ZIMMessage> &message, const zim::ZIMError &errorInfo) {
});
void onMessageSentStatusChanged(zim::ZIM *zim, const std::vector<ZIMMessageSentStatusChangeInfo> &messageSentStatusChangeInfoList{
// Message sent status changed
}
// User A sends a message, taking a single-chat file message as an example
zim::ZIMMediaMessage *message = nullptr;
auto fileMessage = zim::ZIMFileMessage();
// Enter the local path in UTF-8 format
fileMessage.fileLocalPath = "D:\\file\\files.zip";
// If a network URL is entered here, the SDK will pass through the path without ZIM background service processing. If both the network URL and the local path are entered, the SDK will prioritize the network URL that you want to use.
fileMessage.fileDownloadUrl = "";
zim::ZIMMessageSendConfig sendConfig;
sendConfig.priority = zim::ZIM_MESSAGE_PRIORITY_MEDIUM;
message = &fileMessage;
auto notification = std::make_shared<zim::ZIMMessageSendNotification>(
[=](const std::shared_ptr<zim::ZIMMessage> &message) {
// Developers can listen to this callback to execute the business logic before message sending
},
[=](const std::shared_ptr<zim::ZIMMediaMessage> &message,
unsigned long long currentFileSize,
unsigned long long totalFileSize) {
// Cancel sending when the file is being uploaded
zim::ZIMSendingMessageCancelConfig cancelConfig;
zim_->cancelSendingMessage(message, cancelConfig, [=](const zim::ZIMError &errorInfo) {
});
});
zim_->sendMessage(message, receiver_id, zim::ZIMConversationType::ZIM_CONVERSATION_TYPE_PEER, sendConfig, notification,
[=](const std::shared_ptr<zim::ZIMMessage> &message, const zim::ZIMError &errorInfo) {
});
ZIMEventHandler.onMessageSentStatusChanged = (ZIM zim, List<ZIMMessageSentStatusChangeInfo> messageSentStatusChangedInfoList) {
// Message sent status changed
};
// User A sends a message, taking a single-chat file message as an example
String conversationID = "xxx" ; // Conversation ID
ZIMFileMessage message = ZIMFileMessage('xxx/xxx.zip');
ZIMMessageSendConfig sendConfig = ZIMMessageSendConfig();
ZIMMessageSendNotification notification = ZIMMessageSendNotification(onMessageAttached: (message){
// Developers can listen to this callback to execute the business logic before message sending
},onMediaUploadingProgress: (message,currentFileSize,totalFileSize){
// Cancel sending when the file is being uploaded
ZIM.getInstance()!.cancelSendingMessage(message, ZIMSendingMessageCancelConfig())
.then(() => {
//Successfully trigger here
})
.catchError((onError) {
//Failed to trigger here
});
});
ZIM.getInstance()!.sendMessage(
message,
conversationID,
ZIMConversationType.peer,
ZIMMessageSendConfig(), notification)
.then((value) => {
//Successfully trigger here
})
.catchError((onError) {
//Failed to trigger here
});
ZIMEventHandler.onMessageSentStatusChanged = (ZIM zim, List<ZIMMessageSentStatusChangeInfo> messageSentStatusChangedInfoList) {
// Message sent status changed
};
// User A sends a message, taking a single-chat file message as an example
String conversationID = "xxx" ; // Conversation ID
ZIMFileMessage message = ZIMFileMessage('xxx/xxx.zip');
ZIMMessageSendConfig sendConfig = ZIMMessageSendConfig();
ZIMMessageSendNotification notification = ZIMMessageSendNotification(onMessageAttached: (message){
// Developers can listen to this callback to execute the business logic before message sending
},onMediaUploadingProgress: (message,currentFileSize,totalFileSize){
// Cancel sending when the file is being uploaded
ZIM.getInstance()!.cancelSendingMessage(message, ZIMSendingMessageCancelConfig())
.then(() => {
//Successfully trigger here
})
.catchError((onError) {
//Failed to trigger here
});
});
ZIM.getInstance()!.sendMessage(
message,
conversationID,
ZIMConversationType.peer,
ZIMMessageSendConfig(), notification)
.then((value) => {
//Successfully trigger here
})
.catchError((onError) {
//Failed to trigger here
});
zim.on('messageSentStatusChanged', (zim: ZIM, data: ZIMEventOfMessageSentStatusChangedResult) => {
// Message sent status changed
});
zim.on('messageSentStatusChanged', (zim: ZIM, data: ZIMEventOfMessageSentStatusChangedResult) => {
// Message sent status changed
});
// User A sends a message, taking a single-chat file message as an example
const userID_A = "xxxx" ; // User A's ID
const userID_B = "xxxx" ; // User B's ID
const messageObj: ZIMFileMessage = { type: 12, fileLocalPath: file }
const config: ZIMMessageSendConfig = {
priority: 1, // Message priority, the value is 1 for low, 2 for medium, and 3 for high
}
const notification: ZIMMessageSendNotification = {
onMessageAttached: (message: ZIMMessage) => {
// todo: Loading
},
onMediaUploadingProgress: (message: ZIMMediaMessage, currentFileSize: number, totalFileSize: number) {
// Cancel sending when the file is being uploaded
zim.cancelSendingMessage(message, {} as ZIMSendingMessageCancelConfig)
.then((res: ZIMMessageSentResult) => {
// Operation successful
})
.catch((err: ZIMError) => {
// Operation failed
});
}
}
zim.sendMessage(messageObj, userID_B, 0, config, notification)
.then((res: ZIMMessageSentResult) => {
// Sent successfully
})
.catch((err: ZIMError) => {
// Failed to send
});
// User A sends a message, taking a single-chat file message as an example
const userID_A = "xxxx" ; // User A's ID
const userID_B = "xxxx" ; // User B's ID
const messageObj: ZIMFileMessage = { type: 12, fileLocalPath: file }
const config: ZIMMessageSendConfig = {
priority: 1, // Message priority, the value is 1 for low, 2 for medium, and 3 for high
}
const notification: ZIMMessageSendNotification = {
onMessageAttached: (message: ZIMMessage) => {
// todo: Loading
},
onMediaUploadingProgress: (message: ZIMMediaMessage, currentFileSize: number, totalFileSize: number) {
// Cancel sending when the file is being uploaded
zim.cancelSendingMessage(message, {} as ZIMSendingMessageCancelConfig)
.then((res: ZIMMessageSentResult) => {
// Operation successful
})
.catch((err: ZIMError) => {
// Operation failed
});
}
}
zim.sendMessage(messageObj, userID_B, 0, config, notification)
.then((res: ZIMMessageSentResult) => {
// Sent successfully
})
.catch((err: ZIMError) => {
// Failed to send
});