From 9c51cbc989df406346e401869b4c2b36acec0236 Mon Sep 17 00:00:00 2001 From: kerem Date: Sun, 28 Mar 2021 17:13:02 +0300 Subject: [PATCH] make Huawei configs compatible with HMS Core 4.0 (#561) Co-authored-by: Bo-Yi Wu --- README.md | 10 +++++----- config/config.go | 14 +++++++------- config/testdata/config.yml | 2 +- gorush/notification.go | 11 ++++++----- gorush/notification_hms.go | 18 +++++++++--------- gorush/notification_hms_test.go | 22 +++++++++++----------- main.go | 22 +++++++++++----------- 7 files changed, 50 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index c484fde..a7661a6 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ android: huawei: enabled: true - apikey: "YOUR_API_KEY" + appsecret: "YOUR_APP_SECRET" appid: "YOUR_APP_ID" max_retry: 0 # resend fail notification, default value zero is disabled @@ -288,8 +288,8 @@ Android Options: -k, --apikey Android API Key --android enabled android (default: false) Huawei Options: - -hk, --hmskey HMS API Key - -hid, --hmsid HMS APP Id + -hk, --hmskey HMS App Secret + -hid, --hmsid HMS App ID --huawei enabled huawei (default: false) Common Options: --topic iOS or Android topic message @@ -325,7 +325,7 @@ gorush --android --topic "/topics/foo-bar" \ Send single notification with the following command. ```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. @@ -335,7 +335,7 @@ gorush --huawei --topic "foo-bar" \ -title "Gorush with HMS" \ -m "This is a Huawei Mobile Services Topic Message" \ -hk "API Key" \ - -hid "APP Id" + -hid "App ID" ``` - `-m`: Notification message. diff --git a/config/config.go b/config/config.go index 6a761d5..ce37a7c 100644 --- a/config/config.go +++ b/config/config.go @@ -58,7 +58,7 @@ android: huawei: enabled: true - apikey: "YOUR_API_KEY" + appsecret: "YOUR_APP_SECRET" appid: "YOUR_APP_ID" max_retry: 0 # resend fail notification, default value zero is disabled @@ -161,10 +161,10 @@ type SectionAndroid struct { // SectionHuawei is sub section of config. type SectionHuawei struct { - Enabled bool `yaml:"enabled"` - APIKey string `yaml:"apikey"` - APPId string `yaml:"appid"` - MaxRetry int `yaml:"max_retry"` + Enabled bool `yaml:"enabled"` + AppSecret string `yaml:"appsecret"` + AppID string `yaml:"appid"` + MaxRetry int `yaml:"max_retry"` } // SectionIos is sub section of config. @@ -317,8 +317,8 @@ func LoadConf(confPath string) (ConfYaml, error) { // Huawei conf.Huawei.Enabled = viper.GetBool("huawei.enabled") - conf.Huawei.APIKey = viper.GetString("huawei.apikey") - conf.Huawei.APPId = viper.GetString("huawei.appid") + conf.Huawei.AppSecret = viper.GetString("huawei.appsecret") + conf.Huawei.AppID = viper.GetString("huawei.appid") conf.Huawei.MaxRetry = viper.GetInt("huawei.max_retry") // iOS diff --git a/config/testdata/config.yml b/config/testdata/config.yml index 4ddad6b..34d7861 100644 --- a/config/testdata/config.yml +++ b/config/testdata/config.yml @@ -45,7 +45,7 @@ android: huawei: enabled: true - apikey: "YOUR_API_KEY" + appsecret: "YOUR_APP_SECRET" appid: "YOUR_APP_ID" max_retry: 0 # resend fail notification, default value zero is disabled diff --git a/gorush/notification.go b/gorush/notification.go index 2c8bb71..87ec42c 100644 --- a/gorush/notification.go +++ b/gorush/notification.go @@ -81,7 +81,8 @@ type PushNotification struct { Notification *fcm.Notification `json:"notification,omitempty"` // 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"` HuaweiData string `json:"huawei_data,omitempty"` HuaweiCollapseKey int `json:"huawei_collapse_key,omitempty"` @@ -221,12 +222,12 @@ func CheckPushConf() error { } if PushConf.Huawei.Enabled { - if PushConf.Huawei.APIKey == "" { - return errors.New("Missing Huawei API Key") + if PushConf.Huawei.AppSecret == "" { + return errors.New("Missing Huawei App Secret") } - if PushConf.Huawei.APPId == "" { - return errors.New("Missing Huawei APP Id") + if PushConf.Huawei.AppID == "" { + return errors.New("Missing Huawei App ID") } } diff --git a/gorush/notification_hms.go b/gorush/notification_hms.go index b0ea0f5..6d373fe 100644 --- a/gorush/notification_hms.go +++ b/gorush/notification_hms.go @@ -32,23 +32,23 @@ func GetPushClient(conf *config.Config) (*core.HMSClient, error) { } // InitHMSClient use for initialize HMS Client. -func InitHMSClient(apiKey, appID string) (*core.HMSClient, error) { - if apiKey == "" { - return nil, errors.New("Missing Huawei API Key") +func InitHMSClient(appSecret, appID string) (*core.HMSClient, error) { + if appSecret == "" { + return nil, errors.New("Missing Huawei App Secret") } if appID == "" { - return nil, errors.New("Missing Huawei APP Id") + return nil, errors.New("Missing Huawei App ID") } conf := &config.Config{ AppId: appID, - AppSecret: apiKey, - AuthUrl: "https://login.cloud.huawei.com/oauth2/v2/token", - PushUrl: "https://api.push.hicloud.com", + AppSecret: appSecret, + AuthUrl: "https://oauth-login.cloud.huawei.com/oauth2/v3/token", + 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) } @@ -187,7 +187,7 @@ Retry: notification, _ := GetHuaweiNotification(req) - client, err = InitHMSClient(PushConf.Huawei.APIKey, PushConf.Huawei.APPId) + client, err = InitHMSClient(PushConf.Huawei.AppSecret, PushConf.Huawei.AppID) if err != nil { // HMS server error diff --git a/gorush/notification_hms_test.go b/gorush/notification_hms_test.go index abdc1f3..81dcd34 100644 --- a/gorush/notification_hms_test.go +++ b/gorush/notification_hms_test.go @@ -26,42 +26,42 @@ func init() { } } -func TestMissingHuaweiAPIKey(t *testing.T) { +func TestMissingHuaweiAppSecret(t *testing.T) { PushConf, _ = config.LoadConf("") PushConf.Huawei.Enabled = true - PushConf.Huawei.APIKey = "" + PushConf.Huawei.AppSecret = "" err := CheckPushConf() 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.Huawei.Enabled = true - PushConf.Huawei.APPId = "" + PushConf.Huawei.AppID = "" err := CheckPushConf() 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) { - client, err := InitHMSClient("", "APP_ID") +func TestMissingAppSecretForInitHMSClient(t *testing.T) { + client, err := InitHMSClient("", "APP_SECRET") assert.Nil(t, client) 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) { - client, err := InitHMSClient("APP_KEY", "") + client, err := InitHMSClient("APP_ID", "") assert.Nil(t, client) assert.Error(t, err) - assert.Equal(t, "Missing Huawei APP Id", err.Error()) + assert.Equal(t, "Missing Huawei App ID", err.Error()) } diff --git a/main.go b/main.go index 1f6b356..38ab6ec 100644 --- a/main.go +++ b/main.go @@ -66,10 +66,10 @@ func main() { 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, "apikey", "", "Android api key configuration for gorush") - flag.StringVar(&opts.Huawei.APIKey, "hk", "", "Huawei api key configuration for gorush") - flag.StringVar(&opts.Huawei.APIKey, "hmskey", "", "Huawei api key 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.AppSecret, "hk", "", "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, "hmsid", "", "HMS app id configuration for gorush") flag.StringVar(&opts.Core.Address, "A", "", "address to bind") flag.StringVar(&opts.Core.Address, "address", "", "address to bind") flag.StringVar(&opts.Core.Port, "p", "", "port number for gorush") @@ -134,12 +134,12 @@ func main() { gorush.PushConf.Android.APIKey = opts.Android.APIKey } - if opts.Huawei.APIKey != "" { - gorush.PushConf.Huawei.APIKey = opts.Huawei.APIKey + if opts.Huawei.AppSecret != "" { + gorush.PushConf.Huawei.AppSecret = opts.Huawei.AppSecret } - if opts.Huawei.APPId != "" { - gorush.PushConf.Huawei.APPId = opts.Huawei.APPId + if opts.Huawei.AppID != "" { + gorush.PushConf.Huawei.AppID = opts.Huawei.AppID } if opts.Stat.Engine != "" { @@ -331,7 +331,7 @@ func main() { 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) } @@ -394,8 +394,8 @@ Android Options: -k, --apikey Android API Key --android enabled android (default: false) Huawei Options: - -hk, --hmskey HMS API Key - -hid, --hmsid HMS APP Id + -hk, --hmskey HMS App Secret + -hid, --hmsid HMS App ID --huawei enabled huawei (default: false) Common Options: --topic iOS, Android or Huawei topic message