Skip to content

Commit

Permalink
refactor(fcm): enhance message handling and logging (#778)
Browse files Browse the repository at this point in the history
- Rename `notification` to `messages` for clarity
- Simplify error handling by consolidating error logging and response handling
- Remove redundant checks and logging for individual tokens
- Add handling for retrying topic messages
- Refactor success and error logging for topic messages
- Ensure failed tokens are retried by appending them to `newTokens`
- Clear `Topic` and `Condition` fields if no retry is needed

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy authored Jun 15, 2024
1 parent 651f59d commit 07c4319
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,74 +143,75 @@ func PushToAndroid(ctx context.Context, req *PushNotification, cfg *config.ConfY
client, err = InitFCMClient(ctx, cfg)

Retry:
notification := GetAndroidNotification(req)
messages := GetAndroidNotification(req)
if err != nil {
// FCM server error
logx.LogError.Error("FCM server error: " + err.Error())
return resp, err
}

res, err := client.Send(ctx, notification...)
res, err := client.Send(ctx, messages...)
if err != nil {
// Send Message error
logx.LogError.Error("FCM server send message error: " + err.Error())
newErr := fmt.Errorf("fcm service send message error: %v", err)
logx.LogError.Error(newErr)
errLog := logPush(cfg, core.FailedPush, "", req, newErr)
resp.Logs = append(resp.Logs, errLog)
status.StatStorage.AddAndroidError(1)

if req.IsTopic() {
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
resp.Logs = append(resp.Logs, errLog)
status.StatStorage.AddAndroidError(1)
} else {
for _, token := range req.Tokens {
errLog := logPush(cfg, core.FailedPush, token, req, err)
resp.Logs = append(resp.Logs, errLog)
}
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
}
return resp, err
}

if !req.IsTopic() {
logx.LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.SuccessCount, res.FailureCount))
return resp, newErr
}

logx.LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.SuccessCount, res.FailureCount))
status.StatStorage.AddAndroidSuccess(int64(res.SuccessCount))
status.StatStorage.AddAndroidError(int64(res.FailureCount))

var newTokens []string
// result from Send messages to specific devices
if !req.IsTopic() {
for k, result := range res.Responses {
if result.Error != nil {
errLog := logPush(cfg, core.FailedPush, req.Tokens[k], req, result.Error)
resp.Logs = append(resp.Logs, errLog)
continue
}
logPush(cfg, core.SucceededPush, req.Tokens[k], req, nil)
}
}

// result from Send messages to topics
retryTopic := false
if req.IsTopic() {
to := ""
if req.Topic != "" {
to = req.Topic
} else {
}
if req.Condition != "" {
to = req.Condition
}
logx.LogAccess.Debug("Send Topic Message: ", to)
// Success
if res.SuccessCount == 1 {

newResp := res.Responses[0]
if newResp.Success {
logPush(cfg, core.SucceededPush, to, req, nil)
} else {
}

if newResp.Error != nil {
// failure
errLog := logPush(cfg, core.FailedPush, to, req, res.Responses[0].Error)
errLog := logPush(cfg, core.FailedPush, to, req, newResp.Error)
resp.Logs = append(resp.Logs, errLog)
retryTopic = true
}

// remove the first response
res.Responses = res.Responses[1:]
}

var newTokens []string
for k, result := range res.Responses {
if result.Error != nil {
errLog := logPush(cfg, core.FailedPush, req.Tokens[k], req, result.Error)
resp.Logs = append(resp.Logs, errLog)
newTokens = append(newTokens, req.Tokens[k])
continue
}
logPush(cfg, core.SucceededPush, req.Tokens[k], req, nil)
}

if len(newTokens) > 0 && retryCount < maxRetry {
retryCount++

if req.IsTopic() && !retryTopic {
req.Topic = ""
req.Condition = ""
}

// resend fail token
req.Tokens = newTokens
goto Retry
Expand Down

0 comments on commit 07c4319

Please sign in to comment.