Skip to content

Commit

Permalink
Fix HMS Push Notifications (#645)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilário Coelho authored Dec 9, 2021
1 parent bd920d3 commit 20e6566
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ require (
google.golang.org/grpc v1.36.1
google.golang.org/protobuf v1.27.1
)

replace github.com/msalihkarakasli/go-hms-push => github.com/spawn2kill/go-hms-push v0.0.0-20211125124117-e20af53b1304
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/msalihkarakasli/go-hms-push v0.0.0-20210731212030-00e7b986815b h1:GPoWsisltEZb9Itu1MMCXCUsYMKqC51nIG+3EqA3MdE=
github.com/msalihkarakasli/go-hms-push v0.0.0-20210731212030-00e7b986815b/go.mod h1:4X2lQHsWGt+e3uRK124A6ndq3IIVymTAzEI9A1kIQKc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nats-io/jwt v0.3.0/go.mod h1:fRYCDE99xlTsqUzISS1Bi75UBJ6ljOJQOAAu5VglpSg=
Expand Down Expand Up @@ -449,6 +447,8 @@ github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJ
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spawn2kill/go-hms-push v0.0.0-20211125124117-e20af53b1304 h1:NFx3I+/cQkqXlnrDzyOAXUDKWFjPQBcO0LSqowMn1Jo=
github.com/spawn2kill/go-hms-push v0.0.0-20211125124117-e20af53b1304/go.mod h1:4X2lQHsWGt+e3uRK124A6ndq3IIVymTAzEI9A1kIQKc=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
Expand Down
63 changes: 29 additions & 34 deletions notify/notification_hms.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/status"

c "github.com/msalihkarakasli/go-hms-push/push/config"
client "github.com/msalihkarakasli/go-hms-push/push/core"
"github.com/msalihkarakasli/go-hms-push/push/model"
Expand All @@ -21,7 +20,7 @@ var (
once sync.Once
)

// GetPushClient use for create HMS Push
// GetPushClient use for create HMS Push.
func GetPushClient(conf *c.Config) (*client.HMSClient, error) {
once.Do(func() {
client, err := client.NewHttpClient(conf)
Expand Down Expand Up @@ -112,47 +111,43 @@ func GetHuaweiNotification(req *PushNotification) (*model.MessageRequest, error)
// Add data fields
if len(req.HuaweiData) > 0 {
msgRequest.Message.Data = req.HuaweiData
} else {
// Notification Message
msgRequest.Message.Android.Notification = model.GetDefaultAndroidNotification()

n := msgRequest.Message.Android.Notification
isNotificationSet := false
}

if req.HuaweiNotification != nil {
isNotificationSet = true
n = req.HuaweiNotification
// Notification Message
if req.HuaweiNotification != nil {
msgRequest.Message.Android.Notification = req.HuaweiNotification

if n.ClickAction == nil {
n.ClickAction = model.GetDefaultClickAction()
}
if msgRequest.Message.Android.Notification.ClickAction == nil {
msgRequest.Message.Android.Notification.ClickAction = model.GetDefaultClickAction()
}
}

if len(req.Message) > 0 {
isNotificationSet = true
n.Body = req.Message
setDefaultAndroidNotification := func() {
if msgRequest.Message.Android == nil {
msgRequest.Message.Android.Notification = model.GetDefaultAndroidNotification()
}
}

if len(req.Title) > 0 {
isNotificationSet = true
n.Title = req.Title
}
if len(req.Message) > 0 {
setDefaultAndroidNotification()
msgRequest.Message.Android.Notification.Body = req.Message
}

if len(req.Image) > 0 {
isNotificationSet = true
n.Image = req.Image
}
if len(req.Title) > 0 {
setDefaultAndroidNotification()
msgRequest.Message.Android.Notification.Title = req.Title
}

if v, ok := req.Sound.(string); ok && len(v) > 0 {
isNotificationSet = true
n.Sound = v
} else {
n.DefaultSound = true
}
if len(req.Image) > 0 {
setDefaultAndroidNotification()
msgRequest.Message.Android.Notification.Image = req.Image
}

if isNotificationSet {
msgRequest.Message.Android.Notification = n
}
if v, ok := req.Sound.(string); ok && len(v) > 0 {
setDefaultAndroidNotification()
msgRequest.Message.Android.Notification.Sound = v
} else if msgRequest.Message.Android.Notification != nil {
msgRequest.Message.Android.Notification.DefaultSound = true
}

b, err := json.Marshal(msgRequest)
Expand Down

0 comments on commit 20e6566

Please sign in to comment.