sendMessage
Feature Introduction
Description
Send a message.
Note
You must first call createXXXMessage to create a message. Messages created with createXXXMessageByURL need to be sent using sendMessageNotOss. Related Callbacks: onNewConversation onConversationChanged onRecvNewMessage onRecvNewMessages
- iOS
- Android
- Flutter
- uni-app
- Browser/Electron/MiniProgram
- React-Native
- Unity
Function Prototype
Future<Message> sendMessage({
required Message message,
required OfflinePushInfo offlinePushInfo,
String? userID,
String? groupID,
String? operationID,
})
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
message | Message | Yes | Message body |
userID | String | No | Receiver's user ID |
groupID | String | No | Receiver's group ID |
offlinePushInfo | OfflinePushInfo | No | Offline message body |
Return Result
Name | Type | Description |
---|---|---|
~ | Message | Return on success |
Code Example
Message msg = await OpenIM.iMManager.messageManager.sendMessage(message: message, offlinePushInfo: offlinePushInfo);
// todo
Function Prototype
- (void)sendMessage:(OIMMessageInfo *)message
recvID:(NSString * _Nullable)recvID
groupID:(NSString * _Nullable)groupID
offlinePushInfo:(OIMOfflinePushInfo * _Nullable)offlinePushInfo
onSuccess:(nullable OIMMessageInfoCallback)onSuccess
onProgress:(nullable OIMNumberCallback)onProgress
onFailure:(nullable OIMFailureCallback)onFailure;
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
message | OIMMessageInfo | Yes | Message body |
recvID | NSString | No | Receiver's user ID |
groupID | NSString | No | Receiver's group ID |
offlinePushInfo | OIMOfflinePushInfo | No | Offline message body |
Return Result
Name | Type | Description |
---|---|---|
onSuccess | OIMMessageInfo | Return on success |
onProgress | NSInteger | Send progress |
onFailure | OIMFailureCallback | Return on failure |
Code Example
OIMMessageInfo *testMessage = [OIMMessageInfo createTextMessage:@"test"];
[OIMManager.manager sendMessage:testMessage
recvID:@""
groupID:@""
offlinePushInfo:nil
onSuccess:^(OIMMessageInfo * _Nullable message) {
} onProgress:^(NSInteger number) {
} onFailure:^(NSInteger code, NSString * _Nullable msg) {
}];
Function Prototype
public void sendMessage(OnMsgSendCallback base, Message message, String recvUid, String recvGid, OfflinePushInfo offlinePushInfo)
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
base | OnMsgSendCallback | Yes | callback |
message | Message | Yes | Message body |
userID | String | No | Receiver's user ID |
groupID | String | No | Receiver's group ID |
offlinePushInfo | OfflinePushInfo | No | Offline message body |
Return Result
Name | Type | Description |
---|---|---|
~ | Message | Return on success |
Code Example
OpenIMClient.getInstance().messageManager.sendMessage( new OnMsgSendCallback(){
public void onError(int code, String error) {
//Send failed
}
public void onProgress(long progress) {
//Sending progress
}
public void onSuccess(Message s) {
//Send successful
}
}, message, recvUid, recvGid, offlinePushInfo);
Function Prototype
IMSDK.sendMessage({
recvID: string;
groupID: string;
offlinePushInfo?: OfflinePush;
message: MessageItem;
}, operationID?: string): Promise<WsResponse<MessageItem>>
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
message | MessageItem | Yes | Message body |
recvID | string | Yes | Receiver's user ID, send empty string for group |
groupID | string | Yes | Receiver's group ID, send empty string for chat |
offlinePushInfo | OfflinePushInfo | No | Offline message body |
Return Result
Parameter Name | Parameter Type | Description |
---|---|---|
Promise.then() | Promise<WsResponse<MessageItem>> | Success callback |
Promise.catch() | Promise<WsResponse> | Failure callback |
Code Example
import { getSDK } from '@openim/wasm-client-sdk';
const IMSDK = getSDK();
// use in electron with ffi
// import { getWithRenderProcess } from '@openim/electron-client-sdk/lib/render';
// const { instance: IMSDK } = getWithRenderProcess();
// use in mini program
// import { OpenIMSDK } from 'open-im-sdk';
// const IMSDK = new OpenIMSDK();
IMSDK.sendMessage({
recvID: "userID",
groupID: "",
message: {
// MessageItem
...
}
})
.then(({ data }) => {
// Successful call
})
.catch(({ errCode, errMsg }) => {
// Failed call
});
Function Prototype
IMSDK.asyncApi('sendMessage', operationID: string, {
recvID: string;
groupID: string;
offlinePushInfo?: OfflinePush;
message: MessageItem;
}): Promise<void>
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
operationID | string | Yes | Operation ID, used for troubleshooting, must be unique, suggest using current time and a random number |
message | MessageItem | Yes | Message body |
recvID | string | Yes | User ID to receive the message, pass an empty string when sending a group message |
groupID | string | Yes | Group ID to receive the message, pass an empty string when sending a private chat |
offlinePushInfo | OfflinePushInfo | Yes | Offline message body |
Return Result
Using the
openim-uniapp-polyfill
package makes the function Promise-based, when calling you need to usethen
andcatch
to determine and handle success and failure callbacks.
Parameter Name | Parameter Type | Description |
---|---|---|
Promise.then() | Promise<void> | Successful callback |
Promise.catch() | Promise<CatchResponse> | Failed callback |
Code Example
import IMSDK from 'openim-uniapp-polyfill';
IMSDK.asyncApi('sendMessage', IMSDK.uuid(), {
recvID: "userID",
groupID: "",
message: {
// MessageItem
...
},
offlinePushInfo: {
title: 'you have a new message',
desc: 'new message',
ex: '',
iOSPushSound: '+1',
iOSBadgeCount: true,
}
})
.then((data) => {
// Successful call
})
.catch(({ errCode, errMsg }) => {
// Failed call
});
Function Prototype
OpenIMSDKRN.sendMessage({
recvID: string,
groupID: string,
offlinePushInfo?: OfflinePush,
message: MessageItem,
}, operationID: string): Promise<void>
Input Parameters
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
operationID | string | Yes | Operation ID, used for troubleshooting, must be unique, suggest using current time and a random number |
message | MessageItem | Yes | Message body |
recvID | string | Yes | User ID to receive the message, pass an empty string when sending a group message |
groupID | string | Yes | Group ID to receive the message, pass an empty string when sending a private chat |
offlinePushInfo | OfflinePushInfo | Yes | Offline message body |
Return Result
Parameter Name | Parameter Type | Description |
---|---|---|
Promise.then() | Promise<void> | Callback on successful call |
Promise.catch() | Promise<CatchResponse> | Callback on failed call |
Code Example
import OpenIMSDKRN from "open-im-sdk-rn";
OpenIMSDKRN.sendMessage({
recvID: "userID",
groupID: "",
message: {
// MessageItem
...
},
offlinePushInfo: {
title: 'you have a new message',
desc: 'new message',
ex: '',
iOSPushSound: '+1',
iOSBadgeCount: true,
}
}, 'operationID')
.then((data) => {
// Successful call
})
.catch(({ errCode, errMsg }) => {
// Failed call
});
Function Prototype
public static void SendMessage(IMsgSendCallBack cb, Message message, string recvId, string groupId, OfflinePushInfo offlinePushInfo,bool isOnlineOnly)
Input Parameter
Parameter Name | Parameter Type | Mandatory | Description |
---|---|---|---|
cb | IMsgSendCallBack | Yes | callback |
message | Message | Yes | Message body |
userId | string | No | User ID to receive the message, pass an empty string when sending a group message |
groupId | string | No | Group ID to receive the message, pass an empty string when sending a private chat |
offlinePushInfo | OfflinePushInfo | No | Offline message body |
isOnlineOnly | bool | Yes | The receiver must be online to receive it, otherwise it will be lost. |
Return Result
Code Example
var callback = new MsgSendCallBack()
IMSDK.SendMessage(callback,msg ,recvId,groupId,new OfflinePushInfo(){},false);