diff --git a/Gopkg.lock b/Gopkg.lock index c6ec200fb12..382f7360bf9 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,12 +2,12 @@ [[projects]] - digest = "1:ab480b4ff653dc950445e4ac6747c2a063f3dee7b533f68d4314267ce0fd9005" + branch = "master" + digest = "1:5805073c158189c2af2f776b590fc9b88bcc68f2b983842adb08333dbd3c9ae7" name = "github.com/NaySoftware/go-fcm" packages = ["."] pruneopts = "NUT" - revision = "024ca6a2c5444c93980f558f91c35a2defebd362" - source = "github.com/status-im/go-fcm" + revision = "2a6c4f48b49f0d5dbfe621589da4c5405157d7c9" [[projects]] branch = "master" diff --git a/Gopkg.toml b/Gopkg.toml index 254703bb294..d1b3f59a5e1 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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. diff --git a/api/backend.go b/api/backend.go index 942ce9ec728..a862ede790c 100644 --- a/api/backend.go +++ b/api/backend.go @@ -13,6 +13,8 @@ import ( gethnode "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p/enode" + 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" @@ -444,12 +446,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 diff --git a/lib/library.go b/lib/library.go index 5ce8790a391..4a6dbb17186 100644 --- a/lib/library.go +++ b/lib/library.go @@ -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" @@ -416,7 +417,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 @@ -431,7 +432,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 } @@ -445,7 +446,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 diff --git a/notifications/push/fcm/client.go b/notifications/push/fcm/client.go index 5d66b586bbf..2c9b818060d 100644 --- a/notifications/push/fcm/client.go +++ b/notifications/push/fcm/client.go @@ -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 } diff --git a/notifications/push/fcm/client_mock.go b/notifications/push/fcm/client_mock.go index d69f81ee636..71dc2c293b8 100644 --- a/notifications/push/fcm/client_mock.go +++ b/notifications/push/fcm/client_mock.go @@ -5,10 +5,9 @@ package fcm import ( - reflect "reflect" - - fcm "github.com/NaySoftware/go-fcm" + go_fcm "github.com/NaySoftware/go-fcm" gomock "github.com/golang/mock/gomock" + reflect "reflect" ) // MockFirebaseClient is a mock of FirebaseClient interface @@ -35,9 +34,9 @@ func (m *MockFirebaseClient) EXPECT() *MockFirebaseClientMockRecorder { } // NewFcmRegIdsMsg mocks base method -func (m *MockFirebaseClient) NewFcmRegIdsMsg(tokens []string, body interface{}) *fcm.FcmClient { +func (m *MockFirebaseClient) NewFcmRegIdsMsg(tokens []string, body interface{}) *go_fcm.FcmClient { ret := m.ctrl.Call(m, "NewFcmRegIdsMsg", tokens, body) - ret0, _ := ret[0].(*fcm.FcmClient) + ret0, _ := ret[0].(*go_fcm.FcmClient) return ret0 } @@ -47,9 +46,9 @@ func (mr *MockFirebaseClientMockRecorder) NewFcmRegIdsMsg(tokens, body interface } // Send mocks base method -func (m *MockFirebaseClient) Send() (*fcm.FcmResponseStatus, error) { +func (m *MockFirebaseClient) Send() (*go_fcm.FcmResponseStatus, error) { ret := m.ctrl.Call(m, "Send") - ret0, _ := ret[0].(*fcm.FcmResponseStatus) + ret0, _ := ret[0].(*go_fcm.FcmResponseStatus) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -58,3 +57,15 @@ func (m *MockFirebaseClient) Send() (*fcm.FcmResponseStatus, error) { func (mr *MockFirebaseClientMockRecorder) Send() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockFirebaseClient)(nil).Send)) } + +// SetNotificationPayload mocks base method +func (m *MockFirebaseClient) SetNotificationPayload(payload *go_fcm.NotificationPayload) *go_fcm.FcmClient { + ret := m.ctrl.Call(m, "SetNotificationPayload", payload) + ret0, _ := ret[0].(*go_fcm.FcmClient) + return ret0 +} + +// SetNotificationPayload indicates an expected call of SetNotificationPayload +func (mr *MockFirebaseClientMockRecorder) SetNotificationPayload(payload interface{}) *gomock.Call { + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNotificationPayload", reflect.TypeOf((*MockFirebaseClient)(nil).SetNotificationPayload), payload) +} diff --git a/notifications/push/fcm/notification.go b/notifications/push/fcm/notification.go index f7f56c15e26..9ba86b081cd 100644 --- a/notifications/push/fcm/notification.go +++ b/notifications/push/fcm/notification.go @@ -1,7 +1,6 @@ package fcm import ( - "encoding/json" "fmt" "github.com/NaySoftware/go-fcm" @@ -9,7 +8,7 @@ import ( // 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. @@ -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 } diff --git a/notifications/push/fcm/notification_test.go b/notifications/push/fcm/notification_test.go index 0d6356606cb..025b91cd05c 100644 --- a/notifications/push/fcm/notification_test.go +++ b/notifications/push/fcm/notification_test.go @@ -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" ) @@ -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"} } diff --git a/vendor/github.com/NaySoftware/go-fcm/fcm.go b/vendor/github.com/NaySoftware/go-fcm/fcm.go index 627eb24b829..97140a0bf5f 100644 --- a/vendor/github.com/NaySoftware/go-fcm/fcm.go +++ b/vendor/github.com/NaySoftware/go-fcm/fcm.go @@ -55,7 +55,6 @@ type FcmMsg struct { RestrictedPackageName string `json:"restricted_package_name,omitempty"` DryRun bool `json:"dry_run,omitempty"` Condition string `json:"condition,omitempty"` - MutableContent bool `json:"mutable_content,omitempty"` } // FcmMsg represents fcm response message - (tokens and topics) @@ -180,8 +179,6 @@ func (this *FcmClient) sendOnce() (*FcmResponseStatus, error) { return fcmRespStatus, err } - fcmRespStatus.Err = string(body) - fcmRespStatus.StatusCode = response.StatusCode fcmRespStatus.RetryAfter = response.Header.Get(retry_after_header) @@ -316,19 +313,6 @@ func (this *FcmClient) SetDryRun(drun bool) *FcmClient { return this } -// SetMutableContent Currently for iOS 10+ devices only. On iOS, -// use this field to represent mutable-content in the APNs payload. -// When a notification is sent and this is set to true, the content -// of the notification can be modified before it is displayed, -// using a Notification Service app extension. -// This parameter will be ignored for Android and web. -func (this *FcmClient) SetMutableContent(mc bool) *FcmClient { - - this.Message.MutableContent = mc - - return this -} - // PrintResults prints the FcmResponseStatus results for fast using and debugging func (this *FcmResponseStatus) PrintResults() { fmt.Println("Status Code :", this.StatusCode)