|
- package jg_push
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_jg_push.git/md"
- jg_push_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_jg_push.git/utils"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/syyongx/php2go"
- "github.com/tidwall/gjson"
- )
-
- func Send(appKey, appSecret string, param md.PushParam) (string, string, error) {
- if appKey == "" || appSecret == "" {
- return "", "", errors.New("配置未设置")
- }
- url := "https://api.jpush.cn/v3/push"
- var iosAlert = md.PushIosAlert{Title: param.Title, Body: param.Content}
- req := md.PushRequest{
- Platform: param.Platform,
- Audience: param.Audience,
- Notification: struct {
- Android md.PushAndroid `json:"android"`
- Ios md.PushIos `json:"ios"`
- }{
- Android: md.PushAndroid{
- Alert: param.Content,
- Extras: param.Extras,
- Title: param.Title,
- },
- Ios: md.PushIos{
- Alert: iosAlert,
- Extras: param.Extras,
- Sound: "default",
- },
- },
- Message: struct {
- Extras interface{} `json:"extras"`
- MsgContent string `json:"msg_content"`
- }{
- Extras: param.Extras,
- MsgContent: param.Content,
- },
- Options: struct {
- TimeToLive int `json:"time_to_live"`
- }{
- TimeToLive: 86400,
- },
- }
- key := php2go.Base64Encode(appKey + ":" + appSecret)
- headers := map[string]string{
- "Content-Type": "application/json",
- "Authorization": "Basic " + key,
- }
- b, err := json.Marshal(req)
- if err != nil {
- return "", "", err
- }
- res, err := jg_push_utils.CurlPost(url, b, headers)
- if err != nil {
- return "", "", err
- }
- fmt.Println(string(res))
- code := gjson.Get(string(res), "error.code").Int()
- if code > 0 {
- message := gjson.Get(string(res), "error.message").String()
- return "", string(res), errors.New(message)
- }
- msgId := gjson.Get(string(res), "msg_id").String()
- if msgId == "0" || msgId == "" {
- return "", string(res), err
- }
- return msgId, string(res), nil
- }
|