diff --git a/model/account/type.go b/model/account/type.go index 9ff184985b4..26530b41e38 100644 --- a/model/account/type.go +++ b/model/account/type.go @@ -88,7 +88,6 @@ type AccountType struct { Secret interface{} `json:"secret,omitempty"` // For sending notifications via Firebase Cloud Messaging - AndroidAPIKey string `json:"android_api_key"` FCMCredentials json.RawMessage `json:"fcm_credentials"` } diff --git a/pkg/config/config/config.go b/pkg/config/config/config.go index 3f7fd8c5ab8..d5abdf966dd 100644 --- a/pkg/config/config/config.go +++ b/pkg/config/config/config.go @@ -236,9 +236,6 @@ type Notifications struct { FCMCredentialsFile string - AndroidAPIKey string - FCMServer string - IOSCertificateKeyPath string IOSCertificatePassword string IOSKeyID string @@ -859,9 +856,6 @@ func UseViper(v *viper.Viper) error { FCMCredentialsFile: v.GetString("notifications.fcm_credentials_file"), - FCMServer: v.GetString("notifications.fcm_server"), - AndroidAPIKey: v.GetString("notifications.android_api_key"), - IOSCertificateKeyPath: v.GetString("notifications.ios_certificate_key_path"), IOSCertificatePassword: v.GetString("notifications.ios_certificate_password"), IOSKeyID: v.GetString("notifications.ios_key_id"), diff --git a/pkg/config/config/config_test.go b/pkg/config/config/config_test.go index a3265bb1b8e..de8c219a796 100644 --- a/pkg/config/config/config_test.go +++ b/pkg/config/config/config_test.go @@ -69,8 +69,6 @@ func TestConfigUnmarshal(t *testing.T) { // Notifications assert.EqualValues(t, Notifications{ Development: true, - FCMServer: "http://some-server", - AndroidAPIKey: "some-api-key", IOSCertificateKeyPath: "cert-key-path", IOSCertificatePassword: "cert-password", IOSKeyID: "key-id", diff --git a/pkg/config/config/testdata/full_config.yaml b/pkg/config/config/testdata/full_config.yaml index 866dfa682c7..d827a587f0c 100644 --- a/pkg/config/config/testdata/full_config.yaml +++ b/pkg/config/config/testdata/full_config.yaml @@ -133,8 +133,6 @@ flagship: notifications: development: true - fcm_server: http://some-server - android_api_key: some-api-key ios_certificate_key_path: cert-key-path ios_certificate_password: cert-password ios_key_id: key-id diff --git a/worker/push/push.go b/worker/push/push.go index c985f8b230e..f865452d0a6 100644 --- a/worker/push/push.go +++ b/worker/push/push.go @@ -28,8 +28,6 @@ import ( "github.com/cozy/cozy-stack/pkg/mail" "google.golang.org/api/option" - fcm "github.com/appleboy/go-fcm" - apns "github.com/sideshow/apns2" apns_cert "github.com/sideshow/apns2/certificate" apns_payload "github.com/sideshow/apns2/payload" @@ -37,10 +35,9 @@ import ( ) var ( - fcmClient *messaging.Client - legacyFCMClient *fcm.Client - iosClient *apns.Client - huaweiClient *huawei.Client + fcmClient *messaging.Client + iosClient *apns.Client + huaweiClient *huawei.Client ) func init() { @@ -75,19 +72,6 @@ func Init() (err error) { Infof("Initialized FCM client with credentials file") } - if conf.AndroidAPIKey != "" { - if conf.FCMServer != "" { - legacyFCMClient, err = fcm.NewClient(conf.AndroidAPIKey, fcm.WithEndpoint(conf.FCMServer)) - } else { - legacyFCMClient, err = fcm.NewClient(conf.AndroidAPIKey) - } - logger.WithNamespace("push").Infof("Initialized FCM client with Android API Key") - if err != nil { - logger.WithNamespace("push").Warnf("%s", err) - return - } - } - if conf.IOSCertificateKeyPath != "" { var authKey *ecdsa.PrivateKey var certificateKey tls.Certificate @@ -241,7 +225,8 @@ func pushToFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessa client := getFirebaseClient(slug, ctx.Instance.ContextName) if client == nil { - return pushToLegacyFirebase(ctx, c, msg) + ctx.Logger().Warn("Could not send android notification: not configured") + return nil } var priority string @@ -289,67 +274,6 @@ func pushToFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessa return nil } -func pushToLegacyFirebase(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage) error { - slug := msg.Slug() - if c.Flagship { - slug = "" - } - - client := getLegacyFirebaseClient(slug, ctx.Instance.ContextName) - - if client == nil { - ctx.Logger().Warn("Could not send android notification: not configured") - return nil - } - - var priority string - if msg.Priority == "high" { - priority = "high" - } - - var hashedSource []byte - if msg.Collapsible { - hashedSource = hashSource(msg.Source) - } else { - hashedSource = hashSource(msg.Source + msg.NotificationID) - } - - notification := &fcm.Message{ - To: c.NotificationDeviceToken, - Priority: priority, - ContentAvailable: true, - Notification: &fcm.Notification{ - Sound: msg.Sound, - Title: msg.Title, - Body: msg.Message, - }, - Data: prepareLegacyAndroidData(msg, hashedSource), - } - - if msg.Collapsible { - notification.CollapseKey = hex.EncodeToString(hashedSource) - } - - res, err := client.Send(notification) - if err != nil { - ctx.Logger().Warnf("Error during fcm send: %s", err) - return err - } - if res.Failure == 0 { - return nil - } - - for _, result := range res.Results { - if result.Unregistered() { - _ = c.Delete(ctx.Instance) - } - if err = result.Error; err != nil { - return err - } - } - return nil -} - func prepareAndroidData(msg *center.PushMessage, hashedSource []byte) map[string]string { // notID should be an integer, we take the first 32bits of the hashed source // value. @@ -371,27 +295,6 @@ func prepareAndroidData(msg *center.PushMessage, hashedSource []byte) map[string return data } -func prepareLegacyAndroidData(msg *center.PushMessage, hashedSource []byte) map[string]interface{} { - // notID should be an integer, we take the first 32bits of the hashed source - // value. - notID := int32(binary.BigEndian.Uint32(hashedSource[:4])) - if notID < 0 { - notID = -notID - } - - data := map[string]interface{}{ - // Fields required by phonegap-plugin-push - // see: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour - "notId": notID, - "title": msg.Title, - "body": msg.Message, - } - for k, v := range msg.Data { - data[k] = v - } - return data -} - func getFirebaseClient(slug, contextName string) *messaging.Client { if slug == "" { return fcmClient @@ -413,21 +316,6 @@ func getFirebaseClient(slug, contextName string) *messaging.Client { return fcmClient } -func getLegacyFirebaseClient(slug, contextName string) *fcm.Client { - if slug == "" { - return legacyFCMClient - } - typ, err := account.TypeInfo(slug, contextName) - if err == nil && typ.AndroidAPIKey != "" { - client, err := fcm.NewClient(typ.AndroidAPIKey) - if err != nil { - return nil - } - return client - } - return legacyFCMClient -} - func pushToAPNS(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage) error { if iosClient == nil { ctx.Logger().Warn("Could not send iOS notification: not configured") @@ -482,7 +370,7 @@ func pushToHuawei(ctx *job.TaskContext, c *oauth.Client, msg *center.PushMessage } else { hashedSource = hashSource(msg.Source + msg.NotificationID) } - data := prepareLegacyAndroidData(msg, hashedSource) + data := prepareHuaweiData(msg, hashedSource) notification := huawei.NewNotification(msg.Title, msg.Message, c.NotificationDeviceToken, data) ctx.Logger().Infof("Huawei Push Kit send: %#v", notification) @@ -502,6 +390,27 @@ func hashSource(source string) []byte { return h.Sum(nil) } +func prepareHuaweiData(msg *center.PushMessage, hashedSource []byte) map[string]interface{} { + // notID should be an integer, we take the first 32bits of the hashed source + // value. + notID := int32(binary.BigEndian.Uint32(hashedSource[:4])) + if notID < 0 { + notID = -notID + } + + data := map[string]interface{}{ + // Fields required by phonegap-plugin-push + // see: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour + "notId": notID, + "title": msg.Title, + "body": msg.Message, + } + for k, v := range msg.Data { + data[k] = v + } + return data +} + func sendFallbackMail(inst *instance.Instance, email *mail.Options) { if inst == nil || email == nil { return diff --git a/worker/push/push_test.go b/worker/push/push_test.go deleted file mode 100644 index be84d1b8b7d..00000000000 --- a/worker/push/push_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package push - -import ( - "testing" - - "github.com/cozy/cozy-stack/model/account" - "github.com/cozy/cozy-stack/pkg/config/config" - "github.com/cozy/cozy-stack/pkg/couchdb" - "github.com/cozy/cozy-stack/pkg/prefixer" - "github.com/cozy/cozy-stack/tests/testutils" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestPush(t *testing.T) { - if testing.Short() { - t.Skip("a couchdb is required for this test: test skipped due to the use of --short flag") - } - - config.UseTestFile(t) - testutils.NeedCouchdb(t) - - t.Run("DeprecateLegacyFCM", func(t *testing.T) { - testutils.TODO(t, "2024-07-01", "Remove the deprecated calls to the legacy FCM API") - }) - - t.Run("get firebase client", func(t *testing.T) { - contextName := "foo" - slug := "bar" - - // Ensure that the global legacyFCMClient is nil for this test, and restore its - // old value after the test - oldFcmClient := legacyFCMClient - legacyFCMClient = nil - defer func() { - legacyFCMClient = oldFcmClient - }() - - // Create an account type for the test - typ := account.AccountType{ - DocID: contextName + "/" + slug, - Slug: slug, - AndroidAPIKey: "th3_f1r3b4s3_k3y", - } - err := couchdb.CreateNamedDoc(prefixer.SecretsPrefixer, &typ) - require.NoError(t, err) - - defer func() { - _ = couchdb.DeleteDoc(prefixer.SecretsPrefixer, &typ) - }() - - client := getLegacyFirebaseClient(slug, contextName) - assert.NotNil(t, client) - }) -}