Skip to content

Commit 95edcba

Browse files
author
Pedro Pombeiro
committed
Add SendMessageToUsers method. Part of status-im/status-mobile#7268
1 parent 0b72aea commit 95edcba

File tree

8 files changed

+170
-4
lines changed

8 files changed

+170
-4
lines changed

Gopkg.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
name = "github.com/golang/protobuf"
3636
version = "1.1.0"
3737

38+
[[constraint]]
39+
name = "github.com/NaySoftware/go-fcm"
40+
revision = "024ca6a2c5444c93980f558f91c35a2defebd362"
41+
source = "github.com/status-im/go-fcm"
42+
3843
# * * * * * `go-ethereum` dependencies * * * * *
3944
# Pinned down SHAs from `go-ethereum/vendor/vendor.json`
4045
# When upgrading upstream, upgrade these values too.

api/backend.go

+12
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,18 @@ func (b *StatusBackend) NotifyUsers(message string, payload fcmlib.NotificationP
473473
return err
474474
}
475475

476+
// SendMessageToUsers sends a message to users, which can be used to display a push notification.
477+
func (b *StatusBackend) SendMessageToUsers(dataPayloadJSON string, tokens ...string) error {
478+
log.Debug("sending message")
479+
480+
err := b.newNotification().SendMessage(dataPayloadJSON, tokens...)
481+
if err != nil {
482+
b.log.Error("SendMessage failed", "dataPayloadJSON", dataPayloadJSON, "error", err)
483+
}
484+
485+
return err
486+
}
487+
476488
func appendIf(condition bool, services []gethnode.ServiceConstructor, service gethnode.ServiceConstructor) []gethnode.ServiceConstructor {
477489
if !condition {
478490
return services

lib/library.go

+40
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,46 @@ func NotifyUsers(message, payloadJSON, tokensArray *C.char) (outCBytes *C.char)
468468
return
469469
}
470470

471+
// SendMessageToUsers sends messages to users represented by given tokens.
472+
//export SendMessageToUsers
473+
func SendMessageToUsers(dataPayloadJSON, tokensArray *C.char) (outCBytes *C.char) {
474+
var (
475+
err error
476+
outBytes []byte
477+
)
478+
errString := ""
479+
480+
defer func() {
481+
out := NotifyResult{
482+
Status: err == nil,
483+
Error: errString,
484+
}
485+
486+
outBytes, err = json.Marshal(out)
487+
if err != nil {
488+
logger.Error("failed to marshal SendMessageToUsers output", "error", err)
489+
outCBytes = makeJSONResponse(err)
490+
return
491+
}
492+
493+
outCBytes = C.CString(string(outBytes))
494+
}()
495+
496+
tokens, err := ParseJSONArray(C.GoString(tokensArray))
497+
if err != nil {
498+
errString = err.Error()
499+
return
500+
}
501+
502+
err = statusBackend.SendMessageToUsers(C.GoString(dataPayloadJSON), tokens...)
503+
if err != nil {
504+
errString = err.Error()
505+
return
506+
}
507+
508+
return
509+
}
510+
471511
// UpdateMailservers updates mail servers in status backend.
472512
//export UpdateMailservers
473513
func UpdateMailservers(data *C.char) *C.char {

notifications/push/fcm/client_mock.go

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

notifications/push/fcm/notification.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fcm
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/NaySoftware/go-fcm"
@@ -9,6 +10,7 @@ import (
910
// Notifier manages Push Notifications.
1011
type Notifier interface {
1112
Send(body string, payload fcm.NotificationPayload, tokens ...string) error
13+
SendMessage(dataPayloadJSON string, tokens ...string) error
1214
}
1315

1416
// NotificationConstructor returns constructor of configured instance Notifier interface.
@@ -25,6 +27,7 @@ func NewNotification(key string) NotificationConstructor {
2527
client := fcm.NewFcmClient(key).
2628
SetDelayWhileIdle(true).
2729
SetContentAvailable(true).
30+
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)
2831
SetTimeToLive(fcm.MAX_TTL)
2932

3033
return &Notification{client}
@@ -52,3 +55,24 @@ func (n *Notification) Send(body string, payload fcm.NotificationPayload, tokens
5255

5356
return err
5457
}
58+
59+
// SendMessage sends a message to the tokens list.
60+
func (n *Notification) SendMessage(dataPayloadJSON string, tokens ...string) error {
61+
var dataPayload map[string]string
62+
err := json.Unmarshal([]byte(dataPayloadJSON), &dataPayload)
63+
if err != nil {
64+
return err
65+
}
66+
67+
n.client.NewFcmRegIdsMsg(tokens, dataPayload)
68+
resp, err := n.client.Send()
69+
if err != nil {
70+
return err
71+
}
72+
73+
if resp != nil && !resp.Ok {
74+
return fmt.Errorf("FCM error sending message, code=%d err=%s", resp.StatusCode, resp.Err)
75+
}
76+
77+
return nil
78+
}

notifications/push/fcm/notification_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fcm
22

33
import (
4+
"encoding/json"
45
"errors"
56
"testing"
67

@@ -69,3 +70,53 @@ func (s *NotifierTestSuite) TestNotifyError() {
6970
func getPayload() fcm.NotificationPayload {
7071
return fcm.NotificationPayload{Title: "Status - new message", Body: "sum"}
7172
}
73+
74+
func (s *NotifierTestSuite) TestSendSuccess() {
75+
ids := []string{"1"}
76+
dataPayload := make(map[string]string)
77+
dataPayload["from"] = "a"
78+
dataPayload["to"] = "b"
79+
dataPayloadByteArray, err := json.Marshal(dataPayload)
80+
s.Require().NoError(err)
81+
dataPayloadJSON := string(dataPayloadByteArray)
82+
83+
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
84+
s.fcmClientMock.EXPECT().SendMessage().Return(nil, nil).Times(1)
85+
fcmClient := Notification{s.fcmClientMock}
86+
87+
err = fcmClient.SendMessage(dataPayloadJSON, ids...)
88+
89+
s.NoError(err)
90+
}
91+
92+
func (s *NotifierTestSuite) TestSendError() {
93+
expectedError := errors.New("error")
94+
ids := []string{"2"}
95+
dataPayload := make(map[string]string)
96+
dataPayload["from"] = "c"
97+
dataPayload["to"] = "d"
98+
dataPayloadByteArray, err := json.Marshal(dataPayload)
99+
s.Require().NoError(err)
100+
dataPayloadJSON := string(dataPayloadByteArray)
101+
102+
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
103+
s.fcmClientMock.EXPECT().SendMessage().Return(nil, expectedError).Times(1)
104+
fcmClient := Notification{s.fcmClientMock}
105+
106+
err = fcmClient.SendMessage(dataPayloadJSON, ids...)
107+
108+
s.Equal(expectedError, err)
109+
}
110+
111+
func (s *NotifierTestSuite) TestSendWithInvalidJSON() {
112+
ids := []string{"3"}
113+
dataPayloadJSON := "{a=b}"
114+
115+
fcmClient := Notification{s.fcmClientMock}
116+
117+
err := fcmClient.SendMessage(dataPayloadJSON, ids...)
118+
s.Require().Error(err)
119+
120+
_, ok := err.(*json.SyntaxError)
121+
s.True(ok)
122+
}

vendor/github.com/NaySoftware/go-fcm/fcm.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)