Skip to content

Commit

Permalink
feat(vivo): 添加通知id设置
Browse files Browse the repository at this point in the history
  • Loading branch information
am6737 committed Mar 12, 2024
1 parent 675e95f commit 695392b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
19 changes: 10 additions & 9 deletions api/http/v1/dto/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
)

type HuaweiPushRequestData struct {
DryRun bool `json:"dry_run,omitempty"`
Foreground bool `json:"foreground,omitempty"`
TTL string `json:"ttl,omitempty"`
Type string `json:"type,omitempty"`
Expand Down Expand Up @@ -71,14 +70,16 @@ type Sound struct {
}

type VivoPushRequestData struct {
DryRun bool
Foreground bool
TTL int
Type string
Title string
Message string
Category string
Data map[string]string
Foreground bool `json:"foreground,omitempty"`
TTL int `json:"ttl,omitempty"`
// NotifyType 通知类型 1:无,2:响铃,3:振动,4:响铃和振动
NotifyType int `json:"notify_type,omitempty"`
NotifyID string `json:"notify_id,omitempty"`
Title string `json:"title"`
Content string `json:"content"`
Category string `json:"category,omitempty"`
Data map[string]string `json:"data,omitempty"`
ClickAction ClickAction `json:"click_action,omitempty"`
}

type OppoPushRequestData struct {
Expand Down
25 changes: 14 additions & 11 deletions internal/server/http/vivo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/cossim/hipush/pkg/notify"
"github.com/cossim/hipush/pkg/push"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"net/http"
)

Expand All @@ -34,20 +33,24 @@ func (h *Handler) handleVivoPush(c *gin.Context, req *dto.PushRequest) error {
h.logger.Info("Handling push request", "platform", req.Platform, "appID", req.AppID, "tokens", req.Token, "req", r)

rr := &notify.VivoPushNotification{
AppID: req.AppID,
RequestId: uuid.New().String(),
Tokens: req.Token,
Title: r.Title,
Message: r.Message,
Category: r.Category,
Data: r.Data,
ClickAction: nil,
NotifyType: 0,
AppID: req.AppID,
RequestId: r.NotifyID,
Tokens: req.Token,
Title: r.Title,
Message: r.Content,
Category: r.Category,
Data: r.Data,
ClickAction: &notify.VivoClickAction{
Action: r.ClickAction.Action,
Url: r.ClickAction.Url,
Activity: r.ClickAction.Activity,
},
NotifyType: r.NotifyType,
TTL: r.TTL,
Retry: 0,
SendOnline: false,
Foreground: r.Foreground,
Development: true,
Development: req.Option.Development,
}
if err := service.Send(c, rr, &push.SendOptions{
DryRun: req.Option.DryRun,
Expand Down
9 changes: 7 additions & 2 deletions pkg/notify/vivo_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ type VivoPushNotification struct {

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

// Activity 打开应用内页(activity 的 intent action)
Activity string `json:"activity,omitempty"`

// Url 打开网页的地址
Url string `json:"url,omitempty"`
}
4 changes: 2 additions & 2 deletions pkg/push/honor_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (h *HonorService) send(ctx context.Context, appid string, token string, not
// res, err := client.SendMessage(ctx, appid, notification)
// if err != nil || (res != nil && res.Code != 200) {
// if err == nil {
// err = errors.New(res.Message)
// err = errors.New(res.Content)
// } else {
// es = append(es, err)
// }
Expand All @@ -160,7 +160,7 @@ func (h *HonorService) send(ctx context.Context, appid string, token string, not
// h.status.AddHonorFailed(1)
//
// } else {
// log.Printf("honor send success: %s", res.Message)
// log.Printf("honor send success: %s", res.Content)
// h.status.AddHonorSuccess(1)
// }
// }(notification, token)
Expand Down
14 changes: 11 additions & 3 deletions pkg/push/vivo_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
vp "github.com/cossim/vivo-push"
"github.com/go-logr/logr"
"log"
"net/url"
"strings"
)

Expand Down Expand Up @@ -143,8 +144,7 @@ func (v *VivoService) buildNotification(req *notify.VivoPushNotification) (*vp.M

// 设置默认的 ClickAction
defaultClickAction := &notify.VivoClickAction{
Action: 1,
Content: "",
Action: 1,
}

// 检查 ClickAction 是否为空,为空则使用默认值
Expand All @@ -153,6 +153,14 @@ func (v *VivoService) buildNotification(req *notify.VivoPushNotification) (*vp.M
clickAction = defaultClickAction
}

// 检查 URL 是否为合法 URL
if clickAction.Action == 2 {
_, err := url.Parse(clickAction.Url)
if err != nil {
return nil, err
}
}

// 检查 NotifyType 是否为有效值
if req.NotifyType != 0 && req.NotifyType < 1 || req.NotifyType > 4 {
return nil, errors.New("invalid notify type")
Expand All @@ -178,7 +186,7 @@ func (v *VivoService) buildNotification(req *notify.VivoPushNotification) (*vp.M
Content: req.Message,
TimeToLive: int64(req.TTL),
SkipType: clickAction.Action,
SkipContent: clickAction.Content,
SkipContent: clickAction.Url,
NetworkType: -1,
ClientCustomMap: req.Data,
//Extra: req.Data.ExtraMap(),
Expand Down

0 comments on commit 695392b

Please sign in to comment.