Skip to content

Commit

Permalink
refactor: refactor FCM client initialization and configuration
Browse files Browse the repository at this point in the history
- Update Android flag variables to include `KeyPath` and `Credential`
- Add conditional check and assignment for `KeyPath` in the main function
- Change `req.To` to `req.Topic` for FCM topic assignment
- Simplify FCM client initialization by removing the key parameter and using options
- Add `Topic` field to `PushNotification` struct and remove duplicate `Topic` field
- Modify FCM client initialization to handle both `KeyPath` and `Credential`
- Update test for FCM client initialization to reflect changes in configuration handling

Signed-off-by: appleboy <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Jun 7, 2024
1 parent 2923b97 commit 64017d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func main() {
flag.StringVar(&opts.Ios.TeamID, "team-id", "", "iOS Team ID for P8 token")
flag.StringVar(&opts.Ios.Password, "P", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Ios.Password, "password", "", "iOS certificate password for gorush")
flag.StringVar(&opts.Android.Credential, "k", "", "FCM credential configuration for gorush")
flag.StringVar(&opts.Android.Credential, "apikey", "", "FCM credential configuration for gorush")
flag.StringVar(&opts.Android.KeyPath, "fcm-key", "", "FCM key path configuration for gorush")
flag.StringVar(&opts.Android.Credential, "fcm-credential", "", "FCM credential 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")
Expand Down Expand Up @@ -118,6 +118,10 @@ func main() {
cfg.Ios.Password = opts.Ios.Password
}

if opts.Android.KeyPath != "" {
cfg.Android.KeyPath = opts.Android.KeyPath
}

if opts.Android.Credential != "" {
cfg.Android.Credential = opts.Android.Credential
}
Expand Down Expand Up @@ -189,7 +193,7 @@ func main() {

// send topic message
if topic != "" {
req.To = topic
req.Topic = topic
}

err := notify.CheckMessage(req)
Expand Down Expand Up @@ -371,7 +375,7 @@ func main() {
}

if cfg.Android.Enabled {
if _, err = notify.InitFCMClient(cfg, cfg.Android.Credential); err != nil {
if _, err = notify.InitFCMClient(cfg); err != nil {
logx.LogError.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion notify/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type PushNotification struct {
// Common
ID string `json:"notif_id,omitempty"`
To string `json:"to,omitempty"`
Topic string `json:"topic,omitempty"` // FCM and iOS only
Tokens []string `json:"tokens" binding:"required"`
Platform int `json:"platform" binding:"required"`
Message string `json:"message,omitempty"`
Expand Down Expand Up @@ -101,7 +102,6 @@ type PushNotification struct {
Expiration *int64 `json:"expiration,omitempty"`
ApnsID string `json:"apns_id,omitempty"`
CollapseID string `json:"collapse_id,omitempty"`
Topic string `json:"topic,omitempty"`
PushType string `json:"push_type,omitempty"`
Badge *int `json:"badge,omitempty"`
Category string `json:"category,omitempty"`
Expand Down
34 changes: 17 additions & 17 deletions notify/notification_fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,29 @@ import (
)

// InitFCMClient use for initialize FCM Client.
func InitFCMClient(cfg *config.ConfYaml, key string) (*fcm.Client, error) {
var err error
func InitFCMClient(cfg *config.ConfYaml) (*fcm.Client, error) {
var opts []fcm.Option

if key == "" && cfg.Android.Credential == "" {
return nil, errors.New("missing android api key")
if cfg.Android.KeyPath == "" && cfg.Android.Credential == "" {
return nil, errors.New("missing fcm credential data")
}

if key != "" && key != cfg.Android.Credential {
return fcm.NewClient(
context.Background(),
fcm.WithCredentialsJSON([]byte(key)),
)
if cfg.Android.KeyPath != "" {
opts = append(opts, fcm.WithCredentialsFile(cfg.Android.KeyPath))
}

if FCMClient == nil {
FCMClient, err = fcm.NewClient(
context.Background(),
fcm.WithCredentialsJSON([]byte(cfg.Android.Credential)),
)
return FCMClient, err
if cfg.Android.Credential != "" {
opts = append(opts, fcm.WithCredentialsJSON([]byte(cfg.Android.Credential)))
}

return FCMClient, nil
if FCMClient != nil {
return FCMClient, nil
}

return fcm.NewClient(
context.Background(),
opts...,
)
}

// GetAndroidNotification use for define Android notification.
Expand Down Expand Up @@ -102,7 +102,7 @@ func PushToAndroid(req *PushNotification, cfg *config.ConfYaml) (resp *ResponseP
}

resp = &ResponsePush{}
client, err = InitFCMClient(cfg, cfg.Android.Credential)
client, err = InitFCMClient(cfg)

Retry:
notification := GetAndroidNotification(req)
Expand Down
3 changes: 2 additions & 1 deletion notify/notification_fcm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestMissingAndroidCredential(t *testing.T) {
func TestMissingKeyForInitFCMClient(t *testing.T) {
cfg, _ := config.LoadConf()
cfg.Android.Credential = ""
client, err := InitFCMClient(cfg, "")
cfg.Android.KeyPath = ""
client, err := InitFCMClient(cfg)

assert.Nil(t, client)
assert.Error(t, err)
Expand Down

0 comments on commit 64017d1

Please sign in to comment.