Skip to content

Commit

Permalink
feat: 重要通知
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzc19 committed Dec 22, 2024
1 parent a99e1fa commit 4b42107
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Bark API 请求简单封装
- [x] 自定义图标
- [x] 消息分组
- [ ] 加密
- [ ] 警告
- [x] 警告
- [x] 时效性通知
- [x] URL跳转
- [x] 复制推送内容/自动复制
Expand Down
30 changes: 27 additions & 3 deletions bark.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ type Req struct {
Call bool
// NotArchive 是否不保存消息(保存在Bark中) 默认保存
NotArchive bool
// 推送图标 错误图标不会显示
// Icon 推送图标 错误图标不会显示
Icon string
GroupName string
// URL 点击跳转
URL string

// Critical 重要警告 会忽略静音设置和勿扰模式
Critical bool
// Volume 重要警告的音量 默认为5
Volume *int
// 时效性
Level Level
// URL 点击跳转
URL string
// Copy 下拉等出现复制按钮时点击复制[Copy]的值
Copy string
// AutoCopy 自动复制 iOS14.5之后长按或下拉可触发自动复制,iOS14.5之前无需任何操作即可触发自动复制
AutoCopy bool
// Badge 角标
Badge int

Debug bool
}

type _resp struct {
Expand Down Expand Up @@ -77,6 +84,19 @@ func Notify(req Req) error {
if req.GroupName != "" {
url = fmt.Sprintf("%sgroup=%s&", url, req.GroupName)
}
if req.Critical {
if req.Volume == nil {
req.Volume = new(int)
*req.Volume = 5
}
if *req.Volume > 10 {
*req.Volume = 10
}
if *req.Volume < 0 {
*req.Volume = 0
}
url = fmt.Sprintf("%slevel=critical&volume=%o", url, *req.Volume)
}
if req.Level != "" {
url = fmt.Sprintf("%slevel=%s&", url, req.Level)
}
Expand All @@ -93,6 +113,10 @@ func Notify(req Req) error {
url = fmt.Sprintf("%sbadge=%o&", url, req.Badge)
}

if req.Debug {
fmt.Println(url)
}

resp, err := http.Get(url)
if err != nil {
return err
Expand Down
18 changes: 17 additions & 1 deletion bark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"testing"
)

var req = Req{}
var req = Req{
Debug: true,
}

func before() {
var err = godotenv.Load()
Expand Down Expand Up @@ -56,6 +58,20 @@ func TestIcon(t *testing.T) {
}
}

func TestCritical(t *testing.T) {
before()

req.Title = "Critical"
req.Content = "TestCritical"
req.Critical = true
//*req.Volume = -100

err := Notify(req)
if err != nil {
log.Fatal(err)
}
}

func TestLevel(t *testing.T) {
before()

Expand Down

0 comments on commit 4b42107

Please sign in to comment.