Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Change NotifyUsers to allow only data JSON element. Part of… #1347

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Gopkg.lock

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

5 changes: 0 additions & 5 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@
name = "github.com/golang/protobuf"
version = "1.1.0"

[[constraint]]
name = "github.com/NaySoftware/go-fcm"
revision = "024ca6a2c5444c93980f558f91c35a2defebd362"
source = "github.com/status-im/go-fcm"

# * * * * * `go-ethereum` dependencies * * * * *
# Pinned down SHAs from `go-ethereum/vendor/vendor.json`
# When upgrading upstream, upgrade these values too.
Expand Down
9 changes: 4 additions & 5 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"sync"

fcmlib "github.com/NaySoftware/go-fcm"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -463,12 +464,10 @@ func (b *StatusBackend) SelectAccount(address, password string) error {
}

// NotifyUsers sends push notifications to users.
func (b *StatusBackend) NotifyUsers(dataPayloadJSON string, tokens ...string) error {
log.Debug("sending push notification")

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

return err
Expand Down
14 changes: 11 additions & 3 deletions lib/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -422,7 +423,7 @@ func makeJSONResponse(err error) *C.char {

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

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

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

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

33 changes: 16 additions & 17 deletions notifications/push/fcm/notification.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package fcm

import (
"encoding/json"
"fmt"

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

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

// NotificationConstructor returns constructor of configured instance Notifier interface.
Expand All @@ -26,30 +25,30 @@ func NewNotification(key string) NotificationConstructor {
client := fcm.NewFcmClient(key).
SetDelayWhileIdle(true).
SetContentAvailable(true).
SetPriority(fcm.Priority_HIGH). // Message needs to be marked as high-priority so that background task in an Android's recipient device can be invoked (https://github.com/invertase/react-native-firebase/blob/d13f0af53f1c8f20db8bc8d4b6f8c6d210e108b9/android/src/main/java/io/invertase/firebase/messaging/RNFirebaseMessagingService.java#L56)
SetTimeToLive(fcm.MAX_TTL)

return &Notification{client}
}
}

// 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
// Send send to the tokens list.
func (n *Notification) Send(body string, payload fcm.NotificationPayload, tokens ...string) error {
data := map[string]string{
"msg": body,
}

n.client.NewFcmRegIdsMsg(tokens, dataPayload)
resp, err := n.client.Send()
if err != nil {
return err
if payload.Title == "" {
payload.Title = "Status"
}

if resp != nil && !resp.Ok {
return fmt.Errorf("FCM error sending message, code=%d err=%s", resp.StatusCode, resp.Err)
if payload.Body == "" {
payload.Body = "You have a new message"
}

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

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

return err
}
57 changes: 24 additions & 33 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,52 +29,43 @@ func (s *NotifierTestSuite) TearDownTest() {
s.fcmClientMockCtrl.Finish()
}

func (s *NotifierTestSuite) TestSendSuccess() {
func (s *NotifierTestSuite) TestNotifySuccess() {
fcmPayload := getPayload()
ids := []string{"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)
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)
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
fcmClient := Notification{s.fcmClientMock}

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

s.NoError(err)
}

func (s *NotifierTestSuite) TestSendError() {
func (s *NotifierTestSuite) TestNotifyError() {
expectedError := errors.New("error")
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)
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)
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
fcmClient := Notification{s.fcmClientMock}

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

s.Equal(expectedError, err)
}

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)
func getPayload() fcm.NotificationPayload {
return fcm.NotificationPayload{Title: "Status - new message", Body: "sum"}
}
16 changes: 0 additions & 16 deletions vendor/github.com/NaySoftware/go-fcm/fcm.go

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