Skip to content

Commit

Permalink
feat(vivo_push): 添加vivo推送
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 1, 2024
1 parent 6e7f886 commit bc85924
Show file tree
Hide file tree
Showing 14 changed files with 467 additions and 23 deletions.
11 changes: 11 additions & 0 deletions api/http/v1/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ type Sound struct {
Name string `json:"name"`
Volume float64 `json:"volume"`
}

type VivoPushRequestData struct {
DryRun bool
Foreground bool
TTL int
Type string
Title string
Message string
Category string
Data map[string]string
}
2 changes: 0 additions & 2 deletions api/http/v1/dto/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ type PushRequest struct {
// ios capacitor.config文件中的appId 例如com.hitosea.apptest
AppID string `json:"app_id" binding:"required"`

AppSecret string `json:"app_secret"`

// 自定义的消息数据,不同平台可能有不同的格式
Data interface{} `json:"data"`
}
42 changes: 34 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"log"
"os"
"os/signal"
"sync"
"syscall"
)

Expand Down Expand Up @@ -64,28 +65,53 @@ func main() {
return adapter.NewPushServiceAdapter(svc)
})

pushServiceFactory.Register(consts.PlatformVivo.String(), func() push.PushService {
svc, err := push.NewVivoService(cfg)
if err != nil {
panic(err)
}
return adapter.NewPushServiceAdapter(svc)
})

// 创建一个父上下文和取消函数
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var wg sync.WaitGroup

