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

Fix HMS Push Notifications #645

Merged
merged 3 commits into from
Dec 9, 2021
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
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