Create message provider interface

This commit is contained in:
Eugene 2017-10-10 18:30:56 +03:00 committed by Evgeny Danienko
parent 9c1aff3655
commit 7195fe3f92
No known key found for this signature in database
GPG Key ID: BC8C34D8B45BECBF
9 changed files with 112 additions and 52 deletions

View File

@ -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
}

View File

@ -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,

View File

@ -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

View File

@ -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",
}
}

View File

@ -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,
}
}

View File

@ -0,0 +1,7 @@
package message
// Message with data and payload
type Message struct {
Body interface{}
Payload *Payload
}

View File

@ -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
}

View File

@ -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
}