Support Retry condition.

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2016-10-23 18:17:50 +08:00
parent 411f413484
commit f1428c1446
1 changed files with 31 additions and 2 deletions

View File

@ -59,6 +59,7 @@ type PushNotification struct {
ContentAvailable bool `json:"content_available,omitempty"` ContentAvailable bool `json:"content_available,omitempty"`
Sound string `json:"sound,omitempty"` Sound string `json:"sound,omitempty"`
Data D `json:"data,omitempty"` Data D `json:"data,omitempty"`
Retry int `json:"retry,omitempty"`
// Android // Android
APIKey string `json:"api_key,omitempty"` APIKey string `json:"api_key,omitempty"`
@ -328,7 +329,9 @@ func GetIOSNotification(req PushNotification) *apns.Notification {
func PushToIOS(req PushNotification) bool { func PushToIOS(req PushNotification) bool {
LogAccess.Debug("Start push notification for iOS") LogAccess.Debug("Start push notification for iOS")
var isError bool Retry:
var isError = false
var newTokens []string
notification := GetIOSNotification(req) notification := GetIOSNotification(req)
@ -341,8 +344,9 @@ func PushToIOS(req PushNotification) bool {
if err != nil { if err != nil {
// apns server error // apns server error
LogPush(FailedPush, token, req, err) LogPush(FailedPush, token, req, err)
isError = true
StatStorage.AddIosError(1) StatStorage.AddIosError(1)
newTokens = append(newTokens, token)
isError = true
continue continue
} }
@ -351,6 +355,8 @@ func PushToIOS(req PushNotification) bool {
// ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65 // ref: https://github.com/sideshow/apns2/blob/master/response.go#L14-L65
LogPush(FailedPush, token, req, errors.New(res.Reason)) LogPush(FailedPush, token, req, errors.New(res.Reason))
StatStorage.AddIosError(1) StatStorage.AddIosError(1)
newTokens = append(newTokens, token)
isError = true
continue continue
} }
@ -360,6 +366,15 @@ func PushToIOS(req PushNotification) bool {
} }
} }
if isError == true && req.Retry < PushConf.Ios.MaxRetry {
req.Retry++
// reset to default
req.Tokens = newTokens
isError = false
goto Retry
}
return isError return isError
} }
@ -414,6 +429,7 @@ func PushToAndroid(req PushNotification) bool {
LogAccess.Debug("Start push notification for Android") LogAccess.Debug("Start push notification for Android")
var APIKey string var APIKey string
var isError = false
// check message // check message
err := CheckMessage(req) err := CheckMessage(req)
@ -423,6 +439,7 @@ func PushToAndroid(req PushNotification) bool {
return false return false
} }
Retry:
notification := GetAndroidNotification(req) notification := GetAndroidNotification(req)
if APIKey = PushConf.Android.APIKey; req.APIKey != "" { if APIKey = PushConf.Android.APIKey; req.APIKey != "" {
@ -442,8 +459,11 @@ func PushToAndroid(req PushNotification) bool {
StatStorage.AddAndroidSuccess(int64(res.Success)) StatStorage.AddAndroidSuccess(int64(res.Success))
StatStorage.AddAndroidError(int64(res.Failure)) StatStorage.AddAndroidError(int64(res.Failure))
var newTokens []string
for k, result := range res.Results { for k, result := range res.Results {
if result.Error != "" { if result.Error != "" {
isError = true
newTokens = append(newTokens, req.Tokens[k])
LogPush(FailedPush, req.Tokens[k], req, errors.New(result.Error)) LogPush(FailedPush, req.Tokens[k], req, errors.New(result.Error))
continue continue
} }
@ -451,5 +471,14 @@ func PushToAndroid(req PushNotification) bool {
LogPush(SucceededPush, req.Tokens[k], req, nil) LogPush(SucceededPush, req.Tokens[k], req, nil)
} }
if isError == true && req.Retry < PushConf.Android.MaxRetry {
req.Retry++
// reset to default
req.Tokens = newTokens
isError = false
goto Retry
}
return true return true
} }