From 64017d16e265ff1159e98b3dc393284c0ddeceb7 Mon Sep 17 00:00:00 2001 From: appleboy Date: Fri, 7 Jun 2024 21:36:19 +0800 Subject: [PATCH] refactor: refactor FCM client initialization and configuration - 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 --- main.go | 12 ++++++++---- notify/notification.go | 2 +- notify/notification_fcm.go | 34 ++++++++++++++++----------------- notify/notification_fcm_test.go | 3 ++- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index db6a7b08..4198456f 100644 --- a/main.go +++ b/main.go @@ -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") @@ -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 } @@ -189,7 +193,7 @@ func main() { // send topic message if topic != "" { - req.To = topic + req.Topic = topic } err := notify.CheckMessage(req) @@ -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) } } diff --git a/notify/notification.go b/notify/notification.go index da3f9445..db65c3ba 100644 --- a/notify/notification.go +++ b/notify/notification.go @@ -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"` @@ -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"` diff --git a/notify/notification_fcm.go b/notify/notification_fcm.go index 1e9adacd..8496f20b 100644 --- a/notify/notification_fcm.go +++ b/notify/notification_fcm.go @@ -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. @@ -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) diff --git a/notify/notification_fcm_test.go b/notify/notification_fcm_test.go index 854c66e7..55c8ab69 100644 --- a/notify/notification_fcm_test.go +++ b/notify/notification_fcm_test.go @@ -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)