diff --git a/README.md b/README.md index 6b3b9f2ca..20514bfca 100644 --- a/README.md +++ b/README.md @@ -986,7 +986,8 @@ See the following error format. "platform": "android", "token": "*******", "message": "Hello World Android!", - "error": "InvalidRegistration" + "error":"invalid registration token", + "error_reason":"InvalidRegistration" }, { "type": "failed-push", diff --git a/go.mod b/go.mod index 4a766fc65..abc78b65e 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.15 require ( github.com/apex/gateway v1.1.2 github.com/appleboy/gin-status-api v1.1.0 - github.com/appleboy/go-fcm v0.1.5 + github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd github.com/appleboy/gofight/v2 v2.1.2 github.com/asdine/storm/v3 v3.2.1 github.com/buger/jsonparser v1.1.1 diff --git a/go.sum b/go.sum index 580d4f5e3..cb8631c1d 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ github.com/appleboy/gin-status-api v1.1.0 h1:zoXePlNxk/Aa3Jmh8TI2xX0KTF8iET/QwOM github.com/appleboy/gin-status-api v1.1.0/go.mod h1:qUmpFERWhlzRX4Hx+fEznIio8gXAXEDpEnb0Ald1d+g= github.com/appleboy/go-fcm v0.1.5 h1:fKbcZf/7vwGsvDkcop8a+kCHnK+tt4wXX0X7uEzwI6E= github.com/appleboy/go-fcm v0.1.5/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0= +github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd h1:M/LhFbSWAwLmdhjOVBik1Ekj6wAgxS3H3XtV5czaxE4= +github.com/appleboy/go-fcm v0.1.6-0.20210717010109-0181be2279cd/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0= github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4= github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= diff --git a/logx/log.go b/logx/log.go index b7b78b6aa..3c936def8 100644 --- a/logx/log.go +++ b/logx/log.go @@ -23,12 +23,13 @@ var ( // LogPushEntry is push response log type LogPushEntry struct { - ID string `json:"notif_id,omitempty"` - Type string `json:"type"` - Platform string `json:"platform"` - Token string `json:"token"` - Message string `json:"message"` - Error string `json:"error"` + ID string `json:"notif_id,omitempty"` + Type string `json:"type"` + Platform string `json:"platform"` + Token string `json:"token"` + Message string `json:"message"` + Error string `json:"error"` + ErrorReason string `json:"error_reason,omitempty"` } var isTerm bool @@ -175,25 +176,27 @@ func GetLogPushEntry(input *InputLog) LogPushEntry { } return LogPushEntry{ - ID: input.ID, - Type: input.Status, - Platform: plat, - Token: token, - Message: input.Message, - Error: errMsg, + ID: input.ID, + Type: input.Status, + Platform: plat, + Token: token, + Message: input.Message, + Error: errMsg, + ErrorReason: input.ErrorReason, } } // InputLog log request type InputLog struct { - ID string - Status string - Token string - Message string - Platform int - Error error - HideToken bool - Format string + ID string + Status string + Token string + Message string + Platform int + Error error + ErrorReason string + HideToken bool + Format string } // LogPush record user push request and server response. diff --git a/notify/notification_apns.go b/notify/notification_apns.go index d1b2a9d0c..37941441e 100644 --- a/notify/notification_apns.go +++ b/notify/notification_apns.go @@ -12,7 +12,6 @@ import ( "time" "github.com/appleboy/gorush/config" - "github.com/appleboy/gorush/core" "github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/status" @@ -427,7 +426,7 @@ Retry: } // apns server error - errLog := logPush(cfg, core.FailedPush, token, req, err) + errLog := logErrorPush(cfg, token, req, err, res.Reason) resp.Logs = append(resp.Logs, errLog) status.StatStorage.AddIosError(1) @@ -439,7 +438,7 @@ Retry: } if res != nil && res.Sent() { - logPush(cfg, core.SucceededPush, token, req, nil) + logErrorPush(cfg, token, req, nil, res.Reason) status.StatStorage.AddIosSuccess(1) } diff --git a/notify/notification_fcm.go b/notify/notification_fcm.go index 772393b12..6eb652c3d 100644 --- a/notify/notification_fcm.go +++ b/notify/notification_fcm.go @@ -148,12 +148,12 @@ Retry: logx.LogError.Error("FCM server send message error: " + err.Error()) if req.IsTopic() { - errLog := logPush(cfg, core.FailedPush, req.To, req, err) + errLog := logErrorPush(cfg, 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) + errLog := logErrorPush(cfg, token, req, err, "") resp.Logs = append(resp.Logs, errLog) } status.StatStorage.AddAndroidError(int64(len(req.Tokens))) @@ -185,12 +185,12 @@ Retry: newTokens = append(newTokens, to) } - errLog := logPush(cfg, core.FailedPush, to, req, result.Error) + errLog := logErrorPush(cfg, to, req, result.Error, result.ErrorResponseCode) resp.Logs = append(resp.Logs, errLog) continue } - logPush(cfg, core.SucceededPush, to, req, nil) + logSuccessPush(cfg, to, req) } // result from Send messages to topics @@ -204,10 +204,10 @@ Retry: logx.LogAccess.Debug("Send Topic Message: ", to) // Success if res.MessageID != 0 { - logPush(cfg, core.SucceededPush, to, req, nil) + logSuccessPush(cfg, to, req) } else { // failure - errLog := logPush(cfg, core.FailedPush, to, req, res.Error) + errLog := logErrorPush(cfg, to, req, res.Error, res.ErrorResponseCode) resp.Logs = append(resp.Logs, errLog) } } @@ -217,7 +217,7 @@ Retry: newTokens = append(newTokens, res.FailedRegistrationIDs...) // nolint - errLog := logPush(cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails")) + errLog := logErrorPush(cfg, notification.To, req, errors.New("device group: partial success or all fails"), res.ErrorResponseCode) resp.Logs = append(resp.Logs, errLog) } @@ -232,14 +232,28 @@ Retry: return resp, nil } -func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry { +func logErrorPush(cfg *config.ConfYaml, token string, req *PushNotification, err error, errorReason string) logx.LogPushEntry { + return logx.LogPush(&logx.InputLog{ + ID: req.ID, + Status: core.FailedPush, + Token: token, + Message: req.Message, + Platform: req.Platform, + Error: err, + ErrorReason: errorReason, + HideToken: cfg.Log.HideToken, + Format: cfg.Log.Format, + }) +} + +func logSuccessPush(cfg *config.ConfYaml, token string, req *PushNotification) logx.LogPushEntry { return logx.LogPush(&logx.InputLog{ ID: req.ID, - Status: status, + Status: core.SucceededPush, Token: token, Message: req.Message, Platform: req.Platform, - Error: err, + Error: nil, HideToken: cfg.Log.HideToken, Format: cfg.Log.Format, }) diff --git a/notify/notification_hms.go b/notify/notification_hms.go index 1bdfc1504..d75decac0 100644 --- a/notify/notification_hms.go +++ b/notify/notification_hms.go @@ -6,7 +6,6 @@ import ( "sync" "github.com/appleboy/gorush/config" - "github.com/appleboy/gorush/core" "github.com/appleboy/gorush/logx" "github.com/appleboy/gorush/status" c "github.com/msalihkarakasli/go-hms-push/push/config" @@ -199,7 +198,7 @@ Retry: res, err := client.SendMessage(context.Background(), notification) if err != nil { // Send Message error - errLog := logPush(cfg, core.FailedPush, req.To, req, err) + errLog := logErrorPush(cfg, req.To, req, err, "") resp.Logs = append(resp.Logs, errLog) logx.LogError.Error("HMS server send message error: " + err.Error()) return