feat(push): include group information in chat message push notifications
All checks were successful
Build Backend / build (push) Successful in 2m15s
Build Backend / build-docker (push) Successful in 1m32s

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.
This commit is contained in:
2026-05-13 00:31:21 +08:00
parent 06db8e36e4
commit d0852f0e78
2 changed files with 18 additions and 6 deletions

View File

@@ -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)
}
}

View File

@@ -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 {