From 6cf03c32e744018af659b8cc82773111c54ca1ad Mon Sep 17 00:00:00 2001 From: GoHorse Deploy Date: Sat, 7 Mar 2026 19:09:34 -0300 Subject: [PATCH] fix: resolve duplicate FCM service and add SendMulticast method --- .../infrastructure/notifications/fcm.go | 78 ------------------- backend/internal/services/fcm_service.go | 23 ++++++ 2 files changed, 23 insertions(+), 78 deletions(-) delete mode 100644 backend/internal/infrastructure/notifications/fcm.go diff --git a/backend/internal/infrastructure/notifications/fcm.go b/backend/internal/infrastructure/notifications/fcm.go deleted file mode 100644 index fd438ba..0000000 --- a/backend/internal/infrastructure/notifications/fcm.go +++ /dev/null @@ -1,78 +0,0 @@ -package notifications - -import ( - "context" - "fmt" - "log" - - firebase "firebase.google.com/go/v4" - "firebase.google.com/go/v4/messaging" - "google.golang.org/api/option" -) - -type FCMService struct { - app *firebase.App - client *messaging.Client -} - -func NewFCMService(serviceAccountJSON []byte) (*FCMService, error) { - ctx := context.Background() - opt := option.WithCredentialsJSON(serviceAccountJSON) - - app, err := firebase.NewApp(ctx, nil, opt) - if err != nil { - return nil, fmt.Errorf("error initializing firebase app: %w", err) - } - - client, err := app.Messaging(ctx) - if err != nil { - return nil, fmt.Errorf("error getting messaging client: %w", err) - } - - return &FCMService{ - app: app, - client: client, - }, nil -} - -func (s *FCMService) SendPush(ctx context.Context, token, title, body string, data map[string]string) error { - message := &messaging.Message{ - Token: token, - Notification: &messaging.Notification{ - Title: title, - Body: body, - }, - Data: data, - } - - response, err := s.client.Send(ctx, message) - if err != nil { - return fmt.Errorf("error sending fcm message: %w", err) - } - - log.Printf("Successfully sent fcm message: %s", response) - return nil -} - -func (s *FCMService) SendMulticast(ctx context.Context, tokens []string, title, body string, data map[string]string) error { - if len(tokens) == 0 { - return nil - } - - message := &messaging.MulticastMessage{ - Tokens: tokens, - Notification: &messaging.Notification{ - Title: title, - Body: body, - }, - Data: data, - } - - br, err := s.client.SendEachForMulticast(ctx, message) - if err != nil { - return fmt.Errorf("error sending multicast message: %w", err) - } - - log.Printf("Multicast results: %d successes, %d failures", br.SuccessCount, br.FailureCount) - return nil -} diff --git a/backend/internal/services/fcm_service.go b/backend/internal/services/fcm_service.go index 9aa857a..3532bc9 100644 --- a/backend/internal/services/fcm_service.go +++ b/backend/internal/services/fcm_service.go @@ -123,6 +123,29 @@ func (s *FCMService) Send(token, title, body string, data map[string]string) err return s.SendPush(context.Background(), token, title, body, data) } +func (s *FCMService) SendMulticast(ctx context.Context, tokens []string, title, body string, data map[string]string) error { + if len(tokens) == 0 { + return nil + } + + client, err := s.getClient(ctx) + if err != nil { + return err + } + + message := &messaging.MulticastMessage{ + Tokens: tokens, + Notification: &messaging.Notification{ + Title: title, + Body: body, + }, + Data: data, + } + + _, err = client.SendEachForMulticast(ctx, message) + return err +} + // SubscribeToTopic subscribes a token to a topic func (s *FCMService) SubscribeToTopic(ctx context.Context, tokens []string, topic string) error { client, err := s.getClient(ctx)