You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/push/jpush/push.go

90 lines
2.0 KiB

package push
import (
"Open_IM/internal/push"
2 years ago
"Open_IM/internal/push/jpush/body"
"Open_IM/internal/push/jpush/common"
"Open_IM/pkg/common/config"
"bytes"
2 years ago
"encoding/base64"
"encoding/json"
2 years ago
"fmt"
"io/ioutil"
"net/http"
)
3 years ago
var (
JPushClient *JPush
)
func init() {
2 years ago
JPushClient = newJPushClient()
3 years ago
}
type JPush struct{}
2 years ago
func newJPushClient() *JPush {
3 years ago
return &JPush{}
}
3 years ago
func (j *JPush) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
return token, nil
}
3 years ago
func (j *JPush) SetAlias(cid, alias string) (resp string, err error) {
return resp, nil
}
2 years ago
func (j *JPush) getAuthorization(Appkey string, MasterSecret string) string {
str := fmt.Sprintf("%s:%s", Appkey, MasterSecret)
buf := []byte(str)
Authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(buf))
return Authorization
}
2 years ago
func (j *JPush) Push(accounts []string, title, detailContent, operationID string, opts push.PushOpts) (string, error) {
var pf body.Platform
3 years ago
pf.SetAll()
2 years ago
var au body.Audience
au.SetAlias(accounts)
2 years ago
var no body.Notification
var extras body.Extras
if opts.Signal.ClientMsgID != "" {
extras.ClientMsgID = opts.Signal.ClientMsgID
}
no.IOSEnableMutableContent()
no.SetExtras(extras)
no.SetAlert(title)
2 years ago
var me body.Message
me.SetMsgContent(detailContent)
2 years ago
var o body.Options
o.SetApnsProduction(config.Config.IOSPush.Production)
2 years ago
var po body.PushObj
po.SetPlatform(&pf)
po.SetAudience(&au)
po.SetNotification(&no)
po.SetMessage(&me)
po.SetOptions(&o)
con, err := json.Marshal(po)
if err != nil {
3 years ago
return "", err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Jpns.PushUrl, bytes.NewBuffer(con))
if err != nil {
3 years ago
return "", err
}
2 years ago
req.Header.Set("Authorization", j.getAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret))
resp, err := client.Do(req)
if err != nil {
3 years ago
return "", err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
3 years ago
return "", err
}
3 years ago
return string(result), nil
}