refactor(andorid): initial client. (#258)

* refactor(andorid): initial client.
This commit is contained in:
Bo-Yi Wu 2017-07-25 16:41:30 +08:00 committed by GitHub
parent fadf9d280d
commit 430de17755
5 changed files with 147 additions and 103 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/storage" "github.com/appleboy/gorush/storage"
"github.com/appleboy/go-fcm"
apns "github.com/sideshow/apns2" apns "github.com/sideshow/apns2"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -19,6 +20,8 @@ var (
CertificatePemIos tls.Certificate CertificatePemIos tls.Certificate
// ApnsClient is apns client // ApnsClient is apns client
ApnsClient *apns.Client ApnsClient *apns.Client
// FCMClient is apns client
FCMClient *fcm.Client
// LogAccess is log server request log // LogAccess is log server request log
LogAccess *logrus.Logger LogAccess *logrus.Logger
// LogError is log server error log // LogError is log server error log

View File

@ -1,11 +1,32 @@
package gorush package gorush
import ( import (
"errors"
"fmt" "fmt"
"github.com/appleboy/go-fcm" "github.com/appleboy/go-fcm"
) )
// InitFCMClient use for initialize FCM Client.
func InitFCMClient(key string) (*fcm.Client, error) {
var err error
if key == "" {
return nil, errors.New("Missing Android API Key")
}
if key != PushConf.Android.APIKey {
return fcm.NewClient(key)
}
if FCMClient == nil {
FCMClient, err = fcm.NewClient(key)
return FCMClient, err
}
return FCMClient, nil
}
// GetAndroidNotification use for define Android notification. // GetAndroidNotification use for define Android notification.
// HTTP Connection Server Reference for Android // HTTP Connection Server Reference for Android
// https://firebase.google.com/docs/cloud-messaging/http-server-ref // https://firebase.google.com/docs/cloud-messaging/http-server-ref
@ -60,7 +81,7 @@ func PushToAndroid(req PushNotification) bool {
} }
var ( var (
APIKey string client *fcm.Client
retryCount = 0 retryCount = 0
maxRetry = PushConf.Android.MaxRetry maxRetry = PushConf.Android.MaxRetry
) )
@ -79,13 +100,15 @@ func PushToAndroid(req PushNotification) bool {
Retry: Retry:
var isError = false var isError = false
notification := GetAndroidNotification(req) notification := GetAndroidNotification(req)
if APIKey = PushConf.Android.APIKey; req.APIKey != "" { if req.APIKey != "" {
APIKey = req.APIKey client, err = InitFCMClient(req.APIKey)
} else {
client, err = InitFCMClient(PushConf.Android.APIKey)
} }
client, err := fcm.NewClient(APIKey)
if err != nil { if err != nil {
// FCM server error // FCM server error
LogError.Error("FCM server error: " + err.Error()) LogError.Error("FCM server error: " + err.Error())
@ -94,8 +117,8 @@ Retry:
res, err := client.Send(notification) res, err := client.Send(notification)
if err != nil { if err != nil {
// FCM server error // Send Message error
LogError.Error("FCM server error: " + err.Error()) LogError.Error("FCM server send message error: " + err.Error())
return false return false
} }

View File

@ -0,0 +1,105 @@
package gorush
import (
"log"
"os"
"testing"
"github.com/appleboy/gorush/config"
"github.com/stretchr/testify/assert"
)
func init() {
PushConf = config.BuildDefaultPushConf()
if err := InitLog(); err != nil {
log.Fatal(err)
}
if err := InitAppStatus(); err != nil {
log.Fatal(err)
}
}
func TestMissingKeyForInitFCMClient(t *testing.T) {
client, err := InitFCMClient("")
assert.Nil(t, client)
assert.Error(t, err)
assert.Equal(t, "Missing Android API Key", err.Error())
}
func TestPushToAndroidWrongToken(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
}
// Android Success count: 0, Failure count: 2
isError := PushToAndroid(req)
assert.True(t, isError)
}
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
// log for json
PushConf.Log.Format = "json"
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken},
Platform: PlatFormAndroid,
Message: "Welcome",
}
isError := PushToAndroid(req)
assert.False(t, isError)
}
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken},
Platform: PlatFormAndroid,
Message: "Welcome",
}
isError := PushToAndroid(req)
assert.False(t, isError)
}
func TestOverwriteAndroidAPIKey(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
// overwrite android api key
APIKey: "1234",
}
// FCM server error: 401 error: 401 Unauthorized (Wrong API Key)
err := PushToAndroid(req)
assert.False(t, err)
}

View File

@ -447,98 +447,6 @@ func TestPushToIOS(t *testing.T) {
assert.True(t, isError) assert.True(t, isError)
} }
func TestPushToAndroidWrongAPIKey(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY") + "a"
req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
}
// FCM server error: 401 error: 401 Unauthorized
err := PushToAndroid(req)
assert.False(t, err)
}
func TestPushToAndroidWrongToken(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
req := PushNotification{
Tokens: []string{"aaaaaa", "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
}
isError := PushToAndroid(req)
assert.True(t, isError)
}
func TestPushToAndroidRightTokenForJSONLog(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
// log for json
PushConf.Log.Format = "json"
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken},
Platform: PlatFormAndroid,
Message: "Welcome",
}
isError := PushToAndroid(req)
assert.False(t, isError)
}
func TestPushToAndroidRightTokenForStringLog(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
}
isError := PushToAndroid(req)
assert.True(t, isError)
}
func TestOverwriteAndroidAPIKey(t *testing.T) {
PushConf = config.BuildDefaultPushConf()
PushConf.Android.Enabled = true
PushConf.Android.APIKey = os.Getenv("ANDROID_API_KEY")
androidToken := os.Getenv("ANDROID_TEST_TOKEN")
req := PushNotification{
Tokens: []string{androidToken, "bbbbb"},
Platform: PlatFormAndroid,
Message: "Welcome",
// overwrite android api key
APIKey: "1234",
}
// FCM server error: 401 error: 401 Unauthorized
err := PushToAndroid(req)
assert.False(t, err)
}
func TestSenMultipleNotifications(t *testing.T) { func TestSenMultipleNotifications(t *testing.T) {
PushConf = config.BuildDefaultPushConf() PushConf = config.BuildDefaultPushConf()
@ -668,7 +576,7 @@ func TestDisabledIosNotifications(t *testing.T) {
}, },
// android // android
{ {
Tokens: []string{androidToken, "bbbbb"}, Tokens: []string{androidToken, androidToken + "_"},
Platform: PlatFormAndroid, Platform: PlatFormAndroid,
Message: "Welcome", Message: "Welcome",
}, },

13
main.go
View File

@ -290,14 +290,19 @@ func main() {
return return
} }
if err = gorush.InitAPNSClient(); err != nil {
return
}
gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum) gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
var g errgroup.Group var g errgroup.Group
g.Go(func() error {
return gorush.InitAPNSClient()
})
g.Go(func() error {
_, err := gorush.InitFCMClient(gorush.PushConf.Android.APIKey)
return err
})
g.Go(func() error { g.Go(func() error {
// Run httpd server // Run httpd server
return gorush.RunHTTPServer() return gorush.RunHTTPServer()