Extract notification into separete package and interface

This commit is contained in:
Eugene 2017-10-09 19:21:56 +03:00 committed by Evgeny Danienko
parent 1c8d32c451
commit 4aaeeb6ebf
No known key found for this signature in database
GPG Key ID: BC8C34D8B45BECBF
8 changed files with 123 additions and 65 deletions

View File

@ -3,18 +3,12 @@ package api
import (
"context"
"github.com/NaySoftware/go-fcm"
"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/log"
"github.com/status-im/status-go/geth/params"
)
const (
serverKey = "AAAAxwa-r08:APA91bFtMIToDVKGAmVCm76iEXtA4dn9MPvLdYKIZqAlNpLJbd12EgdBI9DSDSXKdqvIAgLodepmRhGVaWvhxnXJzVpE6MoIRuKedDV3kfHSVBhWFqsyoLTwXY4xeufL9Sdzb581U-lx"
)
// StatusAPI provides API to access Status related functionality.
type StatusAPI struct {
b *StatusBackend
@ -199,33 +193,10 @@ func (api *StatusAPI) JailBaseJS(js string) {
api.b.jailManager.BaseJS(js)
}
// Notify sends a push notification to the device with the given token.
// TODO(oskarth): API package this stuff
// Notify and send message
func (api *StatusAPI) Notify(token string) string {
log.Debug("Notify", "token", token)
var NP fcm.NotificationPayload
NP.Title = "Status - new message"
NP.Body = "ping"
// TODO(oskarth): Experiment with this
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
ids := []string{
token,
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmRegIdsMsg(ids, data)
c.SetNotificationPayload(&NP)
_, err := c.Send()
if err != nil {
log.Error("Notify failed:", err)
}
api.b.notification.Notify(token)
api.b.notification.Send()
return token
}

View File

@ -10,6 +10,7 @@ import (
"github.com/status-im/status-go/geth/jail"
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/node"
"github.com/status-im/status-go/geth/notification"
"github.com/status-im/status-go/geth/params"
"github.com/status-im/status-go/geth/signal"
"github.com/status-im/status-go/geth/txqueue"
@ -23,7 +24,7 @@ type StatusBackend struct {
accountManager common.AccountManager
txQueueManager common.TxQueueManager
jailManager common.JailManager
// TODO(oskarth): notifer here
notification common.Notification
}
// NewStatusBackend create a new NewStatusBackend instance
@ -34,12 +35,14 @@ func NewStatusBackend() *StatusBackend {
accountManager := account.NewManager(nodeManager)
txQueueManager := txqueue.NewManager(nodeManager, accountManager)
jailManager := jail.New(nodeManager)
notificationManager := notification.New(notification.NewFCMClient())
return &StatusBackend{
nodeManager: nodeManager,
accountManager: accountManager,
jailManager: jailManager,
txQueueManager: txQueueManager,
notification: notificationManager,
}
}

View File

@ -0,0 +1,16 @@
package common
import "github.com/NaySoftware/go-fcm"
// Notification manages Push Notifications and send messages
type Notification interface {
Notify(token string) string
Send() error
}
// Messaging manages send/notification messaging clients
type Messaging interface {
NewFcmRegIdsMsg(list []string, body interface{}) *fcm.FcmClient
Send() (*fcm.FcmResponseStatus, error)
SetNotificationPayload(payload *fcm.NotificationPayload) *fcm.FcmClient
}

View File

@ -0,0 +1,21 @@
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 @@
package notification

View File

@ -0,0 +1,45 @@
package notification
import (
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log"
)
// Manager of push notifications
type Manager struct {
messaging common.Messaging
}
// New notifications manager
func New(messaging common.Messaging) *Manager {
return &Manager{
messaging,
}
}
// Notify registers notification
func (n *Manager) Notify(token string) string {
log.Debug("Notify", "token", token)
n.messaging.NewFcmRegIdsMsg([]string{token}, n.getMessage)
return token
}
// Send prepared message
func (n *Manager) Send() error {
_, 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",
}
}

View File

@ -0,0 +1 @@
package notification

View File

@ -578,6 +578,7 @@ type bintree struct {
Func func() (*asset, error)
Children map[string]*bintree
}
var _bintree = &bintree{nil, map[string]*bintree{
"config": &bintree{nil, map[string]*bintree{
"cht.json": &bintree{configChtJson, map[string]*bintree{}},
@ -660,4 +661,3 @@ func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}