From c66b7bcd21963946b2a455cb01061aeda144fe2c Mon Sep 17 00:00:00 2001
From: Jolie <412895109@qq.com>
Date: Sat, 27 Dec 2025 17:02:07 +0800
Subject: [PATCH] =?UTF-8?q?feat(notification):=20=E6=B7=BB=E5=8A=A0?=
=?UTF-8?q?=E5=BA=94=E7=94=A8=E8=A7=92=E6=A0=87=E6=9C=AA=E8=AF=BB=E6=95=B0?=
=?UTF-8?q?=E6=98=BE=E7=A4=BA=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 在 AndroidManifest.xml 中添加各厂商桌面角标权限配置
- 集成 app_badge_plus 插件实现角标更新功能
- 在 ConversationController 中监听未读数变化并同步更新应用角标
- 优化消息通知逻辑,仅在 APP 前台时显示通知弹框
- 添加角标更新的错误处理和日志记录
---
android/app/src/main/AndroidManifest.xml | 22 ++++++++++++++++++
.../message/conversation_controller.dart | 23 +++++++++++++++++++
lib/im/im_manager.dart | 6 ++++-
3 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index b1c9205..209992c 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -16,6 +16,28 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
_updateAppBadge(int count) async {
+ try {
+ await AppBadgePlus.updateBadge(count);
+ if (Get.isLogEnable) {
+ Get.log('✅ [ConversationController] 应用角标已更新: $count');
+ }
+ } catch (e) {
+ if (Get.isLogEnable) {
+ Get.log('⚠️ [ConversationController] 更新应用角标失败: $e');
+ }
+ }
+ }
+
/// 检查 IM 登录状态并加载会话列表
Future _checkAndLoadConversations() async {
// 如果已登录,直接加载
@@ -481,6 +500,8 @@ class ConversationController extends GetxController {
total += unreadCount;
}
totalUnreadCount.value = total;
+ // 更新应用角标
+ await _updateAppBadge(total);
if (Get.isLogEnable) {
Get.log('✅ [ConversationController] 总未读数已更新: $total');
}
@@ -489,6 +510,8 @@ class ConversationController extends GetxController {
Get.log('⚠️ [ConversationController] 更新总未读数失败: $e');
}
totalUnreadCount.value = 0;
+ // 更新应用角标为 0
+ await _updateAppBadge(0);
}
}
diff --git a/lib/im/im_manager.dart b/lib/im/im_manager.dart
index a8ff812..2f409fc 100644
--- a/lib/im/im_manager.dart
+++ b/lib/im/im_manager.dart
@@ -214,7 +214,11 @@ class IMManager {
if (message.direction == MessageDirection.RECEIVE && message.onlineState) {
_parseUserInfoFromMessageExt(message);
// 检查发送者是否是当前正在聊天的用户,如果不是则显示弹框
- _checkAndShowNotificationDialog(message);
+ // 只有在 APP 处于前台时才显示弹框
+ final lifecycleState = WidgetsBinding.instance.lifecycleState;
+ if (lifecycleState == AppLifecycleState.resumed) {
+ _checkAndShowNotificationDialog(message);
+ }
}
}
// 收到新消息时,更新会话列表