A flutter IM plugin for android and ios.
flutter_openim_sdk: ^1.0.4
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
// Initialize SDK
OpenIM.iMManager
..initSDK(
platform: IMPlatform.ios,
ipApi: 'Api interface address',
ipWs: 'WebSocket address',
dbPath: 'Database directory',
listener: OnInitSDKListener(
connecting: () {},
connectFailed: (code, error) {},
connectSuccess: () {},
kickedOffline: () {},
userSigExpired: () {},
selfInfoUpdated: (user) {},
),
)
// Add message listener (remove when not in use)
..messageManager.addAdvancedMsgListener(OnAdvancedMsgListener(
recvMessageRevoked: (id) {},
recvC2CReadReceipt: (list) {},
recvNewMessage: (msg) {},
))
// Set up message sending progress listener
..messageManager.setMsgSendProgressListener(OnMsgSendProgressListener(
progressCallback: (id, progress) {},
))
// Set up friend relationship listener
..friendshipManager.setFriendshipListener(OnFriendshipListener(
blackListAdd: (u) {},
blackListDeleted: (u) {},
friendApplicationListAccept: (u) {},
friendApplicationListAdded: (u) {},
friendApplicationListDeleted: (u) {},
friendApplicationListReject: (u) {},
friendInfoChanged: (u) {},
friendListAdded: (u) {},
friendListDeleted: (u) {},
))
// Set up conversation listener
..conversationManager.setConversationListener(OnConversationListener(
conversationChanged: (list) {},
newConversation: (list) {},
totalUnreadMsgCountChanged: (count) {},
syncServerFailed: () {},
syncServerFinish: () {},
syncServerStart: () {},
))
// Set up group listener
..groupManager.setGroupListener(OnGroupListener(
applicationProcessed: (groupId, opUser, agreeOrReject, opReason) {},
groupCreated: (groupId) {},
groupInfoChanged: (groupId, info) {},
memberEnter: (groupId, list) {},
memberInvited: (groupId, opUser, list) {},
memberKicked: (groupId, opUser, list) {},
memberLeave: (groupId, info) {},
receiveJoinApplication: (groupId, info, opReason) {},
));
OpenIM.iMManager.login(uid: uid, token: token).then((value){
// login successful
});
OpenIM.iMManager.conversationManager.getAllConversationList().then((list) {
// Return to the conversation list
});
OpenIM.iMManager.friendshipManager.getFriendList().then((list) {
// Return to friends list
});
List<Message> chatMsgList = List.empty(growable: true);
/// Custom message listener, the arrival of a new message will trigger the onNewMessage method callback
class CustomAdvancedMsgListener extends AdvancedMsgListener {
final ValueChanged<Message>? onNewMessage;
CustomAdvancedMsgListener({
this.onNewMessage,
});
@override
void onRecvNewMessage(Message msg) {
if (null != onNewMessage) onNewMessage!(msg);
}
}
// If userID is not null and groupID is null, get a single chat message list
// If the userID is null and the groupID is not null, get a group chat message list
OpenIM.iMManager.messageManager.getHistoryMessageList(
userID: uid,//User id
startMsg: startMsg,//Last message, take chatMsgList[0]
groupID: gid,//Group id
count: 12,//Page Size
)
.then((list) => chatMsgList.addAll(list));
// Create a message listener
// After entering the chat page, you need to listen for new messages and then render the UI
// Each chat window has a unique uid (user id) or gid (group id)
// Determine whether the received message belongs to the current window according to uid or gid
var msgListener = CustomAdvancedMsgListener(onNewMessage: (message) {
// If the sender user id is equal to the user id of the current chat page,
// or the group id is equal to the current group id of the current chat page
if (message.sendID == uid || message.groupID == gid) {
if (!chatMsgList.contains(message)) {
// New message in current chat page
chatMsgList.add(event.message);
}
}
});
// Add message listener
OpenIM.iMManager.messageManager.addAdvancedMsgListener(msgListener);
// Remove message monitoring
// After adding a message listener, if the page disposes, please remove the listener
// OpenIM.iMManager.messageManager.removeAdvancedMsgListener(msgListener);
// Create message
var message = await OpenIM.iMManager.messageManager.createTextMessage(
text: 'I am the content of the message',
);
// Send
OpenIM.iMManager.messageManager.sendMessage(
message: message,
onlineUserOnly: false,
userID: uid, // Single chat value is not null
groupID: gid, // The group chat value is not null
).then((v) {
// Sent successfully
}).catchError((e){
// Failed to send
});
OpenIM.iMManager.logout();