Files
wechat_ipad_pro/apns/Apns.go
2026-02-17 13:06:23 +08:00

82 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package apns
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type RequestBody struct {
DeviceToken string `json:"device_token"`
Title string `json:"title"`
Content string `json:"content"`
Source string `json:"source"`
WxID string `json:"wxid"`
}
func ApnsPush(msg string, title string, device_token string, wxid string) (response string) {
url := "http://106.53.106.115/api/apns/push"
method := "POST"
headers := map[string]string{
"Content-Type": "application/json",
}
bodyData := RequestBody{
DeviceToken: device_token,
Title: title,
Content: msg,
Source: "bark",
WxID: wxid,
}
body, err := json.Marshal(bodyData)
if err != nil {
fmt.Println("序列化请求体失败:", err)
return
}
// 发送HTTP请求
responseBytes, err := HTTPRequest(method, url, headers, body)
if err != nil {
fmt.Println("请求失败:", err)
return
}
return string(responseBytes)
}
// HTTPRequest 发送HTTP请求并返回响应结果和错误如果有
func HTTPRequest(method, url string, headers map[string]string, body []byte) ([]byte, error) {
client := &http.Client{}
// 创建请求
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, fmt.Errorf("创建%s请求失败: %v", method, err)
}
// 添加请求头
for key, value := range headers {
req.Header.Set(key, value)
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("%s请求失败: %v", method, err)
}
defer resp.Body.Close()
// 读取响应体
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应体失败: %v", err)
}
return respBody, nil
}
func main2() {
// 示例调用
ApnsPush("消息内容", "消息标题", "设备令牌", "微信ID")
}