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:
parent
ef9c4221ad
commit
461a57ec9a
|
@ -117,7 +117,7 @@ func CheckMessage(req PushNotification) error {
|
||||||
var msg string
|
var msg string
|
||||||
|
|
||||||
// ignore send topic mesaage from FCM
|
// 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"
|
msg = "the message must specify at least one registration ID"
|
||||||
LogAccess.Debug(msg)
|
LogAccess.Debug(msg)
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
|
|
|
@ -42,7 +42,9 @@ func GetAndroidNotification(req PushNotification) *fcm.Message {
|
||||||
DryRun: req.DryRun,
|
DryRun: req.DryRun,
|
||||||
}
|
}
|
||||||
|
|
||||||
notification.RegistrationIDs = req.Tokens
|
if len(req.Tokens) > 0 {
|
||||||
|
notification.RegistrationIDs = req.Tokens
|
||||||
|
}
|
||||||
|
|
||||||
if len(req.Priority) > 0 && req.Priority == "high" {
|
if len(req.Priority) > 0 && req.Priority == "high" {
|
||||||
notification.Priority = "high"
|
notification.Priority = "high"
|
||||||
|
@ -133,17 +135,24 @@ Retry:
|
||||||
var newTokens []string
|
var newTokens []string
|
||||||
// result from Send messages to specific devices
|
// result from Send messages to specific devices
|
||||||
for k, result := range res.Results {
|
for k, result := range res.Results {
|
||||||
|
to := ""
|
||||||
|
if k < len(req.Tokens) {
|
||||||
|
to = req.Tokens[k]
|
||||||
|
} else {
|
||||||
|
to = req.To
|
||||||
|
}
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
isError = true
|
isError = true
|
||||||
newTokens = append(newTokens, req.Tokens[k])
|
newTokens = append(newTokens, to)
|
||||||
LogPush(FailedPush, req.Tokens[k], req, result.Error)
|
LogPush(FailedPush, to, req, result.Error)
|
||||||
if PushConf.Core.Sync {
|
if PushConf.Core.Sync {
|
||||||
req.AddLog(getLogPushEntry(FailedPush, req.Tokens[k], req, result.Error))
|
req.AddLog(getLogPushEntry(FailedPush, to, req, result.Error))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
LogPush(SucceededPush, req.Tokens[k], req, nil)
|
LogPush(SucceededPush, to, req, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// result from Send messages to topics
|
// 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 {
|
if isError && retryCount < maxRetry {
|
||||||
retryCount++
|
retryCount++
|
||||||
|
|
||||||
|
|
|
@ -174,6 +174,32 @@ func TestSyncModeForTopicNotification(t *testing.T) {
|
||||||
assert.Equal(t, 1, len(logs))
|
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) {
|
func TestSetProxyURL(t *testing.T) {
|
||||||
|
|
||||||
err := SetProxy("87.236.233.92:8080")
|
err := SetProxy("87.236.233.92:8080")
|
||||||
|
|
|
@ -49,6 +49,9 @@ var (
|
||||||
|
|
||||||
// ErrInvalidParameters occurs when provided parameters have the right name and type
|
// 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")
|
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 (
|
var (
|
||||||
|
@ -147,7 +150,13 @@ func (r *Response) UnmarshalJSON(data []byte) error {
|
||||||
r.Success = response.Success
|
r.Success = response.Success
|
||||||
r.FailedRegistrationIDs = response.FailedRegistrationIDs
|
r.FailedRegistrationIDs = response.FailedRegistrationIDs
|
||||||
r.MessageID = response.MessageID
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -173,7 +182,13 @@ func (r *Result) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
r.MessageID = result.MessageID
|
r.MessageID = result.MessageID
|
||||||
r.RegistrationID = result.RegistrationID
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
"ignore": "test",
|
"ignore": "test",
|
||||||
"package": [
|
"package": [
|
||||||
{
|
{
|
||||||
"checksumSHA1": "TVTjsXflagrWbTXmbxPJdCtTFRo=",
|
"checksumSHA1": "1Jql7x7zDOmbDxGr4gsa8rFEC5g=",
|
||||||
"path": "github.com/appleboy/go-fcm",
|
"path": "github.com/appleboy/go-fcm",
|
||||||
"revision": "c12f9e2e95b14802da2b4d3807dd12ef0dd80a42",
|
"revision": "3bc382cee4180b7e4753761fda69a003de97b0e9",
|
||||||
"revisionTime": "2017-10-24T08:00:40Z"
|
"revisionTime": "2017-10-25T02:33:50Z",
|
||||||
|
"version": "0.1.1",
|
||||||
|
"versionExact": "0.1.1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"checksumSHA1": "Ab7MUtqX0iq2PUzzBxWpgzPSydw=",
|
"checksumSHA1": "Ab7MUtqX0iq2PUzzBxWpgzPSydw=",
|
||||||
|
|
Loading…
Reference in New Issue