Update notify interface and mark old one as deprecated
This commit is contained in:
parent
a3bca52f51
commit
0e72e3d6b4
|
@ -6,12 +6,12 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
|
||||
"github.com/NaySoftware/go-fcm"
|
||||
"github.com/status-im/status-go/geth/common"
|
||||
"github.com/status-im/status-go/geth/log"
|
||||
"github.com/status-im/status-go/geth/params"
|
||||
"github.com/status-im/status-go/helpers/profiling"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
)
|
||||
|
||||
//GenerateConfig for status node
|
||||
|
@ -374,25 +374,56 @@ func makeJSONResponse(err error) *C.char {
|
|||
}
|
||||
|
||||
// Notify sends push notification by given token
|
||||
// @deprecated
|
||||
//export Notify
|
||||
func Notify(token *C.char) *C.char {
|
||||
err := statusAPI.Notify(C.GoString(token))
|
||||
res := statusAPI.Notify(C.GoString(token))
|
||||
return C.CString(res)
|
||||
}
|
||||
|
||||
// NotifyUsers sends push notifications by given tokens.
|
||||
//export NotifyUsers
|
||||
func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char) {
|
||||
var (
|
||||
err error
|
||||
outBytes []byte
|
||||
)
|
||||
errString := ""
|
||||
|
||||
defer func() {
|
||||
out := common.NotifyResult{
|
||||
Status: err == nil,
|
||||
Error: errString,
|
||||
}
|
||||
|
||||
outBytes, err = json.Marshal(out)
|
||||
if err != nil {
|
||||
log.Error("failed to marshal Notify output", "error", err.Error())
|
||||
outCBytes = makeJSONResponse(err)
|
||||
return
|
||||
}
|
||||
|
||||
outCBytes = C.CString(string(outBytes))
|
||||
}()
|
||||
|
||||
tokens, err := common.ParseJSONArray(C.GoString(tokensArray))
|
||||
if err != nil {
|
||||
errString = err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
out := common.NotifyResult{
|
||||
Status: err == nil,
|
||||
Error: errString,
|
||||
}
|
||||
|
||||
outBytes, err := json.Marshal(out)
|
||||
var payload fcm.NotificationPayload
|
||||
err = json.Unmarshal([]byte(C.GoString(payloadJSON)), &payload)
|
||||
if err != nil {
|
||||
log.Error("failed to marshal Notify output", "error", err.Error())
|
||||
return makeJSONResponse(err)
|
||||
errString = err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
return C.CString(string(outBytes))
|
||||
err = statusAPI.NotifyUsers(C.GoString(message), payload, tokens...)
|
||||
if err != nil {
|
||||
errString = err.Error()
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ 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"
|
||||
|
@ -194,17 +195,27 @@ func (api *StatusAPI) JailBaseJS(js string) {
|
|||
api.b.jailManager.BaseJS(js)
|
||||
}
|
||||
|
||||
// Notify and send message.
|
||||
func (api *StatusAPI) Notify(token string) error {
|
||||
// Notify sends a push notification to the device with the given token.
|
||||
// @deprecated
|
||||
func (api *StatusAPI) Notify(token string) string {
|
||||
log.Debug("Notify", "token", token)
|
||||
message := "Hello World1"
|
||||
|
||||
// TODO(oskarth): Experiment with this
|
||||
msg := map[string]string{
|
||||
"msg": "Hello World1",
|
||||
"sum": "Happy Day",
|
||||
tokens := []string{token}
|
||||
|
||||
err := api.b.newNotification().Send(message, fcm.NotificationPayload{}, tokens...)
|
||||
if err != nil {
|
||||
log.Error("Notify failed:", err)
|
||||
}
|
||||
|
||||
err := api.b.newNotification().Send(msg, token)
|
||||
return token
|
||||
}
|
||||
|
||||
// NotifyUsers send notifications to users.
|
||||
func (api *StatusAPI) NotifyUsers(message string, payload fcm.NotificationPayload, tokens ...string) error {
|
||||
log.Debug("Notify", "tokens", tokens)
|
||||
|
||||
err := api.b.newNotification().Send(message, payload, tokens...)
|
||||
if err != nil {
|
||||
log.Error("Notify failed:", err)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package common
|
||||
|
||||
import "github.com/NaySoftware/go-fcm"
|
||||
|
||||
// Notifier manages Push Notifications.
|
||||
type Notifier interface {
|
||||
Send(body interface{}, tokens ...string) error
|
||||
Send(body string, payload fcm.NotificationPayload, tokens ...string) error
|
||||
}
|
||||
|
||||
// NotificationConstructor returns constructor of configured instance Notifier interface.
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
go_fcm "github.com/NaySoftware/go-fcm"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
@ -33,8 +34,8 @@ func (m *MockNotifier) EXPECT() *MockNotifierMockRecorder {
|
|||
}
|
||||
|
||||
// Send mocks base method
|
||||
func (m *MockNotifier) Send(body interface{}, tokens ...string) error {
|
||||
varargs := []interface{}{body}
|
||||
func (m *MockNotifier) Send(body string, payload go_fcm.NotificationPayload, tokens ...string) error {
|
||||
varargs := []interface{}{body, payload}
|
||||
for _, a := range tokens {
|
||||
varargs = append(varargs, a)
|
||||
}
|
||||
|
@ -44,7 +45,7 @@ func (m *MockNotifier) Send(body interface{}, tokens ...string) error {
|
|||
}
|
||||
|
||||
// Send indicates an expected call of Send
|
||||
func (mr *MockNotifierMockRecorder) Send(body interface{}, tokens ...interface{}) *gomock.Call {
|
||||
varargs := append([]interface{}{body}, tokens...)
|
||||
func (mr *MockNotifierMockRecorder) Send(body, payload interface{}, tokens ...interface{}) *gomock.Call {
|
||||
varargs := append([]interface{}{body, payload}, tokens...)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockNotifier)(nil).Send), varargs...)
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package fcm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/NaySoftware/go-fcm"
|
||||
"github.com/status-im/status-go/geth/common"
|
||||
"github.com/status-im/status-go/geth/notification"
|
||||
)
|
||||
|
||||
// Notification represents messaging provider for notifications.
|
||||
|
@ -14,42 +15,33 @@ type Notification struct {
|
|||
// NewNotification Firebase Cloud Messaging client constructor.
|
||||
func NewNotification(key string) common.NotificationConstructor {
|
||||
return func() common.Notifier {
|
||||
return &Notification{fcm.NewFcmClient(key)}
|
||||
client := fcm.NewFcmClient(key).
|
||||
SetDelayWhileIdle(true).
|
||||
SetContentAvailable(true).
|
||||
SetTimeToLive(fcm.MAX_TTL)
|
||||
|
||||
return &Notification{client}
|
||||
}
|
||||
}
|
||||
|
||||
// Send send to the tokens list.
|
||||
func (n *Notification) Send(body interface{}, tokens ...string) error {
|
||||
n.setPayload(¬ification.Payload{
|
||||
Title: "Status - new message",
|
||||
Body: "ping",
|
||||
})
|
||||
func (n *Notification) Send(body string, payload fcm.NotificationPayload, tokens ...string) error {
|
||||
data := map[string]string{
|
||||
"msg": body,
|
||||
}
|
||||
|
||||
n.setMessage(body, tokens...)
|
||||
if payload.Title == "" {
|
||||
payload.Title = "Status - new message"
|
||||
}
|
||||
if payload.Body == "" {
|
||||
payload.Body = "ping"
|
||||
}
|
||||
|
||||
fmt.Println(payload.Title, payload.Body)
|
||||
|
||||
n.client.NewFcmRegIdsMsg(tokens, data)
|
||||
n.client.SetNotificationPayload(&payload)
|
||||
_, err := n.client.Send()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// SetMessage to send for given the tokens list.
|
||||
func (n *Notification) setMessage(body interface{}, tokens ...string) {
|
||||
n.client.NewFcmRegIdsMsg(tokens, body)
|
||||
}
|
||||
|
||||
// SetPayload sets payload message information.
|
||||
func (n *Notification) setPayload(payload *notification.Payload) {
|
||||
fcmPayload := n.toFCMPayload(payload)
|
||||
n.client.SetNotificationPayload(fcmPayload)
|
||||
}
|
||||
|
||||
func (n *Notification) toFCMPayload(payload *notification.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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,14 +32,17 @@ func (s *NotifierTestSuite) TearDownTest() {
|
|||
func (s *NotifierTestSuite) TestNotifySuccess() {
|
||||
fcmPayload := getPayload()
|
||||
ids := []string{"1"}
|
||||
body := interface{}("body")
|
||||
payload := fcmPayload
|
||||
msg := make(map[string]string)
|
||||
body := "body"
|
||||
msg["msg"] = body
|
||||
|
||||
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
|
||||
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
|
||||
s.fcmClientMock.EXPECT().SetNotificationPayload(&fcmPayload).Times(1)
|
||||
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, msg).Times(1)
|
||||
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
|
||||
fcmClient := Notification{s.fcmClientMock}
|
||||
|
||||
err := fcmClient.Send(body, ids...)
|
||||
err := fcmClient.Send(body, payload, ids...)
|
||||
|
||||
s.NoError(err)
|
||||
}
|
||||
|
@ -48,18 +51,21 @@ func (s *NotifierTestSuite) TestNotifyError() {
|
|||
expectedError := errors.New("error")
|
||||
fcmPayload := getPayload()
|
||||
ids := []string{"1"}
|
||||
body := interface{}("body")
|
||||
payload := fcmPayload
|
||||
msg := make(map[string]string)
|
||||
body := "body"
|
||||
msg["msg"] = body
|
||||
|
||||
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
|
||||
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
|
||||
s.fcmClientMock.EXPECT().SetNotificationPayload(&fcmPayload).Times(1)
|
||||
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, msg).Times(1)
|
||||
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
|
||||
fcmClient := Notification{s.fcmClientMock}
|
||||
|
||||
err := fcmClient.Send(body, ids...)
|
||||
err := fcmClient.Send(body, payload, ids...)
|
||||
|
||||
s.Equal(expectedError, err)
|
||||
}
|
||||
|
||||
func getPayload() *fcm.NotificationPayload {
|
||||
return &fcm.NotificationPayload{Title: "Status - new message", Body: "ping"}
|
||||
func getPayload() fcm.NotificationPayload {
|
||||
return fcm.NotificationPayload{Title: "Status - new message", Body: "sum"}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue