Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 17e35d38
authored
2026-01-15 09:19:31 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
消息推送完善
1 parent
ca90354c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
103 additions
and
72 deletions
lib/bloc/web_cubit.dart
lib/config/constant.dart
lib/config/locator.dart
lib/data/repositories/subs_repository.dart
lib/services/im_service.dart
lib/bloc/web_cubit.dart
View file @
17e35d3
...
...
@@ -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
)
{
...
...
lib/config/constant.dart
View file @
17e35d3
...
...
@@ -99,6 +99,6 @@ class Constant {
static
const
String
classIdSetKey
=
'auth_class_ids'
;
/// 测试阶段使用
static
const
bool
needIM
=
fals
e
;
static
const
bool
needIM
=
tru
e
;
static
const
bool
needUpgrade
=
true
;
}
lib/config/locator.dart
View file @
17e35d3
...
...
@@ -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
());
}
lib/data/repositories/subs_repository.dart
0 → 100644
View file @
17e35d3
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
;
}
}
lib/services/im_service.dart
View file @
17e35d3
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
((
elemen
t
)
{
for
(
var
element
in
receiptLis
t
)
{
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
);
p
rint
(
debugP
rint
(
"收到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
)
{
p
rint
(
"IM SDK 初始化成功--------
${initSDKRes.data}
"
);
debugP
rint
(
"IM SDK 初始化成功--------
${initSDKRes.data}
"
);
return
true
;
}
else
{
p
rint
(
"IM SDK 初始化失败--------
${initSDKRes.data}
"
);
debugP
rint
(
"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
();
p
rint
(
'IM 登录状态:
${loginStatus.data}
'
);
debugP
rint
(
'IM 登录状态:
${loginStatus.data}
'
);
if
(
res
.
code
==
0
)
{
p
rint
(
"IM 登录成功--------"
);
debugP
rint
(
"IM 登录成功--------"
);
// 添加消息的事件监听器
// await TencentImSDKPlugin.v2TIMManager.getMessageManager().addAdvancedMsgListener(listener: msgListener);
await
addMsgListener
(
_msgListener
);
var
loginUserResp
=
await
TencentImSDKPlugin
.
v2TIMManager
.
getLoginUser
();
p
rint
(
"当前登录用户:
${loginUserResp.data}
"
);
debugP
rint
(
"当前登录用户:
${loginUserResp.data}
"
);
return
true
;
}
else
{
// 登录失败逻辑
p
rint
(
"IM 登录失败--------"
);
debugP
rint
(
"IM 登录失败--------"
);
return
false
;
}
}
...
...
@@ -271,14 +273,14 @@ class ImService {
var
logoutRes
=
await
TencentImSDKPlugin
.
v2TIMManager
.
logout
();
if
(
logoutRes
.
code
==
0
)
{
// 登出成功逻辑
p
rint
(
"IM 登出成功--------"
);
debugP
rint
(
"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
)
{
p
rint
(
'推送监听器 onRecvPushMessage-------------'
);
debugP
rint
(
'推送监听器 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
)
{
p
rint
(
'推送监听器 onRevokePushMessage-------------'
);
debugP
rint
(
'推送监听器 onRevokePushMessage-------------'
);
debugPrint
(
"message:
$messageId
"
);
},
onNotificationClicked:
(
String
ext
)
{
p
rint
(
'推送监听器 onNotificationClicked-------------'
);
debugP
rint
(
'推送监听器 onNotificationClicked-------------'
);
debugPrint
(
"ext:
$ext
"
);
},
);
...
...
@@ -325,7 +376,7 @@ class ImService {
appKey:
Constant
.
imClientSecure
,
);
if
(
res
.
code
==
0
)
{
p
rint
(
'注册推送成功--------'
);
debugP
rint
(
'注册推送成功--------'
);
/// 添加监听器
///
...
...
@@ -333,20 +384,20 @@ class ImService {
var
getIdRes
=
await
TencentCloudChatPush
().
getRegistrationID
();
if
(
getIdRes
.
code
==
0
)
{
p
rint
(
'getRegistrationID:
${getIdRes.data}
'
);
debugP
rint
(
'getRegistrationID:
${getIdRes.data}
'
);
}
else
{
print
(
'getRegistrationID
:
${getIdRes.errorMessage}
'
);
debugPrint
(
'getRegistrationID error
:
${getIdRes.errorMessage}
'
);
}
var
tokenRes
=
await
TencentCloudChatPush
().
getAndroidPushToken
();
if
(
tokenRes
.
code
==
0
)
{
p
rint
(
'android Token:
${tokenRes.data}
'
);
debugP
rint
(
'android Token:
${tokenRes.data}
'
);
}
else
{
print
(
'android Token
:
${tokenRes.errorMessage}
'
);
debugPrint
(
'android Token error
:
${tokenRes.errorMessage}
'
);
}
}
else
{
p
rint
(
'注册推送失败--------'
);
p
rint
(
'
${res.errorMessage}
'
);
debugP
rint
(
'注册推送失败--------'
);
debugP
rint
(
'
${res.errorMessage}
'
);
}
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment