refactor(andorid): initial client. (#258)
* refactor(andorid): initial client.
This commit is contained in:
parent
fadf9d280d
commit
430de17755
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/appleboy/gorush/config"
|
||||
"github.com/appleboy/gorush/storage"
|
||||
|
||||
"github.com/appleboy/go-fcm"
|
||||
apns "github.com/sideshow/apns2"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
@ -19,6 +20,8 @@ var (
|
|||
CertificatePemIos tls.Certificate
|
||||
// ApnsClient is apns client
|
||||
ApnsClient *apns.Client
|
||||
// FCMClient is apns client
|
||||
FCMClient *fcm.Client
|
||||
// LogAccess is log server request log
|
||||
LogAccess *logrus.Logger
|
||||
// LogError is log server error log
|
||||
|
|
|
@ -1,11 +1,32 @@
|
|||
package gorush
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"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.
|
||||
// HTTP Connection Server Reference for Android
|
||||
// https://firebase.google.com/docs/cloud-messaging/http-server-ref
|
||||
|
@ -60,7 +81,7 @@ func PushToAndroid(req PushNotification) bool {
|
|||
}
|
||||
|
||||
var (
|
||||
APIKey string
|
||||
client *fcm.Client
|
||||
retryCount = 0
|
||||
maxRetry = PushConf.Android.MaxRetry
|
||||
)
|
||||
|
@ -79,13 +100,15 @@ func PushToAndroid(req PushNotification) bool {
|
|||
|
||||
Retry:
|
||||
var isError = false
|
||||
|
||||
notification := GetAndroidNotification(req)
|
||||
|
||||
if APIKey = PushConf.Android.APIKey; req.APIKey != "" {
|
||||
APIKey = req.APIKey
|
||||
if req.APIKey != "" {
|
||||
client, err = InitFCMClient(req.APIKey)
|
||||
} else {
|
||||
client, err = InitFCMClient(PushConf.Android.APIKey)
|
||||
}
|
||||
|
||||
client, err := fcm.NewClient(APIKey)
|
||||
if err != nil {
|
||||
// FCM server error
|
||||
LogError.Error("FCM server error: " + err.Error())
|
||||
|
@ -94,8 +117,8 @@ Retry:
|
|||
|
||||
res, err := client.Send(notification)
|
||||
if err != nil {
|
||||
// FCM server error
|
||||
LogError.Error("FCM server error: " + err.Error())
|
||||
// Send Message error
|
||||
LogError.Error("FCM server send message error: " + err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
|
@ -447,98 +447,6 @@ func TestPushToIOS(t *testing.T) {
|
|||
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) {
|
||||
PushConf = config.BuildDefaultPushConf()
|
||||
|
||||
|
@ -668,7 +576,7 @@ func TestDisabledIosNotifications(t *testing.T) {
|
|||
},
|
||||
// android
|
||||
{
|
||||
Tokens: []string{androidToken, "bbbbb"},
|
||||
Tokens: []string{androidToken, androidToken + "_"},
|
||||
Platform: PlatFormAndroid,
|
||||
Message: "Welcome",
|
||||
},
|
||||
|
|
13
main.go
13
main.go
|
@ -290,14 +290,19 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if err = gorush.InitAPNSClient(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
gorush.InitWorkers(gorush.PushConf.Core.WorkerNum, gorush.PushConf.Core.QueueNum)
|
||||
|
||||
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 {
|
||||
// Run httpd server
|
||||
return gorush.RunHTTPServer()
|
||||
|
|
Loading…
Reference in New Issue