mirror of
https://github.com/wahyd4/jpush-api-go-client.git
synced 2026-08-09 04:46:43 +10:00
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package jpushclient
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
SUCCESS_FLAG = "msg_id"
|
|
HOST_NAME_SSL = "https://api.jpush.cn/v3/push"
|
|
BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
)
|
|
|
|
var base64Coder = base64.NewEncoding(BASE64_TABLE)
|
|
|
|
type PushClient struct {
|
|
MasterSecret string
|
|
AppKey string
|
|
AuthCode string
|
|
BaseUrl string
|
|
}
|
|
|
|
func NewPushClient(secret, appKey string) *PushClient {
|
|
//base64
|
|
auth := "Basic " + base64Coder.EncodeToString([]byte(appKey+":"+secret))
|
|
pusher := &PushClient{secret, appKey, auth, HOST_NAME_SSL}
|
|
return pusher
|
|
}
|
|
|
|
func (this *PushClient) Send(data []byte) (string, error) {
|
|
return this.SendPushBytes(data)
|
|
}
|
|
|
|
func (this *PushClient) SendPushString(content string) (string, error) {
|
|
ret, err := SendPostString(this.BaseUrl, content, this.AuthCode)
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
if strings.Contains(ret, "msg_id") {
|
|
return ret, nil
|
|
} else {
|
|
return "", errors.New(ret)
|
|
}
|
|
}
|
|
|
|
func (this *PushClient) SendPushBytes(content []byte) (string, error) {
|
|
//ret, err := SendPostBytes(this.BaseUrl, content, this.AuthCode)
|
|
ret, err := SendPostBytes2(this.BaseUrl, content, this.AuthCode)
|
|
if err != nil {
|
|
return ret, err
|
|
}
|
|
if strings.Contains(ret, "msg_id") {
|
|
return ret, nil
|
|
} else {
|
|
return "", errors.New(ret)
|
|
}
|
|
}
|