Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ type Notice struct {
}

type AndroidNotice struct {
Alert string `json:"alert"`
Title string `json:"title,omitempty"`
BuilderId int `json:"builder_id,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
Alert string `json:"alert"`
Title string `json:"title,omitempty"`
BuilderId int `json:"builder_id,omitempty"`
BadgeAddNum int `json:"badge_add_num,omitempty"`
BadgeClass string `json:"badge_class,omitempty"`
Extras map[string]interface{} `json:"extras,omitempty"`
}

type IOSNotice struct {
Alert interface{} `json:"alert"`
Sound string `json:"sound,omitempty"`
Badge string `json:"badge,omitempty"`
Badge int `json:"badge,omitempty"`
ContentAvailable bool `json:"content-available,omitempty"`
MutableContent bool `json:"mutable-content,omitempty"`
Category string `json:"category,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions pushclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
BASE64_TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

var base64Coder = base64.NewEncoding(BASE64_TABLE)
var Base64Coder = base64.NewEncoding(BASE64_TABLE)

type PushClient struct {
MasterSecret string
Expand All @@ -27,7 +27,7 @@ type PushClient struct {

func NewPushClient(secret, appKey string) *PushClient {
//base64
auth := "Basic " + base64Coder.EncodeToString([]byte(appKey+":"+secret))
auth := "Basic " + Base64Coder.EncodeToString([]byte(appKey+":"+secret))
pusher := &PushClient{secret, appKey, auth, HOST_NAME_SSL}
return pusher
}
Expand Down
90 changes: 90 additions & 0 deletions tagclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package jpushclient

import (
"encoding/json"
"errors"
"fmt"
"strings"
)

const (
// 查询设备的别名与标签
TAG_HOST_NAME_SSL = "https://device.jpush.cn/v3/"
)

type TagClient struct {
MasterSecret string
AppKey string
AuthCode string
BaseUrl string
}

func NewTagClient(appKey, secret string) *TagClient {
auth := "Basic " + Base64Coder.EncodeToString([]byte(appKey+":"+secret))
pusher := &TagClient{secret, appKey, auth, TAG_HOST_NAME_SSL}
return pusher
}

func (this *TagClient) HttpGet(url string) (string, error) {
rsp, err := Get(url).Header("Authorization", this.AuthCode).String()
if err != nil {
return "", err
}
_, err = UnmarshalResponse(rsp)
if err != nil {
return "", err
}
return rsp, nil
}

// GetAllTag 获取全部tag
func (this *TagClient) GetAllTag() (string, error) {
return this.HttpGet(fmt.Sprintf(this.BaseUrl + "tags"))
}

// GetTag 读取当前设备绑定的全部tag
func (this *TagClient) GetTag(registrationId string) (string, error) {
return this.HttpGet(fmt.Sprintf(this.BaseUrl+"devices/%s", registrationId))
}

// MvRegIdToTag 添加指定设备到标签 add/remove 最多各支持 1000 个
func (this *TagClient) MvRegIdToTag(tag string, addRegIds, rmRegIds []string) (string, error) {
type reqRegIds struct {
Add []string `json:"add,omitempty"`
Remove []string `json:"remove,omitempty"`
}

type req struct {
RegistrationIds reqRegIds `json:"registration_ids"`
}

if len(addRegIds) == 0 && len(rmRegIds) == 0 {
return "", nil
}

postData := req{
RegistrationIds: reqRegIds{
Add: addRegIds,
Remove: rmRegIds,
},
}

data, err := json.Marshal(postData)
if err != nil {
return "", nil
}
return this.SendPushBytes(this.BaseUrl+"tags/"+tag, data)
}

// SendPushBytes 推送
func (this *TagClient) SendPushBytes(url string, content []byte) (string, error) {
ret, err := SendPostBytes2(url, content, this.AuthCode)
if err != nil {
return ret, err
}
if strings.Contains(ret, "msg_id") {
return ret, nil
} else {
return "", errors.New(ret)
}
}
28 changes: 28 additions & 0 deletions tagpayload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package jpushclient

import (
"encoding/json"
)

type TagPayLoad struct {
Tags interface{} `json:"tags"`
Alias interface{} `json:"alias,omitempty"`
Mobile interface{} `json:"mobile,omitempty"`
}

func NewPushTagPayLoad() *TagPayLoad {
pl := &TagPayLoad{}
return pl
}

func (this *TagPayLoad) SetTags(v map[string][]string) {
this.Tags = v
}

func (this *TagPayLoad) ToBytes() ([]byte, error) {
content, err := json.Marshal(this)
if err != nil {
return nil, err
}
return content, nil
}