From 461a57ec9a41ea493f3d4f9c95ce229c2d0cbff1 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 24 Oct 2017 21:37:53 -0500 Subject: [PATCH] support Device Group HTTP POST Request (#299) * support Device Group HTTP POST Request Signed-off-by: Bo-Yi Wu * fix vendor Signed-off-by: Bo-Yi Wu * check error exist Signed-off-by: Bo-Yi Wu --- gorush/notification.go | 2 +- gorush/notification_fcm.go | 32 ++++++++++++++++--- gorush/notification_test.go | 26 +++++++++++++++ vendor/github.com/appleboy/go-fcm/response.go | 19 +++++++++-- vendor/vendor.json | 8 +++-- 5 files changed, 76 insertions(+), 11 deletions(-) diff --git a/gorush/notification.go b/gorush/notification.go index 8fd089b..905f525 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -117,7 +117,7 @@ func CheckMessage(req PushNotification) error { var msg string // ignore send topic mesaage from FCM - if !req.IsTopic() && len(req.Tokens) == 0 { + if !req.IsTopic() && len(req.Tokens) == 0 && len(req.To) == 0 { msg = "the message must specify at least one registration ID" LogAccess.Debug(msg) return errors.New(msg) diff --git a/gorush/notification_fcm.go b/gorush/notification_fcm.go index 17ed97d..0076d13 100644 --- a/gorush/notification_fcm.go +++ b/gorush/notification_fcm.go @@ -42,7 +42,9 @@ func GetAndroidNotification(req PushNotification) *fcm.Message { DryRun: req.DryRun, } - notification.RegistrationIDs = req.Tokens + if len(req.Tokens) > 0 { + notification.RegistrationIDs = req.Tokens + } if len(req.Priority) > 0 && req.Priority == "high" { notification.Priority = "high" @@ -133,17 +135,24 @@ Retry: var newTokens []string // result from Send messages to specific devices for k, result := range res.Results { + to := "" + if k < len(req.Tokens) { + to = req.Tokens[k] + } else { + to = req.To + } + if result.Error != nil { isError = true - newTokens = append(newTokens, req.Tokens[k]) - LogPush(FailedPush, req.Tokens[k], req, result.Error) + newTokens = append(newTokens, to) + LogPush(FailedPush, to, req, result.Error) if PushConf.Core.Sync { - req.AddLog(getLogPushEntry(FailedPush, req.Tokens[k], req, result.Error)) + req.AddLog(getLogPushEntry(FailedPush, to, req, result.Error)) } continue } - LogPush(SucceededPush, req.Tokens[k], req, nil) + LogPush(SucceededPush, to, req, nil) } // result from Send messages to topics @@ -168,6 +177,19 @@ Retry: } } + // Device Group HTTP Response + if len(res.FailedRegistrationIDs) > 0 { + isError = true + for _, id := range res.FailedRegistrationIDs { + newTokens = append(newTokens, id) + } + + LogPush(FailedPush, notification.To, req, errors.New("device group: partial success or all fails")) + if PushConf.Core.Sync { + req.AddLog(getLogPushEntry(FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))) + } + } + if isError && retryCount < maxRetry { retryCount++ diff --git a/gorush/notification_test.go b/gorush/notification_test.go index 7aaffb3..68f6481 100644 --- a/gorush/notification_test.go +++ b/gorush/notification_test.go @@ -174,6 +174,32 @@ func TestSyncModeForTopicNotification(t *testing.T) { assert.Equal(t, 1, len(logs)) } +func TestSyncModeForDeviceGroupNotification(t *testing.T) { + PushConf, _ = config.LoadConf("") + + PushConf.Android.Enabled = true + PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + PushConf.Log.HideToken = false + + // enable sync mode + PushConf.Core.Sync = true + + req := RequestPush{ + Notifications: []PushNotification{ + // android + { + To: "aUniqueKey", + Platform: PlatFormAndroid, + Message: "This is a Firebase Cloud Messaging Device Group Message!", + }, + }, + } + + count, logs := queueNotification(req) + assert.Equal(t, 1, count) + assert.Equal(t, 1, len(logs)) +} + func TestSetProxyURL(t *testing.T) { err := SetProxy("87.236.233.92:8080") diff --git a/vendor/github.com/appleboy/go-fcm/response.go b/vendor/github.com/appleboy/go-fcm/response.go index 715f745..7deb576 100644 --- a/vendor/github.com/appleboy/go-fcm/response.go +++ b/vendor/github.com/appleboy/go-fcm/response.go @@ -49,6 +49,9 @@ var ( // ErrInvalidParameters occurs when provided parameters have the right name and type ErrInvalidParameters = errors.New("check that the provided parameters have the right name and type") + + // ErrUnknown for unknown error type + ErrUnknown = errors.New("unknown error type") ) var ( @@ -147,7 +150,13 @@ func (r *Response) UnmarshalJSON(data []byte) error { r.Success = response.Success r.FailedRegistrationIDs = response.FailedRegistrationIDs r.MessageID = response.MessageID - r.Error = errMap[response.Error] + if response.Error != "" { + if val, ok := errMap[response.Error]; ok { + r.Error = val + } else { + r.Error = ErrUnknown + } + } return nil } @@ -173,7 +182,13 @@ func (r *Result) UnmarshalJSON(data []byte) error { r.MessageID = result.MessageID r.RegistrationID = result.RegistrationID - r.Error = errMap[result.Error] + if result.Error != "" { + if val, ok := errMap[result.Error]; ok { + r.Error = val + } else { + r.Error = ErrUnknown + } + } return nil } diff --git a/vendor/vendor.json b/vendor/vendor.json index 9c976b5..532e536 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -3,10 +3,12 @@ "ignore": "test", "package": [ { - "checksumSHA1": "TVTjsXflagrWbTXmbxPJdCtTFRo=", + "checksumSHA1": "1Jql7x7zDOmbDxGr4gsa8rFEC5g=", "path": "github.com/appleboy/go-fcm", - "revision": "c12f9e2e95b14802da2b4d3807dd12ef0dd80a42", - "revisionTime": "2017-10-24T08:00:40Z" + "revision": "3bc382cee4180b7e4753761fda69a003de97b0e9", + "revisionTime": "2017-10-25T02:33:50Z", + "version": "0.1.1", + "versionExact": "0.1.1" }, { "checksumSHA1": "Ab7MUtqX0iq2PUzzBxWpgzPSydw=",