diff --git a/geth/api/api.go b/geth/api/api.go index da6b41b07..13d4969d7 100644 --- a/geth/api/api.go +++ b/geth/api/api.go @@ -6,6 +6,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" gethcommon "github.com/ethereum/go-ethereum/common" "github.com/status-im/status-go/geth/common" + "github.com/status-im/status-go/geth/notification/message" "github.com/status-im/status-go/geth/params" ) @@ -193,10 +194,21 @@ func (api *StatusAPI) JailBaseJS(js string) { api.b.jailManager.BaseJS(js) } -// Notify and send message +// Notify and send message. func (api *StatusAPI) Notify(token string) string { - api.b.notification.Notify(token) - api.b.notification.Send() + // TODO(oskarth): Experiment with this + msg := &message.Message{ + Body: map[string]string{ + "msg": "Hello World1", + "sum": "Happy Day", + }, + Payload: &message.Payload{ + Title: "Status - new message", + Body: "ping", + }, + } + + api.b.notification.Notify(token, msg) return token } diff --git a/geth/api/backend.go b/geth/api/backend.go index 02764474d..3dca45068 100644 --- a/geth/api/backend.go +++ b/geth/api/backend.go @@ -14,6 +14,12 @@ import ( "github.com/status-im/status-go/geth/params" "github.com/status-im/status-go/geth/signal" "github.com/status-im/status-go/geth/txqueue" + "github.com/NaySoftware/go-fcm" +) + +const ( + //todo(jeka): should be removed + fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx" ) // StatusBackend implements Status.im service @@ -35,7 +41,7 @@ func NewStatusBackend() *StatusBackend { accountManager := account.NewManager(nodeManager) txQueueManager := txqueue.NewManager(nodeManager, accountManager) jailManager := jail.New(nodeManager) - notificationManager := notification.New(notification.NewFCMClient()) + notificationManager := notification.New(notification.NewFCMProvider(fcm.NewFcmClient(fcmServerKey))) return &StatusBackend{ nodeManager: nodeManager, diff --git a/geth/common/notification.go b/geth/common/notification.go index b1a9c7088..fec73f77d 100644 --- a/geth/common/notification.go +++ b/geth/common/notification.go @@ -1,15 +1,24 @@ package common -import "github.com/NaySoftware/go-fcm" +import ( + "github.com/NaySoftware/go-fcm" + "github.com/status-im/status-go/geth/notification/message" +) -// Notification manages Push Notifications and send messages +// Notification manages Push Notifications and send messages. type Notification interface { - Notify(token string) string + Notify(token string, msg *message.Message) (string, error) +} + +// MessagingProvider manages send/notification messaging clients. +type MessagingProvider interface { + SetMessage(ids []string, body interface{}) + SetPayload(payload *message.Payload) Send() error } -// Messaging manages send/notification messaging clients -type Messaging interface { +// FirebaseClient is a copy of "go-fcm" client methods. +type FirebaseClient interface { NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient Send() (*fcm.FcmResponseStatus, error) SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient diff --git a/geth/notification/fcm_client.go b/geth/notification/fcm_client.go deleted file mode 100644 index e25d07c1c..000000000 --- a/geth/notification/fcm_client.go +++ /dev/null @@ -1,21 +0,0 @@ -package notification - -import "github.com/NaySoftware/go-fcm" - -const ( - //todo(jeka): should be removed - fcmServerKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx" -) - -// NewFCMClient Firebase Cloud Messaging client constructor -func NewFCMClient() *fcm.FcmClient { - return fcm.NewFcmClient(fcmServerKey).SetNotificationPayload(getNotificationPayload()) -} - -// only for feature testing -func getNotificationPayload() *fcm.NotificationPayload { - return &fcm.NotificationPayload{ - Title: "Status - new message", - Body: "ping", - } -} diff --git a/geth/notification/fcm_provider.go b/geth/notification/fcm_provider.go new file mode 100644 index 000000000..bd9f63b51 --- /dev/null +++ b/geth/notification/fcm_provider.go @@ -0,0 +1,46 @@ +package notification + +import ( + "github.com/NaySoftware/go-fcm" + "github.com/status-im/status-go/geth/notification/message" + "github.com/status-im/status-go/geth/common" +) + +// FCMProvider represents messaging provider for notifications. +type FCMProvider struct { + common.FirebaseClient +} + +// NewFCMProvider Firebase Cloud Messaging client constructor. +func NewFCMProvider(fcmClient common.FirebaseClient) *FCMProvider { + return &FCMProvider{fcmClient} +} + +// SetMessage to send for given ids. +func (p *FCMProvider) SetMessage(ids []string, body interface{}) { + p.NewFcmRegIdsMsg(ids, body) +} + +// SetPayload sets payload message information. +func (p *FCMProvider) SetPayload(payload *message.Payload) { + fcmPayload := p.toFCMPayload(payload) + p.SetNotificationPayload(fcmPayload) +} + +// Send message. +func (p *FCMProvider) Send() error { + _, err := p.FirebaseClient.Send() + return err +} + +func (p *FCMProvider) toFCMPayload(payload *message.Payload) *fcm.NotificationPayload { + return &fcm.NotificationPayload{ + Title: payload.Title, + Body: payload.Body, + Icon: payload.Icon, + Sound: payload.Sound, + Badge: payload.Badge, + Tag: payload.Tag, + Color: payload.Color, + } +} diff --git a/geth/notification/fcm_client_test.go b/geth/notification/fcm_provider_test.go similarity index 100% rename from geth/notification/fcm_client_test.go rename to geth/notification/fcm_provider_test.go diff --git a/geth/notification/message/message.go b/geth/notification/message/message.go new file mode 100644 index 000000000..9e5c841e9 --- /dev/null +++ b/geth/notification/message/message.go @@ -0,0 +1,7 @@ +package message + +// Message with data and payload +type Message struct { + Body interface{} + Payload *Payload +} diff --git a/geth/notification/message/payload.go b/geth/notification/message/payload.go new file mode 100644 index 000000000..c856e5c5e --- /dev/null +++ b/geth/notification/message/payload.go @@ -0,0 +1,12 @@ +package message + +// Payload data of message. +type Payload struct { + Title string + Body string + Icon string + Sound string + Badge string + Tag string + Color string +} diff --git a/geth/notification/notification.go b/geth/notification/notification.go index e24d7f0d6..6b77220be 100644 --- a/geth/notification/notification.go +++ b/geth/notification/notification.go @@ -3,43 +3,32 @@ package notification import ( "github.com/status-im/status-go/geth/common" "github.com/status-im/status-go/geth/log" + "github.com/status-im/status-go/geth/notification/message" ) -// Manager of push notifications +// Manager of push notifications. type Manager struct { - messaging common.Messaging + messaging common.MessagingProvider } -// New notifications manager -func New(messaging common.Messaging) *Manager { +// New notifications manager. +func New(messaging common.MessagingProvider) *Manager { return &Manager{ messaging, } } -// Notify registers notification -func (n *Manager) Notify(token string) string { +// Notify makes send message and notification. +func (n *Manager) Notify(token string, msg *message.Message) (string, error) { log.Debug("Notify", "token", token) - n.messaging.NewFcmRegIdsMsg([]string{token}, n.getMessage) + n.messaging.SetMessage([]string{token}, msg.Body) + n.messaging.SetPayload(msg.Payload) - return token -} - -// Send prepared message -func (n *Manager) Send() error { - _, err := n.messaging.Send() + err := n.messaging.Send() if err != nil { log.Error("Notify failed:", err) } - return err -} - -func (n *Manager) getMessage() interface{} { - // TODO(oskarth): Experiment with this - return map[string]string{ - "msg": "Hello World1", - "sum": "Happy Day", - } + return token, err }