if cfg.HTTP.Enabled {
wg.Add(1)
go func() {
defer wg.Done()
httpHandler := h.NewHandler(cfg, zapr.NewLogger(zapLogger), pushServiceFactory)
if err := httpHandler.Start(context.Background()); err != nil {
if err := httpHandler.Start(ctx); err != nil {
log.Fatalf("failed to start HTTP server: %v", err)
}
}()
}

if cfg.GRPC.Enabled {
wg.Add(1)
go func() {
defer wg.Done()
grpcHandler := g.NewHandler(cfg, zapr.NewLogger(zapLogger), pushServiceFactory)
if err := grpcHandler.Start(context.Background()); err != nil {
if err := grpcHandler.Start(ctx); err != nil {
log.Fatalf("failed to start GRPC server: %v", err)
}
}()
}

// 等待信号
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
<-signalChan

log.Println("shutting down...")
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
go func() {
select {
case <-ctx.Done():
wg.Wait()
return
case <-sig:
log.Println("receive system signal, cancel context")
cancel()
}
}()
wg.Wait()
}
19 changes: 14 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type iOSAppConfig struct {
MaxRetry int `yaml:"max_retry"`
Enabled bool `yaml:"enabled"`
Production bool `yaml:"production"`
AppID string `yaml:"appid"`
AppID string `yaml:"app_id"`
KeyPath string `yaml:"key_path"`
KeyType string `yaml:"key_type"`
Password string `yaml:"password"`
Expand All @@ -21,17 +21,25 @@ type iOSAppConfig struct {

type HuaweiAppConfig struct {
Enabled bool `yaml:"enabled"`
AppID string `yaml:"appid"`
AppSecret string `yaml:"appsecret"`
AppID string `yaml:"app_id"`
AppSecret string `yaml:"app_secret"`
AuthUrl string `yaml:"auth_url"`
PushUrl string `yaml:"push_url"`
MaxRetry int `yaml:"max_retry"`
}

type VivoAppConfig struct {
Enabled bool `yaml:"enabled"`
AppID string `yaml:"app_id"`
AppKey string `yaml:"app_key"`
AppSecret string `yaml:"app_secret"`
MaxRetry int `yaml:"max_retry"`
}

type AndroidAppConfig struct {
Enabled bool `yaml:"enabled"`
AppID string `yaml:"appid"`
AppKey string `yaml:"appkey"`
AppID string `yaml:"app_id"`
AppKey string `yaml:"app_key"`
MaxRetry int `yaml:"max_retry"`
}

Expand All @@ -41,6 +49,7 @@ type Config struct {
IOS []iOSAppConfig `yaml:"ios"`
Huawei []HuaweiAppConfig `yaml:"huawei"`
Android []AndroidAppConfig `yaml:" android"`
Vivo []VivoAppConfig `yaml:"vivo"`
}

type HTTPConfig struct {
Expand Down
38 changes: 35 additions & 3 deletions config/example.yaml
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
http:
enabled: true
address: "0.0.0.0"
port: 7070

grpc:
enabled: true
address: "0.0.0.0"
port: 7071

ios:
- enabled: true

# 应用程序的 Bundle ID
# ios capacitor.config文件中的appId 例如com.hitosea.apptest
appid: com.hitosea.apptest

# APNs 密钥文件路径
key_path: ""

# 密钥类型(例如:pem)
key_type: pem

# 密钥文件的密码(如果有)
password: ""

# 是否为生产环境
production: false

# 最大并发推送数
max_concurrent_pushes: 100
# 最大重试次数

max_retry: 5

# 密钥 ID
key_id: ""

# 开发团队 ID
team_id: ""

- enabled: true
appid: ""
app_id: ""
key_path: ""
key_type: pem
password: ""
Expand All @@ -31,8 +49,22 @@ ios:
max_retry: 0
key_id: ""
team_id: ""

huawei:
- enabled: true
appid: huawei-appid-1
appsecret: huawei-app-secret-1
app_id: huawei-appid-1
app_secret: huawei-app-secret-1

# 华为认证URL
auth_url: https://oauth-login.cloud.huawei.com/oauth2/v3/token

# 华为推送URL
push_url: https://push-api.cloud.huawei.com
max_retry: 5

vivo:
- enabled: true
app_id: ""
app_key: ""
app_secret: ""
max_retry: 5
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ go 1.20
require (
github.com/appleboy/go-fcm v0.1.6
github.com/cossim/go-hms-push v0.0.0-20240301034220-38310a1d80e5
github.com/cossim/vivo-push v0.0.0-20240301025332-148acd987861
github.com/gin-gonic/gin v1.9.1
github.com/go-logr/logr v1.4.1
github.com/go-logr/zapr v1.3.0
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/sideshow/apns2 v0.23.0
go.uber.org/zap v1.27.0
golang.org/x/net v0.20.0
google.golang.org/grpc v1.62.0
google.golang.org/protobuf v1.32.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/bytedance/sonic v1.10.0-rc // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
Expand All @@ -31,16 +33,19 @@ require (
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
Expand Down
18 changes: 17 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/appleboy/go-fcm v0.1.6 h1:F3xHY3HxL/aZg2quFq0DaEGeXCjjoq5JWa3NCHkUup8=
github.com/appleboy/go-fcm v0.1.6/go.mod h1:MSxZ4LqGRsnywOjnlXJXMqbjZrG4vf+0oHitfC9HRH0=
github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow=
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc h1:3S5HeWxjX08CUqNrXtEittExpJsEKBNzrV5UnrzHxVQ=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
Expand All @@ -13,6 +15,9 @@ github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/cossim/go-hms-push v0.0.0-20240301034220-38310a1d80e5 h1:AGXSM5ClpfmtNEFQvJUncqHDbnxp9ZzcpymKKvSpHIY=
github.com/cossim/go-hms-push v0.0.0-20240301034220-38310a1d80e5/go.mod h1:QqZK9q8Ecb/frr2gWxb4+wz4MAFRzlVelNCr1VFRZrQ=
github.com/cossim/vivo-push v0.0.0-20240301025332-148acd987861 h1:7VkgDgzr3C38pLkyDjhKRqOIrlhb1nVz8JL+0lSi4Xw=
github.com/cossim/vivo-push v0.0.0-20240301025332-148acd987861/go.mod h1:PHaZK+wOOXEZR4B8pt2MYWpV1o+jGZ1CcAh4858IvTg=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -43,12 +48,18 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
Expand All @@ -62,8 +73,13 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sideshow/apns2 v0.23.0 h1:lpkikaZ995GIcKk6AFsYzHyezCrsrfEDvUWcWkEGErY=
github.com/sideshow/apns2 v0.23.0/go.mod h1:7Fceu+sL0XscxrfLSkAoH6UtvKefq3Kq1n4W3ayQZqE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -117,8 +133,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
5 changes: 5 additions & 0 deletions internal/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const (
NORMAL = "nornal"
)

type ClickAction struct {
Action string `json:"action,omitempty"`
Content string `json:"content,omitempty"`
}

// D provide string array
type D map[string]interface{}

Expand Down
43 changes: 43 additions & 0 deletions internal/notify/vivo_notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package notify

// VivoPushNotification
// https://dev.vivo.com.cn/documentCenter/doc/362#:~:text=%E6%8E%A5%E5%8F%A3%E5%AE%9A%E4%B9%89-,%E8%BE%93%E5%85%A5%E5%8F%82%E6%95%B0%EF%BC%9A,-intent%20uri
type VivoPushNotification struct {
AppID string `json:"app_id,omitempty"`
RequestId string `json:"request_id,omitempty"`
// Tokens 对应regId列表
Tokens []string `json:"tokens" binding:"required"`

Title string `json:"title,omitempty"`
Message string `json:"message,omitempty"`
Category string `json:"category,omitempty"`

// Data 透传数据 客户端自定义键值对 key和Value键值对总长度不能超过1024字符
Data map[string]string `json:"data,omitempty"`

ClickAction *VivoClickAction `json:"click_action,omitempty"`

// NotifyType 通知类型 1:无,2:响铃,3:振动,4:响铃和振动
NotifyType int `json:"notify_type,omitempty"`

// TTL 消息缓存时间,单位是秒,取值至少60秒,最长一天。当值为空时,默认一天
TTL int `json:"ttl,omitempty"`

// Retry 重试次数
Retry int `json:"retry,omitempty"`

// SendOnline true表示是在线直推,false表示非直推,设备离线直接丢弃
SendOnline bool `json:"send_online,omitempty"`

// Foreground 是否前台通知展示
Foreground bool `json:"foreground,omitempty"`

// Development 对应PushMode
Development bool `json:"development,omitempty"`
}

type VivoClickAction struct {
// Action 点击跳转类型 1:打开APP首页 2:打开链接 3:自定义 4:打开app内指定页面
Action int `json:"action,omitempty"`
Content string `json:"content,omitempty"`
}
Loading

0 comments on commit bc85924

Please sign in to comment.