support Device Group HTTP POST Request (#299)

* support Device Group HTTP POST Request

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* fix vendor

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>

* check error exist

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-10-24 21:37:53 -05:00 committed by GitHub
parent ef9c4221ad
commit 461a57ec9a
5 changed files with 76 additions and 11 deletions

View File

@ -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)

View File

@ -42,7 +42,9 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
DryRun: req.DryRun,
}
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++

View File

@ -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")

View File

@ -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
}

8
vendor/vendor.json vendored
View File

@ -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=",