2021-07-23 17:56:33 +00:00
|
|
|
package notify
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-17 12:14:19 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/logx"
|
2021-07-13 15:58:47 +00:00
|
|
|
"github.com/appleboy/gorush/status"
|
2021-07-13 08:32:39 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
c "github.com/msalihkarakasli/go-hms-push/push/config"
|
2021-07-17 12:14:19 +00:00
|
|
|
client "github.com/msalihkarakasli/go-hms-push/push/core"
|
2020-09-04 03:01:21 +00:00
|
|
|
"github.com/msalihkarakasli/go-hms-push/push/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pushError error
|
2021-07-17 12:14:19 +00:00
|
|
|
pushClient *client.HMSClient
|
2020-09-04 03:01:21 +00:00
|
|
|
once sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPushClient use for create HMS Push
|
2021-07-17 12:14:19 +00:00
|
|
|
func GetPushClient(conf *c.Config) (*client.HMSClient, error) {
|
2020-09-04 03:01:21 +00:00
|
|
|
once.Do(func() {
|
2021-07-17 12:14:19 +00:00
|
|
|
client, err := client.NewHttpClient(conf)
|
2020-09-04 03:01:21 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pushClient = client
|
|
|
|
pushError = err
|
|
|
|
})
|
|
|
|
|
|
|
|
return pushClient, pushError
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitHMSClient use for initialize HMS Client.
|
2021-07-17 12:14:19 +00:00
|
|
|
func InitHMSClient(cfg config.ConfYaml, appSecret, appID string) (*client.HMSClient, error) {
|
2021-03-28 14:13:02 +00:00
|
|
|
if appSecret == "" {
|
|
|
|
return nil, errors.New("Missing Huawei App Secret")
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if appID == "" {
|
2021-03-28 14:13:02 +00:00
|
|
|
return nil, errors.New("Missing Huawei App ID")
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
conf := &c.Config{
|
2020-09-04 03:01:21 +00:00
|
|
|
AppId: appID,
|
2021-03-28 14:13:02 +00:00
|
|
|
AppSecret: appSecret,
|
|
|
|
AuthUrl: "https://oauth-login.cloud.huawei.com/oauth2/v3/token",
|
|
|
|
PushUrl: "https://push-api.cloud.huawei.com",
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if appSecret != cfg.Huawei.AppSecret || appID != cfg.Huawei.AppID {
|
2020-09-04 03:01:21 +00:00
|
|
|
return GetPushClient(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
if HMSClient == nil {
|
|
|
|
return GetPushClient(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HMSClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHuaweiNotification use for define HMS notification.
|
|
|
|
// HTTP Connection Server Reference for HMS
|
|
|
|
// https://developer.huawei.com/consumer/en/doc/development/HMS-References/push-sendapi
|
|
|
|
func GetHuaweiNotification(req PushNotification) (*model.MessageRequest, error) {
|
|
|
|
msgRequest := model.NewNotificationMsgRequest()
|
|
|
|
|
|
|
|
msgRequest.Message.Android = model.GetDefaultAndroid()
|
|
|
|
|
|
|
|
if len(req.Tokens) > 0 {
|
|
|
|
msgRequest.Message.Token = req.Tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Topic) > 0 {
|
|
|
|
msgRequest.Message.Topic = req.Topic
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.To) > 0 {
|
|
|
|
msgRequest.Message.Topic = req.To
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Condition) > 0 {
|
|
|
|
msgRequest.Message.Condition = req.Condition
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Priority == "high" {
|
|
|
|
msgRequest.Message.Android.Urgency = "HIGH"
|
|
|
|
}
|
|
|
|
|
2021-01-23 01:39:06 +00:00
|
|
|
// if req.HuaweiCollapseKey != nil {
|
2020-09-04 03:01:21 +00:00
|
|
|
msgRequest.Message.Android.CollapseKey = req.HuaweiCollapseKey
|
|
|
|
//}
|
|
|
|
|
|
|
|
if len(req.Category) > 0 {
|
|
|
|
msgRequest.Message.Android.Category = req.Category
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.HuaweiTTL) > 0 {
|
|
|
|
msgRequest.Message.Android.TTL = req.HuaweiTTL
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.BiTag) > 0 {
|
|
|
|
msgRequest.Message.Android.BiTag = req.BiTag
|
|
|
|
}
|
|
|
|
|
|
|
|
msgRequest.Message.Android.FastAppTarget = req.FastAppTarget
|
|
|
|
|
2021-01-23 01:39:06 +00:00
|
|
|
// Add data fields
|
2020-09-04 03:01:21 +00:00
|
|
|
if len(req.HuaweiData) > 0 {
|
|
|
|
msgRequest.Message.Data = req.HuaweiData
|
|
|
|
} else {
|
2021-01-23 01:39:06 +00:00
|
|
|
// Notification Message
|
2020-09-04 03:01:21 +00:00
|
|
|
msgRequest.Message.Android.Notification = model.GetDefaultAndroidNotification()
|
|
|
|
|
|
|
|
n := msgRequest.Message.Android.Notification
|
|
|
|
isNotificationSet := false
|
|
|
|
|
|
|
|
if req.HuaweiNotification != nil {
|
|
|
|
isNotificationSet = true
|
|
|
|
n = req.HuaweiNotification
|
|
|
|
|
|
|
|
if n.ClickAction == nil {
|
|
|
|
n.ClickAction = model.GetDefaultClickAction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Message) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Body = req.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Title) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Title = req.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(req.Image) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Image = req.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := req.Sound.(string); ok && len(v) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Sound = v
|
|
|
|
} else {
|
|
|
|
n.DefaultSound = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNotificationSet {
|
|
|
|
msgRequest.Message.Android.Notification = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(msgRequest)
|
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("Failed to marshal the default message! Error is " + err.Error())
|
2020-09-04 03:01:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debugf("Default message is %s", string(b))
|
2020-09-04 03:01:21 +00:00
|
|
|
return msgRequest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushToHuawei provide send notification to Android server.
|
2021-07-16 08:30:01 +00:00
|
|
|
func PushToHuawei(req PushNotification) bool {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Start push notification for Huawei")
|
2021-07-17 12:14:19 +00:00
|
|
|
|
|
|
|
if req.Cfg.Core.Sync && !core.IsLocalQueue(core.Queue(req.Cfg.Queue.Engine)) {
|
|
|
|
req.Cfg.Core.Sync = false
|
|
|
|
}
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
var (
|
2021-07-17 12:14:19 +00:00
|
|
|
client *client.HMSClient
|
2020-09-04 03:01:21 +00:00
|
|
|
retryCount = 0
|
2021-07-17 12:14:19 +00:00
|
|
|
maxRetry = req.Cfg.Huawei.MaxRetry
|
2020-09-04 03:01:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if req.Retry > 0 && req.Retry < maxRetry {
|
|
|
|
maxRetry = req.Retry
|
|
|
|
}
|
|
|
|
|
|
|
|
// check message
|
|
|
|
err := CheckMessage(req)
|
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("request error: " + err.Error())
|
2020-09-04 03:01:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
Retry:
|
2021-01-23 01:39:06 +00:00
|
|
|
isError := false
|
2020-09-04 03:01:21 +00:00
|
|
|
|
2020-12-13 14:09:05 +00:00
|
|
|
notification, _ := GetHuaweiNotification(req)
|
2020-09-04 03:01:21 +00:00
|
|
|
|
2021-07-17 12:14:19 +00:00
|
|
|
client, err = InitHMSClient(req.Cfg, req.Cfg.Huawei.AppSecret, req.Cfg.Huawei.AppID)
|
2020-09-04 03:01:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// HMS server error
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("HMS server error: " + err.Error())
|
2020-09-04 03:01:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := client.SendMessage(context.Background(), notification)
|
|
|
|
if err != nil {
|
|
|
|
// Send Message error
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("HMS server send message error: " + err.Error())
|
2020-09-04 03:01:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Huawei Push Send API does not support exact results for each token
|
|
|
|
if res.Code == "80000000" {
|
2021-07-13 15:58:47 +00:00
|
|
|
status.StatStorage.AddHuaweiSuccess(int64(1))
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Huwaei Send Notification is completed successfully!")
|
2020-09-04 03:01:21 +00:00
|
|
|
} else {
|
|
|
|
isError = true
|
2021-07-13 15:58:47 +00:00
|
|
|
status.StatStorage.AddHuaweiError(int64(1))
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Huawei Send Notification is failed! Code: " + res.Code)
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if isError && retryCount < maxRetry {
|
|
|
|
retryCount++
|
|
|
|
|
|
|
|
// resend all tokens
|
|
|
|
goto Retry
|
|
|
|
}
|
|
|
|
|
|
|
|
return isError
|
|
|
|
}
|