# Conflicts: # .idea/libraries/Dart_SDK.xml # android/build.gradle # android/gradle/wrapper/gradle-wrapper.properties # android/src/main/AndroidManifest.xml # example/android/app/build.gradle # example/android/build.gradle # example/android/settings.gradle # example/lib/main.dart # example/pubspec.lock # ios/flutter_openim_sdk.podspec # lib/src/manager/im_conversation_manager.dart # lib/src/manager/im_friendship_manager.dart # lib/src/models/notification_info.dart
238 lines
7.9 KiB
Dart
238 lines
7.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
|
|
import 'package:flutter_openim_sdk/src/enum/group_notify_filter.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
void main() {
|
|
try {
|
|
runApp(const MyApp());
|
|
} catch (e, stackTrace) {
|
|
print('Error during app startup: $e');
|
|
print('Stack trace: $stackTrace');
|
|
}
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
bool _isLoading = true;
|
|
bool _isLoggedIn = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_init();
|
|
}
|
|
|
|
Future<void> _init() async {
|
|
await _requestPermissions();
|
|
await _initSDK();
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
|
|
Future<void> _requestPermissions() async {
|
|
// 请求存储权限
|
|
var status = await Permission.storage.request();
|
|
if (status.isGranted) {
|
|
// 权限已授予,可以读写文件
|
|
}
|
|
}
|
|
|
|
Future<void> _initSDK() async {
|
|
var directory = await getApplicationDocumentsDirectory();
|
|
|
|
final rootPath = directory.path;
|
|
OpenIM.iMManager
|
|
.initSDK(
|
|
platformID: 2,
|
|
apiAddr: 'http://192.168.77.135:10002',
|
|
wsAddr: 'ws://192.168.77.135:10001',
|
|
dataDir: '$rootPath/',
|
|
listener: OnConnectListener(onConnectSuccess: () {
|
|
// Already connected to the server
|
|
_isLoggedIn = true;
|
|
}, onConnecting: () {
|
|
// Connecting to the server, can be used for UI prompts
|
|
}, onUserTokenExpired: () {
|
|
// User token has expired, can be used for UI prompts
|
|
_isLoggedIn = false;
|
|
}, onKickedOffline: () {
|
|
// The current user is kicked offline, and the user needs to be prompted to log in again
|
|
_isLoggedIn = false;
|
|
}))
|
|
.then((value) {
|
|
if (value) {
|
|
OpenIM.iMManager.userManager.setUserListener(OnUserListener(
|
|
onSelfInfoUpdated: (userInfo) {
|
|
debugPrint('onSelfInfoUpdated: ${userInfo.toJson()}');
|
|
},
|
|
));
|
|
|
|
OpenIM.iMManager.setNotificationVisibilityRule(
|
|
notificationType: MessageType.memberQuitNotification,
|
|
visibilityType:
|
|
GroupNotifyFilter.notificationVisibleToOperatorAndAdmin);
|
|
|
|
OpenIM.iMManager.setNotificationVisibilityRule(
|
|
notificationType: MessageType.memberKickedNotification,
|
|
visibilityType:
|
|
GroupNotifyFilter.notificationVisibleToOperatorAndAdmin);
|
|
|
|
// success
|
|
} else {
|
|
// fail
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _login() async {
|
|
try {
|
|
await OpenIM.iMManager
|
|
.login(
|
|
userID: "8bfe13b5eac44e87963652abb91d80d2",
|
|
token:
|
|
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiI4YmZlMTNiNWVhYzQ0ZTg3OTYzNjUyYWJiOTFkODBkMiIsIlBsYXRmb3JtSUQiOjIsImV4cCI6MTc3MTQwNzc2MCwiaWF0IjoxNzYzNjMxNzU1fQ.Da8DBofn085JsQlnSHfDqakH6puabZn7A2NAsUs9FoM")
|
|
.then((value) {
|
|
_isLoggedIn = true;
|
|
List<String> list = [];
|
|
OpenIM.iMManager.userManager.subscribeUsersStatus(list);
|
|
//send();
|
|
|
|
OpenIM.iMManager.messageManager
|
|
.setAdvancedMsgListener(OnAdvancedMsgListener(
|
|
// 当消息被撤回时调用
|
|
onRecvNewMessage: (msg) {
|
|
debugPrint('Received onRecvNewMessage: ${msg.toJson()}');
|
|
},
|
|
onRecvOnlineOnlyMessage: (msg) =>
|
|
debugPrint('Received online-only message: ${msg.toJson()}'),
|
|
onRecvOfflineNewMessage: (msg) =>
|
|
debugPrint('Received offline message: ${msg.toJson()}'),
|
|
));
|
|
|
|
OpenIM.iMManager.groupManager.setGroupListener(OnGroupListener(
|
|
// 当群组申请被接受时调用
|
|
onGroupApplicationAccepted: (groupApplication) {},
|
|
// 当群组申请被添加时调用
|
|
onGroupApplicationAdded: (groupApplication) {},
|
|
// 当群组申请被删除时调用
|
|
onGroupApplicationDeleted: (groupApplication) {},
|
|
// 当群组申请被拒绝时调用
|
|
onGroupApplicationRejected: (groupApplication) {
|
|
debugPrint('Group application rejected: $groupApplication');
|
|
},
|
|
// 当群组信息发生变化时调用
|
|
onGroupInfoChanged: (groupInfo) {
|
|
debugPrint('Group info changed: $groupInfo');
|
|
//等待一段时间,获得会话的最后一条消息
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
OpenIM.iMManager.conversationManager
|
|
.getOneConversation(
|
|
sourceID: groupInfo.groupID,
|
|
sessionType: ConversationType.superGroup)
|
|
.then((conversation) {
|
|
debugPrint(
|
|
'Updated conversation last message: ${conversation.latestMsg}');
|
|
});
|
|
});
|
|
},
|
|
// 当群组成员被添加时调用
|
|
onGroupMemberAdded: (groupMember) {
|
|
debugPrint('Group member added: $groupMember');
|
|
},
|
|
// 当群组成员被删除时调用
|
|
onGroupMemberDeleted: (groupMember) {
|
|
debugPrint('Group member deleted: $groupMember');
|
|
},
|
|
// 当群组成员信息发生变化时调用
|
|
onGroupMemberInfoChanged: (groupMember) {
|
|
debugPrint('Group member info changed: $groupMember');
|
|
},
|
|
// 当加入的群组被添加时调用
|
|
onJoinedGroupAdded: (groupInfo) {
|
|
debugPrint('Joined group added: $groupInfo');
|
|
},
|
|
// 当加入的群组被删除时调用
|
|
onJoinedGroupDeleted: (groupInfo) {
|
|
debugPrint('Joined group deleted: $groupInfo');
|
|
},
|
|
));
|
|
OpenIM.iMManager.conversationManager
|
|
.getAllConversationList()
|
|
.then((value) {
|
|
print('Get all conversation list successful');
|
|
});
|
|
});
|
|
} catch (error) {
|
|
_isLoggedIn = false;
|
|
} finally {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> send() async {
|
|
OpenIM.iMManager.messageManager
|
|
.sendMessage(
|
|
userID: "724f91aceb434a28a1cd10f5564b2c68",
|
|
message: await OpenIM.iMManager.messageManager
|
|
.createTextMessage(text: 'hello openim'),
|
|
offlinePushInfo: OfflinePushInfo(title: 'title', desc: 'desc'))
|
|
.then((value) {
|
|
print('send success');
|
|
}).catchError((error) {
|
|
print('send error');
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MaterialApp(
|
|
home: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Plugin example app'),
|
|
),
|
|
body: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _isLoggedIn
|
|
? Column(
|
|
children: [
|
|
TextButton(
|
|
onPressed: () {
|
|
OpenIM.iMManager.logout().then((_) {
|
|
setState(() {
|
|
_isLoggedIn = false;
|
|
});
|
|
});
|
|
},
|
|
child: const Text('logout')),
|
|
],
|
|
)
|
|
: Center(
|
|
child: TextButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
_isLoading = true;
|
|
});
|
|
_login().then((_) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
});
|
|
},
|
|
child: const Text('login'))),
|
|
),
|
|
);
|
|
}
|
|
}
|