From d0852f0e785c4cda1e723b4b4eb10f7069e665aa Mon Sep 17 00:00:00 2001 From: lan Date: Wed, 13 May 2026 00:31:21 +0800 Subject: [PATCH] feat(push): include group information in chat message push notifications Update the chat message push mechanism to include group ID and group avatar when sending messages in group conversations. This ensures that push notifications for group chats display the group's identity and icon rather than the sender's personal avatar. - Update `PushChatMessage` interface and implementation to accept `groupID` and `groupAvatar`. - Modify `chat_service.go` to extract group metadata and pass it to the push service. - Update `push_service.go` to include group details in the push payload extras and set the notification icon to the group avatar for group conversations. --- internal/service/chat_service.go | 10 +++++++--- internal/service/push_service.go | 14 +++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/service/chat_service.go b/internal/service/chat_service.go index ff86d43..da5b683 100644 --- a/internal/service/chat_service.go +++ b/internal/service/chat_service.go @@ -452,8 +452,12 @@ func (s *chatServiceImpl) SendMessage(ctx context.Context, senderID string, conv } convType := conv.Type convName := "" + groupID := "" + groupAvatar := "" if conv.Type == model.ConversationTypeGroup && conv.Group != nil { convName = conv.Group.Name + groupID = conv.Group.ID + groupAvatar = conv.Group.Avatar } allTargetIDs := make([]string, 0, len(participants)) for _, p := range participants { @@ -469,9 +473,9 @@ func (s *chatServiceImpl) SendMessage(ctx context.Context, senderID string, conv offlineTargetIDs = allTargetIDs } if len(offlineTargetIDs) > 0 { - go func(ids []string, sender ChatMessageSender) { + go func(ids []string, sender ChatMessageSender, gid string, gavatar string) { for _, uid := range ids { - if pushErr := s.pushSvc.PushChatMessage(context.Background(), uid, conversationID, &sender, convType, convName, message); pushErr != nil { + if pushErr := s.pushSvc.PushChatMessage(context.Background(), uid, conversationID, &sender, convType, convName, message, gid, gavatar); pushErr != nil { zap.L().Debug("push chat message failed", zap.String("userID", uid), zap.String("conversationID", conversationID), @@ -479,7 +483,7 @@ func (s *chatServiceImpl) SendMessage(ctx context.Context, senderID string, conv ) } } - }(offlineTargetIDs, sender) + }(offlineTargetIDs, sender, groupID, groupAvatar) } } diff --git a/internal/service/push_service.go b/internal/service/push_service.go index 008c6f6..6f50848 100644 --- a/internal/service/push_service.go +++ b/internal/service/push_service.go @@ -54,7 +54,7 @@ type PushService interface { // 聊天消息推送(含免打扰检查) // 该方法会:1. 检查用户是否在线(在线则跳过);2. 检查会话免打扰(免打扰则跳过);3. 通过 JPush 推送 - PushChatMessage(ctx context.Context, userID string, conversationID string, sender *ChatMessageSender, convType model.ConversationType, convName string, message *model.Message) error + PushChatMessage(ctx context.Context, userID string, conversationID string, sender *ChatMessageSender, convType model.ConversationType, convName string, message *model.Message, groupID string, groupAvatar string) error // 系统消息推送 PushSystemMessage(ctx context.Context, userID string, msgType, title, content string, data map[string]any) error @@ -686,7 +686,7 @@ func (s *pushServiceImpl) doRetry() int { // PushChatMessage 推送聊天消息给离线用户 // 检查用户在线状态和免打扰设置,仅对离线且未免打扰的用户通过 JPush 推送 -func (s *pushServiceImpl) PushChatMessage(ctx context.Context, userID string, conversationID string, sender *ChatMessageSender, convType model.ConversationType, convName string, message *model.Message) error { +func (s *pushServiceImpl) PushChatMessage(ctx context.Context, userID string, conversationID string, sender *ChatMessageSender, convType model.ConversationType, convName string, message *model.Message, groupID string, groupAvatar string) error { // 1. 用户在线则跳过(已通过 WebSocket 收到消息) if s.wsHub != nil && s.wsHub.HasClients(userID) { return nil @@ -743,6 +743,11 @@ func (s *pushServiceImpl) PushChatMessage(ctx context.Context, userID string, co "sender_avatar": sender.Avatar, "category": string(message.Category), } + if convType == model.ConversationTypeGroup { + extras["group_id"] = groupID + extras["group_name"] = convName + extras["group_avatar"] = groupAvatar + } if message.Category == model.CategoryNotification && message.ExtraData != nil { title = message.ExtraData.ActorName if title == "" { @@ -756,7 +761,10 @@ func (s *pushServiceImpl) PushChatMessage(ctx context.Context, userID string, co } notification := jpush.BuildNotification(title, content, "chat_message", extras) - if sender.Avatar != "" { + // 群聊用群头像,私聊用发送者头像 + if convType == model.ConversationTypeGroup && groupAvatar != "" { + notification.Android.LargeIcon = groupAvatar + } else if sender.Avatar != "" { notification.Android.LargeIcon = sender.Avatar } if _, pushErr := s.jpushClient.PushByRegistrationIDs(regIDs, notification); pushErr != nil {