Commit 17e35d38 by tanghuan

消息推送完善

1 parent ca90354c
......@@ -406,9 +406,9 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
debugPrint("缓存自动登录处,IM 登录成功");
// 注册推送服务
imService.registerPush();
//
// 处理加群和退群
if (state.loginOpFlag) {
_getPendingGroup();
// imService.joinAndLeaveGroup(state.userCode!);
}
} else {
debugPrint("缓存自动登录处,IM 登录失败");
......@@ -416,51 +416,6 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
}
}
///
/// 获取待加群和待退群
///
Future<Map<String, List<String>>> _getPendingGroup() async {
var classIds = getIt.get<SharedPreferences>().getStringList(Constant.classIdSetKey);
List<String> classIdList = [];
if (classIds != null) {
// 转换和去重
classIdList = Set<String>.from(classIds).toList();
}
var unjoinedGroupIdList = <String>[];
var leaveGroupIdList = <String>[];
Map<String, List<String>> result = {
'unjoinedGroupIdList': unjoinedGroupIdList,
'leaveGroupIdList': leaveGroupIdList,
};
var imService = getIt.get<ImService>();
var joinedGroupIdList = await imService.getJoinedGroupList();
// 获取群组列表失败,joinedGroupIdList=null
if (joinedGroupIdList == null) {
return result;
}
// 需要加群
for (var classId in classIdList) {
if (!joinedGroupIdList.contains(classId)) {
unjoinedGroupIdList.add(classId);
}
}
// 需要退群
for (var joinedGroupId in joinedGroupIdList) {
if (!classIdList.contains(joinedGroupId)) {
leaveGroupIdList.add(joinedGroupId);
}
}
debugPrint('待加群: $unjoinedGroupIdList');
debugPrint('待退群: $leaveGroupIdList');
return result;
}
void _onMessageReceived(JavaScriptMessage message) async {
try {
_dispatcher.dispatch(message.message, (response) {
......
......@@ -99,6 +99,6 @@ class Constant {
static const String classIdSetKey = 'auth_class_ids';
/// 测试阶段使用
static const bool needIM = false;
static const bool needIM = true;
static const bool needUpgrade = true;
}
......@@ -34,6 +34,7 @@ import 'package:appframe/data/repositories/message/video_info_handler.dart';
import 'package:appframe/data/repositories/message/wifi_info_handler.dart';
import 'package:appframe/data/repositories/message/window_info_handler.dart';
import 'package:appframe/data/repositories/phone_auth_repository.dart';
import 'package:appframe/data/repositories/subs_repository.dart';
import 'package:appframe/data/repositories/wechat_auth_repository.dart';
import 'package:appframe/services/api_service.dart';
import 'package:appframe/services/dispatcher.dart';
......@@ -219,4 +220,5 @@ Future<void> setupLocator() async {
/// repository
getIt.registerLazySingleton<WechatAuthRepository>(() => WechatAuthRepository());
getIt.registerLazySingleton<PhoneAuthRepository>(() => PhoneAuthRepository());
getIt.registerLazySingleton<SubsRepository>(() => SubsRepository());
}
import 'package:appframe/config/locator.dart';
import 'package:appframe/services/api_service.dart';
import 'package:dio/dio.dart';
class SubsRepository {
late final ApiService _appService;
SubsRepository() {
_appService = getIt<ApiService>(instanceName: 'appApiService');
}
Future<dynamic> userGroups(String type, String userid, String classCode) async {
Response resp = await _appService.post(
'/api/v1/comm/subs/usergroups',
{
"type": type,
"userid": userid,
"classCode": classCode,
},
);
return resp.data;
}
}
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/data/repositories/subs_repository.dart';
import 'package:appframe/services/api_service.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tencent_cloud_chat_push/common/tim_push_listener.dart';
import 'package:tencent_cloud_chat_push/common/tim_push_message.dart';
import 'package:tencent_cloud_chat_push/tencent_cloud_chat_push.dart';
......@@ -63,13 +65,13 @@ class ImService {
},
onRecvMessageReadReceipts: (List<V2TimMessageReceipt> receiptList) {
//群聊已读回调
receiptList.forEach((element) {
for (var element in receiptList) {
element.groupID; // 群id
element.msgID; // 已读回执消息 ID
element.readCount; // 群消息最新已读数
element.unreadCount; // 群消息最新未读数
element.userID; // C2C 消息对方 ID
});
}
},
onRecvMessageRevoked: (String messageid) {
// 在本地维护的消息中处理被对方撤回的消息
......@@ -81,7 +83,7 @@ class ImService {
// 时间戳转换
DateTime dt = DateTime.fromMillisecondsSinceEpoch(message.timestamp! * 1000);
print(
debugPrint(
"收到IM消息—— 时间:${dt.year}-${dt.month}-${dt.day} ${dt.hour}:${dt.minute}:${dt.second} 发送者:${message.sender} 内容:${message.textElem?.text}");
// 目前只会有文本消息,所以其他消息类型暂不处理,直接return
......@@ -171,7 +173,7 @@ class ImService {
msgID: message.msgID!,
);
if (download.code == 0) {
List<V2TimMessage>? messageList = download.data;
//List<V2TimMessage>? messageList = download.data;
}
}
if (message.textElem?.nextElem != null) {
......@@ -197,10 +199,10 @@ class ImService {
);
if (initSDKRes.code == 0) {
print("IM SDK 初始化成功-------- ${initSDKRes.data}");
debugPrint("IM SDK 初始化成功-------- ${initSDKRes.data}");
return true;
} else {
print("IM SDK 初始化失败-------- ${initSDKRes.data}");
debugPrint("IM SDK 初始化失败-------- ${initSDKRes.data}");
return false;
}
}
......@@ -244,21 +246,21 @@ class ImService {
V2TimCallback res = await TencentImSDKPlugin.v2TIMManager.login(userID: userID, userSig: userSig);
loginStatus = await TencentImSDKPlugin.v2TIMManager.getLoginStatus();
print('IM 登录状态:${loginStatus.data}');
debugPrint('IM 登录状态:${loginStatus.data}');
if (res.code == 0) {
print("IM 登录成功--------");
debugPrint("IM 登录成功--------");
// 添加消息的事件监听器
// await TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: msgListener);
await addMsgListener(_msgListener);
var loginUserResp = await TencentImSDKPlugin.v2TIMManager.getLoginUser();
print("当前登录用户:${loginUserResp.data}");
debugPrint("当前登录用户:${loginUserResp.data}");
return true;
} else {
// 登录失败逻辑
print("IM 登录失败--------");
debugPrint("IM 登录失败--------");
return false;
}
}
......@@ -271,14 +273,14 @@ class ImService {
var logoutRes = await TencentImSDKPlugin.v2TIMManager.logout();
if (logoutRes.code == 0) {
// 登出成功逻辑
print("IM 登出成功--------");
debugPrint("IM 登出成功--------");
return true;
} else {
return false;
}
}
Future<List<String>?> getJoinedGroupList() async {
Future<List<String>?> _getJoinedGroupList() async {
var groupListRes = await TencentImSDKPlugin.v2TIMManager.getGroupManager().getJoinedGroupList();
if (groupListRes.code == 0) {
debugPrint("获取群列表成功--------");
......@@ -290,9 +292,51 @@ class ImService {
}
}
void joinAndLeaveGroup(String userId) async {
List<String>? classIds = getIt.get<SharedPreferences>().getStringList(Constant.classIdSetKey);
List<String> classIdList = [];
if (classIds != null) {
// 转换和去重
classIdList = Set<String>.from(classIds).toList();
}
var unjoinedGroupIdList = <String>[];
var leaveGroupIdList = <String>[];
var joinedGroupIdList = await _getJoinedGroupList();
// 获取群组列表失败,joinedGroupIdList=null
if (joinedGroupIdList == null) {
return;
}
// 需要加群
for (var classId in classIdList) {
if (!joinedGroupIdList.contains(classId)) {
unjoinedGroupIdList.add(classId);
}
}
// 需要退群
for (var joinedGroupId in joinedGroupIdList) {
if (!classIdList.contains(joinedGroupId)) {
leaveGroupIdList.add(joinedGroupId);
}
}
// 发送请求
var subsRepository = getIt.get<SubsRepository>();
if (unjoinedGroupIdList.isNotEmpty) {
debugPrint("需要加入:$unjoinedGroupIdList");
await subsRepository.userGroups('addMember', userId, unjoinedGroupIdList.join(","));
}
if (leaveGroupIdList.isNotEmpty) {
debugPrint("需要退出:$leaveGroupIdList");
await subsRepository.userGroups('deleteMember', userId, leaveGroupIdList.join(","));
}
}
void _onNotificationClicked({required String ext, String? userID, String? groupID}) {
print("收到推送消息--------");
print("_onNotificationClicked: $ext, userID: $userID, groupID: $groupID");
debugPrint("_onNotificationClicked: $ext, userID: $userID, groupID: $groupID");
if (userID != null || groupID != null) {
// 根据 userID 或 groupID 跳转至对应 Message 页面.
} else {
......@@ -302,16 +346,23 @@ class ImService {
TIMPushListener timPushListener = TIMPushListener(
onRecvPushMessage: (TimPushMessage message) {
print('推送监听器 onRecvPushMessage-------------');
debugPrint('推送监听器 onRecvPushMessage-------------');
String messageLog = message.toLogString();
debugPrint("message: $messageLog");
// // 手机消息通知
// getIt.get<NotificationService>().showNotification(
// id: DateTime.now().millisecondsSinceEpoch % 1000000,
// title: message.title ?? '',
// body: message.desc ?? '',
// );
},
onRevokePushMessage: (String messageId) {
print('推送监听器 onRevokePushMessage-------------');
debugPrint('推送监听器 onRevokePushMessage-------------');
debugPrint("message: $messageId");
},
onNotificationClicked: (String ext) {
print('推送监听器 onNotificationClicked-------------');
debugPrint('推送监听器 onNotificationClicked-------------');
debugPrint("ext: $ext");
},
);
......@@ -325,7 +376,7 @@ class ImService {
appKey: Constant.imClientSecure,
);
if (res.code == 0) {
print('注册推送成功--------');
debugPrint('注册推送成功--------');
/// 添加监听器
///
......@@ -333,20 +384,20 @@ class ImService {
var getIdRes = await TencentCloudChatPush().getRegistrationID();
if (getIdRes.code == 0) {
print('getRegistrationID: ${getIdRes.data}');
debugPrint('getRegistrationID: ${getIdRes.data}');
} else {
print('getRegistrationID: ${getIdRes.errorMessage}');
debugPrint('getRegistrationID error: ${getIdRes.errorMessage}');
}
var tokenRes = await TencentCloudChatPush().getAndroidPushToken();
if (tokenRes.code == 0) {
print('android Token: ${tokenRes.data}');
debugPrint('android Token: ${tokenRes.data}');
} else {
print('android Token: ${tokenRes.errorMessage}');
debugPrint('android Token error: ${tokenRes.errorMessage}');
}
} else {
print('注册推送失败--------');
print('${res.errorMessage}');
debugPrint('注册推送失败--------');
debugPrint('${res.errorMessage}');
}
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!