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:
		
							parent
							
								
									d030d1ef6a
								
							
						
					
					
						commit
						9c51cbc989
					
				
							
								
								
									
										10
									
								
								README.md
								
								
								
								
							
							
						
						
									
										10
									
								
								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 <api_key>           Android API Key
 | 
			
		||||
    --android                        enabled android (default: false)
 | 
			
		||||
Huawei Options:
 | 
			
		||||
    -hk, --hmskey <hms_key>          HMS API Key
 | 
			
		||||
    -hid, --hmsid <hms_id>           HMS APP Id
 | 
			
		||||
    -hk, --hmskey <hms_key>          HMS App Secret
 | 
			
		||||
    -hid, --hmsid <hms_id>           HMS App ID
 | 
			
		||||
    --huawei                         enabled huawei (default: false)
 | 
			
		||||
Common Options:
 | 
			
		||||
    --topic <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.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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())
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										22
									
								
								main.go
								
								
								
								
							
							
						
						
									
										22
									
								
								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 <api_key>           Android API Key
 | 
			
		||||
    --android                        enabled android (default: false)
 | 
			
		||||
Huawei Options:
 | 
			
		||||
    -hk, --hmskey <hms_key>          HMS API Key
 | 
			
		||||
    -hid, --hmsid <hms_id>			 HMS APP Id
 | 
			
		||||
    -hk, --hmskey <hms_key>          HMS App Secret
 | 
			
		||||
    -hid, --hmsid <hms_id>			 HMS App ID
 | 
			
		||||
    --huawei                         enabled huawei (default: false)
 | 
			
		||||
Common Options:
 | 
			
		||||
    --topic <topic>                  iOS, Android or Huawei topic message
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue