For Flutter
NetEase cloud letter SDK
NIMSDK
ability to record and send voice messagesBefore use, first initialize:
void main () async {
FlutterNIM (). init (
appKey : "123456" ,
apnsCername : "ABCDEFG" , // iOS production environment certificate name
apnsCernameDevelop : "ABCDEFG" , // iOS test environment certificate name
imAccount : "123456" ,
imToken : "123456" ,
);
runApp ( MyApp ());
}
Due to SDK limitations, the Android platform still needs to be initialized in Application, you can refer to the following code:
public class MyApplication extends FlutterApplication {
@Override
public void onCreate () {
super . onCreate();
FlutterNIMPreferences . SetContext ( this );
// SDK initialization (start background service, if user login information already exists, SDK will complete automatic login)
NIMClient . Init( this , FlutterNIMPreferences . GetLoginInfo (), buildSDKOptions());
}
// NetEase
Yunxin configuration (you can also use your own configuration here) private SDKOptions buildSDKOptions () {
return FlutterNIMSDKOptionConfig . GetSDKOptions( this , " 123456 " , buildMixPushConfig());
}
// NetEase
Yunxin third-party push configuration private MixPushConfig buildMixPushConfig () {
MixPushConfig config = new MixPushConfig ();
// Xiaomi pushes
config . xmAppId = " 123456 " ;
config . xmAppKey = " 123456 " ;
config . xmCertificateName = " ABCDEFG " ;
// Huawei pushes
config . HwCertificateName = " ABCDEFG " ;
...
return config;
}
}
bool isSuccess = await FlutterNIM (). login (imAccount, imToken);
FlutterNIM (). logout ();
FlutterNIM ().kickReasonResponse. listen (( NIMKickReason reason) {
// Handling the logic of being kicked offline
});
Manually get the list of recent conversations:
FlutterNIM (). loadRecentSessions ();
Listening to recent conversations, manually calling to get the recent conversation list or changes in the recent conversation list (add, update, delete, mark read) will trigger this callback.
FlutterNIM ().recentSessionsResponse. listen ((recentSessions) {
List < NIMSession > _recentSessions = recentSessions;
// Here you can calculate the total unread of recent sessions
int unreadNum = recentSessions
. map ((s) => s.unreadCount)
. fold ( 0 , (curr, next) => curr + next);
});
Delete a session (only delete the local cache, not the cloud record)
FlutterNIM (). deleteRecentSession (session.sessionId);
Start a new conversation
bool isSuccess = await FlutterNIM (). startChat (session.sessionId);
Listen for conversation messages
FlutterNIM ().messagesResponse. listen ((messages) {
List < NIMMessage > _messages = messages;
// Find the remainder, if the result is equal to zero, there may be more data
int _remainder = this ._messages.length % 20 ;
bool _hasMoreData = remainder == 0 ;
});
When you exit the chat page, you need to close the session
FlutterNIM (). exitChat ();
send messages
// Send text message
FlutterNIM (). sendText (content);
// Send image message
FlutterNIM (). sendImage (file.path);
// Send video message
FlutterNIM (). sendVideo (file.path);
The voice message calls the recording capability of the native Yunxin SDK
// Start recording
FlutterNIM (). onStartRecording ();
// End the recording and automatically send a voice message
FlutterNIM (). onStopRecording ();
// Cancel recording, will not send
FlutterNIM (). onCancelRecording ();
Put the custom message content Map
in:
// Feel free to define
Map < String , dynamic > customObject = {
"type" : "Custom" ,
"url" : "xxx" ,
...
};
FlutterNIM (). sendCustomMessage (
customObject,
apnsContent : "[A custom message has been sent]" ,
);
The received custom message body JSON
exists NIMMessage
in the format of a string customMessageContent
, and then parse it by yourself:
final customObjectMap = json. decode (customMessageContent);
// Custom
var model = CustomModel . fromJson (customObjectMap);
This SDK refers to the following author’s work flutter_nim
Author: cls8428181
Source Code: https://github.com/cls8428181/nim_flutter
#flutter #dart #mobile-apps