Skip to content

Commit

Permalink
Change NotifyUsers to allow only data JSON element. Part of status-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Pombeiro committed Nov 21, 2018
1 parent 134cce5 commit 384a607
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 72 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.17.1-beta
0.17.1-gh6772
8 changes: 3 additions & 5 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/ethereum/go-ethereum/log"
gethnode "github.com/ethereum/go-ethereum/node"

fcmlib "github.com/NaySoftware/go-fcm"

"github.com/status-im/status-go/account"
"github.com/status-im/status-go/node"
"github.com/status-im/status-go/notifications/push/fcm"
Expand Down Expand Up @@ -445,10 +443,10 @@ func (b *StatusBackend) SelectAccount(address, password string) error {
}

// NotifyUsers sends push notifications to users.
func (b *StatusBackend) NotifyUsers(message string, payload fcmlib.NotificationPayload, tokens ...string) error {
err := b.newNotification().Send(message, payload, tokens...)
func (b *StatusBackend) NotifyUsers(dataPayloadJSON string, tokens ...string) error {
err := b.newNotification().Send(dataPayloadJSON, tokens...)
if err != nil {
b.log.Error("Notify failed", "error", err)
b.log.Error("NotifyUsers failed", "error", err)
}

return err
Expand Down
14 changes: 3 additions & 11 deletions lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"unsafe"

"github.com/NaySoftware/go-fcm"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/api"
"github.com/status-im/status-go/logutils"
Expand Down Expand Up @@ -417,7 +416,7 @@ func makeJSONResponse(err error) *C.char {

// NotifyUsers sends push notifications by given tokens.
//export NotifyUsers
func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char) {
func NotifyUsers(dataPayloadJSON, tokensArray *C.char) (outCBytes *C.char) {
var (
err error
outBytes []byte
Expand All @@ -432,7 +431,7 @@ func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char)

outBytes, err = json.Marshal(out)
if err != nil {
logger.Error("failed to marshal Notify output", "error", err)
logger.Error("failed to marshal NotifyUsers output", "error", err)
outCBytes = makeJSONResponse(err)
return
}
Expand All @@ -446,14 +445,7 @@ func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char)
return
}

var payload fcm.NotificationPayload
err = json.Unmarshal([]byte(C.GoString(payloadJSON)), &payload)
if err != nil {
errString = err.Error()
return
}

err = statusBackend.NotifyUsers(C.GoString(message), payload, tokens...)
err = statusBackend.NotifyUsers(C.GoString(dataPayloadJSON), tokens...)
if err != nil {
errString = err.Error()
return
Expand Down
1 change: 0 additions & 1 deletion notifications/push/fcm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ import (
type FirebaseClient interface {
NewFcmRegIdsMsg(tokens []string, body interface{}) *gofcm.FcmClient
Send() (*gofcm.FcmResponseStatus, error)
SetNotificationPayload(payload *gofcm.NotificationPayload) *gofcm.FcmClient
}
12 changes: 0 additions & 12 deletions notifications/push/fcm/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 11 additions & 18 deletions notifications/push/fcm/notification.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package fcm

import (
"fmt"
"encoding/json"

"github.com/NaySoftware/go-fcm"
)

// Notifier manages Push Notifications.
type Notifier interface {
Send(body string, payload fcm.NotificationPayload, tokens ...string) error
Send(dataPayloadJSON string, tokens ...string) error
}

// NotificationConstructor returns constructor of configured instance Notifier interface.
Expand All @@ -25,30 +25,23 @@ func NewNotification(key string) NotificationConstructor {
client := fcm.NewFcmClient(key).
SetDelayWhileIdle(true).
SetContentAvailable(true).
SetRestrictedPackageName("im.status.ethereum").
SetTimeToLive(fcm.MAX_TTL)

return &Notification{client}
}
}

// Send send to the tokens list.
func (n *Notification) Send(body string, payload fcm.NotificationPayload, tokens ...string) error {
data := map[string]string{
"msg": body,
// Send sends a push notification to the tokens list.
func (n *Notification) Send(dataPayloadJSON string, tokens ...string) error {
var dataPayload map[string]string
err := json.Unmarshal([]byte(dataPayloadJSON), &dataPayload)
if err != nil {
return err
}

if payload.Title == "" {
payload.Title = "Status"
}
if payload.Body == "" {
payload.Body = "You have a new message"
}

fmt.Println(payload.Title, payload.Body)

n.client.NewFcmRegIdsMsg(tokens, data)
n.client.SetNotificationPayload(&payload)
_, err := n.client.Send()
n.client.NewFcmRegIdsMsg(tokens, dataPayload)
_, err = n.client.Send()

return err
}
57 changes: 33 additions & 24 deletions notifications/push/fcm/notification_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package fcm

import (
"encoding/json"
"errors"
"testing"

"github.com/NaySoftware/go-fcm"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
)
Expand All @@ -29,43 +29,52 @@ func (s *NotifierTestSuite) TearDownTest() {
s.fcmClientMockCtrl.Finish()
}

func (s *NotifierTestSuite) TestNotifySuccess() {
fcmPayload := getPayload()
func (s *NotifierTestSuite) TestSendSuccess() {
ids := []string{"1"}
payload := fcmPayload
msg := make(map[string]string)
body := "body1"
msg["msg"] = body

s.fcmClientMock.EXPECT().SetNotificationPayload(&fcmPayload).Times(1)
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, msg).Times(1)
dataPayload := make(map[string]string)
dataPayload["from"] = "a"
dataPayload["to"] = "b"
dataPayloadByteArray, err := json.Marshal(dataPayload)
s.Require().NoError(err)
dataPayloadJSON := string(dataPayloadByteArray)

s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
fcmClient := Notification{s.fcmClientMock}

err := fcmClient.Send(body, payload, ids...)
err = fcmClient.Send(dataPayloadJSON, ids...)

s.NoError(err)
}

func (s *NotifierTestSuite) TestNotifyError() {
func (s *NotifierTestSuite) TestSendError() {
expectedError := errors.New("error")
fcmPayload := getPayload()
ids := []string{"1"}
payload := fcmPayload
msg := make(map[string]string)
body := "body2"
msg["msg"] = body

s.fcmClientMock.EXPECT().SetNotificationPayload(&fcmPayload).Times(1)
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, msg).Times(1)
ids := []string{"2"}
dataPayload := make(map[string]string)
dataPayload["from"] = "c"
dataPayload["to"] = "d"
dataPayloadByteArray, err := json.Marshal(dataPayload)
s.Require().NoError(err)
dataPayloadJSON := string(dataPayloadByteArray)

s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
fcmClient := Notification{s.fcmClientMock}

err := fcmClient.Send(body, payload, ids...)
err = fcmClient.Send(dataPayloadJSON, ids...)

s.Equal(expectedError, err)
}

func getPayload() fcm.NotificationPayload {
return fcm.NotificationPayload{Title: "Status - new message", Body: "sum"}
func (s *NotifierTestSuite) TestSendWithInvalidJSON() {
ids := []string{"3"}
dataPayloadJSON := "{a=b}"

fcmClient := Notification{s.fcmClientMock}

err := fcmClient.Send(dataPayloadJSON, ids...)
s.Require().Error(err)

_, ok := err.(*json.SyntaxError)
s.True(ok)
}

0 comments on commit 384a607

Please sign in to comment.