Skip to content

Commit

Permalink
fix:FCM push failed will return error
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu committed Jun 12, 2024
1 parent e32d30f commit 3683c9a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions internal/push/offlinepush/fcm/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package fcm

import (
"context"
"fmt"
"github.com/openimsdk/open-im-server/v3/internal/push/offlinepush/options"
"path/filepath"
"strings"

firebase "firebase.google.com/go"
"firebase.google.com/go/messaging"
Expand Down Expand Up @@ -79,16 +81,30 @@ func (f *Fcm) Push(ctx context.Context, userIDs []string, title, content string,
notification.Body = content
notification.Title = title
var messages []*messaging.Message
var sendErrBuilder strings.Builder
var msgErrBuilder strings.Builder
for userID, personTokens := range allTokens {
apns := &messaging.APNSConfig{Payload: &messaging.APNSPayload{Aps: &messaging.Aps{Sound: opts.IOSPushSound}}}
messageCount := len(messages)
if messageCount >= SinglePushCountLimit {
response, err := f.fcmMsgCli.SendAll(ctx, messages)
if err != nil {
Fail = Fail + messageCount
// Record push error
sendErrBuilder.WriteString(err.Error())
sendErrBuilder.WriteByte('\n')
} else {
Success = Success + response.SuccessCount
Fail = Fail + response.FailureCount
if response.FailureCount != 0 {
// Record message error
for i := range response.Responses {
if !response.Responses[i].Success {
msgErrBuilder.WriteString(response.Responses[i].Error.Error())
msgErrBuilder.WriteByte('\n')
}
}
}
}
messages = messages[0:0]
}
Expand Down Expand Up @@ -134,5 +150,9 @@ func (f *Fcm) Push(ctx context.Context, userIDs []string, title, content string,
Fail = Fail + response.FailureCount
}
}
if Fail != 0 {
return errs.New(fmt.Sprintf("%d message send failed\nsend err:%s\n,message err:%s",
Fail, sendErrBuilder.String(), msgErrBuilder.String())).Wrap()
}
return nil
}

0 comments on commit 3683c9a

Please sign in to comment.