Create message provider interface
This commit is contained in:
parent
9c1aff3655
commit
7195fe3f92
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/accounts/keystore"
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
||||||
gethcommon "github.com/ethereum/go-ethereum/common"
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/status-im/status-go/geth/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"
|
"github.com/status-im/status-go/geth/params"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -193,10 +194,21 @@ func (api *StatusAPI) JailBaseJS(js string) {
|
||||||
api.b.jailManager.BaseJS(js)
|
api.b.jailManager.BaseJS(js)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify and send message
|
// Notify and send message.
|
||||||
func (api *StatusAPI) Notify(token string) string {
|
func (api *StatusAPI) Notify(token string) string {
|
||||||
api.b.notification.Notify(token)
|
// TODO(oskarth): Experiment with this
|
||||||
api.b.notification.Send()
|
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
|
return token
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,12 @@ import (
|
||||||
"github.com/status-im/status-go/geth/params"
|
"github.com/status-im/status-go/geth/params"
|
||||||
"github.com/status-im/status-go/geth/signal"
|
"github.com/status-im/status-go/geth/signal"
|
||||||
"github.com/status-im/status-go/geth/txqueue"
|
"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
|
// StatusBackend implements Status.im service
|
||||||
|
@ -35,7 +41,7 @@ func NewStatusBackend() *StatusBackend {
|
||||||
accountManager := account.NewManager(nodeManager)
|
accountManager := account.NewManager(nodeManager)
|
||||||
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
|
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
|
||||||
jailManager := jail.New(nodeManager)
|
jailManager := jail.New(nodeManager)
|
||||||
notificationManager := notification.New(notification.NewFCMClient())
|
notificationManager := notification.New(notification.NewFCMProvider(fcm.NewFcmClient(fcmServerKey)))
|
||||||
|
|
||||||
return &StatusBackend{
|
return &StatusBackend{
|
||||||
nodeManager: nodeManager,
|
nodeManager: nodeManager,
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
package common
|
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 {
|
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
|
Send() error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Messaging manages send/notification messaging clients
|
// FirebaseClient is a copy of "go-fcm" client methods.
|
||||||
type Messaging interface {
|
type FirebaseClient interface {
|
||||||
NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient
|
NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient
|
||||||
Send() (*fcm.FcmResponseStatus, error)
|
Send() (*fcm.FcmResponseStatus, error)
|
||||||
SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient
|
SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient
|
||||||
|
|
|
@ -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",
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package message
|
||||||
|
|
||||||
|
// Message with data and payload
|
||||||
|
type Message struct {
|
||||||
|
Body interface{}
|
||||||
|
Payload *Payload
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -3,43 +3,32 @@ package notification
|
||||||
import (
|
import (
|
||||||
"github.com/status-im/status-go/geth/common"
|
"github.com/status-im/status-go/geth/common"
|
||||||
"github.com/status-im/status-go/geth/log"
|
"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 {
|
type Manager struct {
|
||||||
messaging common.Messaging
|
messaging common.MessagingProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
// New notifications manager
|
// New notifications manager.
|
||||||
func New(messaging common.Messaging) *Manager {
|
func New(messaging common.MessagingProvider) *Manager {
|
||||||
return &Manager{
|
return &Manager{
|
||||||
messaging,
|
messaging,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify registers notification
|
// Notify makes send message and notification.
|
||||||
func (n *Manager) Notify(token string) string {
|
func (n *Manager) Notify(token string, msg *message.Message) (string, error) {
|
||||||
log.Debug("Notify", "token", token)
|
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
|
err := n.messaging.Send()
|
||||||
}
|
|
||||||
|
|
||||||
// Send prepared message
|
|
||||||
func (n *Manager) Send() error {
|
|
||||||
_, err := n.messaging.Send()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Notify failed:", err)
|
log.Error("Notify failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return token, err
|
||||||
}
|
|
||||||
|
|
||||||
func (n *Manager) getMessage() interface{} {
|
|
||||||
// TODO(oskarth): Experiment with this
|
|
||||||
return map[string]string{
|
|
||||||
"msg": "Hello World1",
|
|
||||||
"sum": "Happy Day",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue