增加群组通知过滤
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
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 {
|
||||
@@ -18,6 +22,165 @@ class MyApp extends StatefulWidget {
|
||||
}
|
||||
|
||||
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.eyJVc2VySUQiOiI4YmZlMTNiNWVhYzQ0ZTg3OTYzNjUyYWJiOTFkODBkMiIsIlBsYXRmb3JtSUQiOjIsImV4cCI6MTc3MTIzNzE1NywiaWF0IjoxNzYzNDYxMTUyfQ.J0WCPoGAJAV8dn8F5vfan0WPn_DOFmc4pg93V7W6VZ0")
|
||||
.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(
|
||||
@@ -32,36 +195,6 @@ class _MyAppState extends State<MyApp> {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
OpenIM.iMManager
|
||||
.initSDK(
|
||||
platformID: 3,
|
||||
apiAddr: 'http://192.168.77.135:10002',
|
||||
wsAddr: 'ws://192.168.77.135:10001',
|
||||
dataDir: './',
|
||||
listener: OnConnectListener())
|
||||
.then((value) {});
|
||||
OpenIM.iMManager.userManager.setUserListener(OnUserListener());
|
||||
|
||||
OpenIM.iMManager
|
||||
.login(
|
||||
userID: "8bfe13b5eac44e87963652abb91d80d2",
|
||||
token:
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySUQiOiI4YmZlMTNiNWVhYzQ0ZTg3OTYzNjUyYWJiOTFkODBkMiIsIlBsYXRmb3JtSUQiOjMsImV4cCI6MTc1NDM2NDkwNCwiaWF0IjoxNzQ2NTg4ODk5fQ.575xwR9lzJo1i5Te-Ul-99odONfxbzDXW0_2Kv3nFEE")
|
||||
.then((value) {
|
||||
List<String> list = [];
|
||||
OpenIM.iMManager.userManager.subscribeUsersStatus(list);
|
||||
send();
|
||||
OpenIM.iMManager.conversationManager
|
||||
.getAllConversationList()
|
||||
.then((value) {
|
||||
print('Get all conversation list successful');
|
||||
});
|
||||
}).catchError((error) {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
@@ -69,11 +202,35 @@ class _MyAppState extends State<MyApp> {
|
||||
appBar: AppBar(
|
||||
title: const Text('Plugin example app'),
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
TextButton(onPressed: () {}, child: const Text('login')),
|
||||
],
|
||||
),
|
||||
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'))),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user