make Huawei configs compatible with HMS Core 4.0 (#561)

Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
kerem 2021-03-28 17:13:02 +03:00 committed by GitHub
parent d030d1ef6a
commit 9c51cbc989
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 49 deletions

View File

@ -146,7 +146,7 @@ android:
huawei: huawei:
enabled: true enabled: true
apikey: "YOUR_API_KEY" appsecret: "YOUR_APP_SECRET"
appid: "YOUR_APP_ID" appid: "YOUR_APP_ID"
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled
@ -288,8 +288,8 @@ Android Options:
-k, --apikey <api_key> Android API Key -k, --apikey <api_key> Android API Key
--android enabled android (default: false) --android enabled android (default: false)
Huawei Options: Huawei Options:
-hk, --hmskey <hms_key> HMS API Key -hk, --hmskey <hms_key> HMS App Secret
-hid, --hmsid <hms_id> HMS APP Id -hid, --hmsid <hms_id> HMS App ID
--huawei enabled huawei (default: false) --huawei enabled huawei (default: false)
Common Options: Common Options:
--topic <topic> iOS or Android topic message --topic <topic> iOS or Android topic message
@ -325,7 +325,7 @@ gorush --android --topic "/topics/foo-bar" \
Send single notification with the following command. Send single notification with the following command.
```bash ```bash
gorush -huawei -title "Gorush with HMS" -m "your message" -hk "API Key" -hid "APP Id" -t "Device token" gorush -huawei -title "Gorush with HMS" -m "your message" -hk "API Key" -hid "App ID" -t "Device token"
``` ```
Send messages to topics. Send messages to topics.
@ -335,7 +335,7 @@ gorush --huawei --topic "foo-bar" \
-title "Gorush with HMS" \ -title "Gorush with HMS" \
-m "This is a Huawei Mobile Services Topic Message" \ -m "This is a Huawei Mobile Services Topic Message" \
-hk "API Key" \ -hk "API Key" \
-hid "APP Id" -hid "App ID"
``` ```
- `-m`: Notification message. - `-m`: Notification message.

View File

@ -58,7 +58,7 @@ android:
huawei: huawei:
enabled: true enabled: true
apikey: "YOUR_API_KEY" appsecret: "YOUR_APP_SECRET"
appid: "YOUR_APP_ID" appid: "YOUR_APP_ID"
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled
@ -161,10 +161,10 @@ type SectionAndroid struct {
// SectionHuawei is sub section of config. // SectionHuawei is sub section of config.
type SectionHuawei struct { type SectionHuawei struct {
Enabled bool `yaml:"enabled"` Enabled bool `yaml:"enabled"`
APIKey string `yaml:"apikey"` AppSecret string `yaml:"appsecret"`
APPId string `yaml:"appid"` AppID string `yaml:"appid"`
MaxRetry int `yaml:"max_retry"` MaxRetry int `yaml:"max_retry"`
} }
// SectionIos is sub section of config. // SectionIos is sub section of config.
@ -317,8 +317,8 @@ func LoadConf(confPath string) (ConfYaml, error) {
// Huawei // Huawei
conf.Huawei.Enabled = viper.GetBool("huawei.enabled") conf.Huawei.Enabled = viper.GetBool("huawei.enabled")
conf.Huawei.APIKey = viper.GetString("huawei.apikey") conf.Huawei.AppSecret = viper.GetString("huawei.appsecret")
conf.Huawei.APPId = viper.GetString("huawei.appid") conf.Huawei.AppID = viper.GetString("huawei.appid")
conf.Huawei.MaxRetry = viper.GetInt("huawei.max_retry") conf.Huawei.MaxRetry = viper.GetInt("huawei.max_retry")
// iOS // iOS

View File

@ -45,7 +45,7 @@ android:
huawei: huawei:
enabled: true enabled: true
apikey: "YOUR_API_KEY" appsecret: "YOUR_APP_SECRET"
appid: "YOUR_APP_ID" appid: "YOUR_APP_ID"
max_retry: 0 # resend fail notification, default value zero is disabled max_retry: 0 # resend fail notification, default value zero is disabled

View File

@ -81,7 +81,8 @@ type PushNotification struct {
Notification *fcm.Notification `json:"notification,omitempty"` Notification *fcm.Notification `json:"notification,omitempty"`
// Huawei // Huawei
APPId string `json:"app_id,omitempty"` AppID string `json:"app_id,omitempty"`
AppSecret string `json:"app_secret,omitempty"`
HuaweiNotification *model.AndroidNotification `json:"huawei_notification,omitempty"` HuaweiNotification *model.AndroidNotification `json:"huawei_notification,omitempty"`
HuaweiData string `json:"huawei_data,omitempty"` HuaweiData string `json:"huawei_data,omitempty"`
HuaweiCollapseKey int `json:"huawei_collapse_key,omitempty"` HuaweiCollapseKey int `json:"huawei_collapse_key,omitempty"`
@ -221,12 +222,12 @@ func CheckPushConf() error {
} }
if PushConf.Huawei.Enabled { if PushConf.Huawei.Enabled {
if PushConf.Huawei.APIKey == "" { if PushConf.Huawei.AppSecret == "" {
return errors.New("Missing Huawei API Key") return errors.New("Missing Huawei App Secret")
} }
if PushConf.Huawei.APPId == "" { if PushConf.Huawei.AppID == "" {
return errors.New("Missing Huawei APP Id") return errors.New("Missing Huawei App ID")
} }
} }

View File

@ -32,23 +32,23 @@ func GetPushClient(conf *config.Config) (*core.HMSClient, error) {
} }
// InitHMSClient use for initialize HMS Client. // InitHMSClient use for initialize HMS Client.
func InitHMSClient(apiKey, appID string) (*core.HMSClient, error) { func InitHMSClient(appSecret, appID string) (*core.HMSClient, error) {
if apiKey == "" { if appSecret == "" {
return nil, errors.New("Missing Huawei API Key") return nil, errors.New("Missing Huawei App Secret")
} }
if appID == "" { if appID == "" {
return nil, errors.New("Missing Huawei APP Id") return nil, errors.New("Missing Huawei App ID")
} }
conf := &config.Config{ conf := &config.Config{
AppId: appID, AppId: appID,
AppSecret: apiKey, AppSecret: appSecret,
AuthUrl: "https://login.cloud.huawei.com/oauth2/v2/token", AuthUrl: "https://oauth-login.cloud.huawei.com/oauth2/v3/token",
PushUrl: "https://api.push.hicloud.com", PushUrl: "https://push-api.cloud.huawei.com",
} }
if apiKey != PushConf.Huawei.APIKey || appID != PushConf.Huawei.APPId { if appSecret != PushConf.Huawei.AppSecret || appID != PushConf.Huawei.AppID {
return GetPushClient(conf) return GetPushClient(conf)
} }
@ -187,7 +187,7 @@ Retry:
notification, _ := GetHuaweiNotification(req) notification, _ := GetHuaweiNotification(req)
client, err = InitHMSClient(PushConf.Huawei.APIKey, PushConf.Huawei.APPId) client, err = InitHMSClient(PushConf.Huawei.AppSecret, PushConf.Huawei.AppID)
if err != nil { if err != nil {
// HMS server error // HMS server error

View File

@ -26,42 +26,42 @@ func init() {
} }
} }
func TestMissingHuaweiAPIKey(t *testing.T) { func TestMissingHuaweiAppSecret(t *testing.T) {
PushConf, _ = config.LoadConf("") PushConf, _ = config.LoadConf("")
PushConf.Huawei.Enabled = true PushConf.Huawei.Enabled = true
PushConf.Huawei.APIKey = "" PushConf.Huawei.AppSecret = ""
err := CheckPushConf() err := CheckPushConf()
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Missing Huawei API Key", err.Error()) assert.Equal(t, "Missing Huawei App Secret", err.Error())
} }
func TestMissingHuaweiAPPId(t *testing.T) { func TestMissingHuaweiAppID(t *testing.T) {
PushConf, _ = config.LoadConf("") PushConf, _ = config.LoadConf("")
PushConf.Huawei.Enabled = true PushConf.Huawei.Enabled = true
PushConf.Huawei.APPId = "" PushConf.Huawei.AppID = ""
err := CheckPushConf() err := CheckPushConf()
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Missing Huawei APP Id", err.Error()) assert.Equal(t, "Missing Huawei App ID", err.Error())
} }
func TestMissingKeyForInitHMSClient(t *testing.T) { func TestMissingAppSecretForInitHMSClient(t *testing.T) {
client, err := InitHMSClient("", "APP_ID") client, err := InitHMSClient("", "APP_SECRET")
assert.Nil(t, client) assert.Nil(t, client)
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Missing Huawei API Key", err.Error()) assert.Equal(t, "Missing Huawei App Secret", err.Error())
} }
func TestMissingAppIDForInitHMSClient(t *testing.T) { func TestMissingAppIDForInitHMSClient(t *testing.T) {
client, err := InitHMSClient("APP_KEY", "") client, err := InitHMSClient("APP_ID", "")
assert.Nil(t, client) assert.Nil(t, client)
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, "Missing Huawei APP Id", err.Error()) assert.Equal(t, "Missing Huawei App ID", err.Error())
} }

22
main.go
View File

@ -66,10 +66,10 @@ func main() {
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush") flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush") flag.StringVar(&opts.Android.APIKey, "k", "", "Android api key configuration for gorush")
flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush") flag.StringVar(&opts.Android.APIKey, "apikey", "", "Android api key configuration for gorush")
flag.StringVar(&opts.Huawei.APIKey, "hk", "", "Huawei api key configuration for gorush") flag.StringVar(&opts.Huawei.AppSecret, "hk", "", "Huawei api key configuration for gorush")
flag.StringVar(&opts.Huawei.APIKey, "hmskey", "", "Huawei api key configuration for gorush") flag.StringVar(&opts.Huawei.AppSecret, "hmskey", "", "Huawei api key configuration for gorush")
flag.StringVar(&opts.Huawei.APPId, "hid", "", "HMS app id configuration for gorush") flag.StringVar(&opts.Huawei.AppID, "hid", "", "HMS app id configuration for gorush")
flag.StringVar(&opts.Huawei.APPId, "hmsid", "", "HMS app id configuration for gorush") flag.StringVar(&opts.Huawei.AppID, "hmsid", "", "HMS app id configuration for gorush")
flag.StringVar(&opts.Core.Address, "A", "", "address to bind") flag.StringVar(&opts.Core.Address, "A", "", "address to bind")
flag.StringVar(&opts.Core.Address, "address", "", "address to bind") flag.StringVar(&opts.Core.Address, "address", "", "address to bind")
flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush") flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush")
@ -134,12 +134,12 @@ func main() {
gorush.PushConf.Android.APIKey = opts.Android.APIKey gorush.PushConf.Android.APIKey = opts.Android.APIKey
} }
if opts.Huawei.APIKey != "" { if opts.Huawei.AppSecret != "" {
gorush.PushConf.Huawei.APIKey = opts.Huawei.APIKey gorush.PushConf.Huawei.AppSecret = opts.Huawei.AppSecret
} }
if opts.Huawei.APPId != "" { if opts.Huawei.AppID != "" {
gorush.PushConf.Huawei.APPId = opts.Huawei.APPId gorush.PushConf.Huawei.AppID = opts.Huawei.AppID
} }
if opts.Stat.Engine != "" { if opts.Stat.Engine != "" {
@ -331,7 +331,7 @@ func main() {
gorush.LogError.Fatal(err) gorush.LogError.Fatal(err)
} }
if _, err = gorush.InitHMSClient(gorush.PushConf.Huawei.APIKey, gorush.PushConf.Huawei.APPId); err != nil { if _, err = gorush.InitHMSClient(gorush.PushConf.Huawei.AppSecret, gorush.PushConf.Huawei.AppID); err != nil {
gorush.LogError.Fatal(err) gorush.LogError.Fatal(err)
} }
@ -394,8 +394,8 @@ Android Options:
-k, --apikey <api_key> Android API Key -k, --apikey <api_key> Android API Key
--android enabled android (default: false) --android enabled android (default: false)
Huawei Options: Huawei Options:
-hk, --hmskey <hms_key> HMS API Key -hk, --hmskey <hms_key> HMS App Secret
-hid, --hmsid <hms_id> HMS APP Id -hid, --hmsid <hms_id> HMS App ID
--huawei enabled huawei (default: false) --huawei enabled huawei (default: false)
Common Options: Common Options:
--topic <topic> iOS, Android or Huawei topic message --topic <topic> iOS, Android or Huawei topic message