jpushclient

package module
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

jpush-api-go-client

概述

这是JPush REST API 的 go 版本封装开发包,仅支持最新的REST API v3功能。 REST API 文档:http://docs.jpush.cn/display/dev/Push-API-v3

使用

go get gitee.com/allan577/go-lib-jpush

推送流程

1.构建要推送的平台: jpushclient.Platform
//Platform
var pf jpushclient.Platform
pf.Add(jpushclient.ANDROID)
pf.Add(jpushclient.IOS)
pf.Add(jpushclient.WINPHONE)
//pf.All()
2.构建接收听众: jpushclient.Audience
//Audience
var ad jpushclient.Audience
s := []string{"t1", "t2", "t3"}
ad.SetTag(s)
id := []string{"1", "2", "3"}
ad.SetID(id)
//ad.All()
3.构建通知 jpushclient.Notice,或者消息: jpushclient.Message
//Notice
var notice jpushclient.Notice
notice.SetAlert("alert_test")
notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "AndroidNotice"})
notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "IOSNotice"})
notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})
  
//jpushclient.Message
var msg jpushclient.Message
msg.Title = "Hello"
msg.Content = "你是ylywn"
4.构建jpushclient.PayLoad
payload := jpushclient.NewPushPayLoad()
payload.SetPlatform(&pf)
payload.SetAudience(&ad)
payload.SetMessage(&msg)
payload.SetNotice(&notice)
5.构建PushClient,发出推送
c := jpushclient.NewPushClient(secret, appKey)
r, err := c.Send(bytes)
if err != nil {
	fmt.Printf("err:%s", err.Error())
} else {
	fmt.Printf("ok:%s", r)
}
6.完整demo
package main

import (
	"fmt"
	"github.com/ylywyn/jpush-api-go-client"
)

const (
	appKey = "you jpush appkey"
	secret = "you jpush secret"
)

func main() {

	//Platform
	var pf jpushclient.Platform
	pf.Add(jpushclient.ANDROID)
	pf.Add(jpushclient.IOS)
	pf.Add(jpushclient.WINPHONE)
	//pf.All()

	//Audience
	var ad jpushclient.Audience
	s := []string{"1", "2", "3"}
	ad.SetTag(s)
	ad.SetAlias(s)
	ad.SetID(s)
	//ad.All()

	//Notice
	var notice jpushclient.Notice
	notice.SetAlert("alert_test")
	notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "AndroidNotice"})
	notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "IOSNotice"})
	notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})

	var msg jpushclient.Message
	msg.Title = "Hello"
	msg.Content = "你是ylywn"

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	payload.SetNotice(&notice)

	bytes, _ := payload.ToBytes()
	fmt.Printf("%s\r\n", string(bytes))

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	str, err := c.Send(bytes)
	if err != nil {
		fmt.Printf("err:%s", err.Error())
	} else {
		fmt.Printf("ok:%s", str)
	}
}

Documentation

Index

Constants

View Source
const (
	TAG     = "tag"
	TAG_AND = "tag_and"
	TAG_NOT = "tag_not"
	ALIAS   = "alias"
	ID      = "registration_id"
)
View Source
const (
	CHARSET                    = "UTF-8"
	CONTENT_TYPE_JSON          = "application/json"
	DEFAULT_CONNECTION_TIMEOUT = 20 //seconds
	DEFAULT_SOCKET_TIMEOUT     = 30 // seconds
)
View Source
const (
	IOS      = "ios"
	ANDROID  = "android"
	WINPHONE = "winphone"
)
View Source
const (
	SUCCESS_FLAG  = "msg_id"
	HOST_NAME_SSL = "https://api.jpush.cn/v3/push"
	HOST_SCHEDULE = "https://api.jpush.cn/v3/schedules"
	HOST_REPORT   = "https://report.jpush.cn/v3/received"
	BASE64_TABLE  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
)

Variables

This section is empty.

Functions

func SendPostBytes

func SendPostBytes(url string, content []byte, authCode string) (string, error)

func SendPostBytes2

func SendPostBytes2(url string, data []byte, authCode string) (string, error)

func SendPostString

func SendPostString(url, content, authCode string) (string, error)

func TimeoutDialer

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)

TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.

func UnmarshalResponse

func UnmarshalResponse(rsp string) (map[string]interface{}, error)

Types

type AndroidNotice

type AndroidNotice struct {
	Alert       string                 `json:"alert"`
	Title       string                 `json:"title,omitempty"`
	UriActivity string                 `json:"uri_activity,omitempty"`
	UriAction   string                 `json:"uri_action,omitempty"`
	BuilderId   int                    `json:"builder_id,omitempty"`
	Style       int                    `json:"style,omitempty"`
	Inbox       map[string]interface{} `json:"inbox,omitempty"`
	Extras      map[string]interface{} `json:"extras,omitempty"`
}

type Audience

type Audience struct {
	Object interface{}
	// contains filtered or unexported fields
}

func (*Audience) All

func (a *Audience) All()

func (*Audience) SetAlias

func (a *Audience) SetAlias(alias []string)

func (*Audience) SetID

func (a *Audience) SetID(ids []string)

func (*Audience) SetTag

func (a *Audience) SetTag(tags []string)

func (*Audience) SetTagAnd

func (a *Audience) SetTagAnd(tags []string)

func (*Audience) SetTagNot

func (a *Audience) SetTagNot(tags []string)

type HttpRequest

type HttpRequest struct {
	// contains filtered or unexported fields
}

HttpRequest provides more useful methods for requesting one url than http.Request.

func Delete

func Delete(url string) *HttpRequest

func Get

func Get(url string) *HttpRequest

Get returns *HttpRequest with GET method.

func Post

func Post(url string) *HttpRequest

Post returns *HttpRequest with POST method.

func (*HttpRequest) Body

func (b *HttpRequest) Body(data interface{}) *HttpRequest

Body adds request raw body. it supports string and []byte.

func (*HttpRequest) Bytes

func (b *HttpRequest) Bytes() ([]byte, error)

Bytes returns the body []byte in response. it calls Response inner.

func (*HttpRequest) Header

func (b *HttpRequest) Header(key, value string) *HttpRequest

Header add header item string in request.

func (*HttpRequest) Param

func (b *HttpRequest) Param(key, value string) *HttpRequest

Param adds query param in to request. params build query string as ?key1=value1&key2=value2...

func (*HttpRequest) Response

func (b *HttpRequest) Response() (*http.Response, error)

Response executes request client gets response mannually.

func (*HttpRequest) SetBasicAuth

func (b *HttpRequest) SetBasicAuth(userName, password string) *HttpRequest

func (*HttpRequest) SetCookie

func (b *HttpRequest) SetCookie(cookie *http.Cookie) *HttpRequest

SetCookie add cookie into request.

func (*HttpRequest) SetProtocolVersion

func (b *HttpRequest) SetProtocolVersion(vers string) *HttpRequest

Set the protocol version for incoming requests. Client requests always use HTTP/1.1.

func (*HttpRequest) SetProxy

func (b *HttpRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *HttpRequest

Set http proxy example:

func(req *http.Request) (*url.URL, error) {
	u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
	return u, nil
}

func (*HttpRequest) SetTLSClientConfig

func (b *HttpRequest) SetTLSClientConfig(config *tls.Config) *HttpRequest

SetTLSClientConfig sets tls connection configurations if visiting https url.

func (*HttpRequest) SetTimeout

func (b *HttpRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *HttpRequest

SetTimeout sets connect time out and read-write time out for Request.

func (*HttpRequest) SetTransport

func (b *HttpRequest) SetTransport(transport http.RoundTripper) *HttpRequest

Set transport to

func (*HttpRequest) String

func (b *HttpRequest) String() (string, error)

String returns the body string in response. it calls Response inner.

func (*HttpRequest) ToFile

func (b *HttpRequest) ToFile(filename string) error

ToFile saves the body data in response to one file. it calls Response inner.

func (*HttpRequest) ToJson

func (b *HttpRequest) ToJson(v interface{}) error

ToJson returns the map that marshals from the body bytes as json in response . it calls Response inner.

func (*HttpRequest) ToXML

func (b *HttpRequest) ToXML(v interface{}) error

ToXml returns the map that marshals from the body bytes as xml in response . it calls Response inner.

type IOSNotice

type IOSNotice struct {
	Alert            interface{}            `json:"alert"`
	Sound            string                 `json:"sound,omitempty"`
	Badge            string                 `json:"badge,omitempty"`
	ContentAvailable bool                   `json:"content-available,omitempty"`
	MutableContent   bool                   `json:"mutable-content,omitempty"`
	Category         string                 `json:"category,omitempty"`
	Extras           map[string]interface{} `json:"extras,omitempty"`
}

type Message

type Message struct {
	Content     string                 `json:"msg_content"`
	Title       string                 `json:"title,omitempty"`
	ContentType string                 `json:"content_type,omitempty"`
	Extras      map[string]interface{} `json:"extras,omitempty"`
}

func (*Message) AddExtras

func (this *Message) AddExtras(key string, value interface{})

func (*Message) SetContent

func (this *Message) SetContent(c string)

func (*Message) SetContentType

func (this *Message) SetContentType(t string)

func (*Message) SetTitle

func (this *Message) SetTitle(title string)

type Notice

type Notice struct {
	Alert    string          `json:"alert,omitempty"`
	Android  *AndroidNotice  `json:"android,omitempty"`
	IOS      *IOSNotice      `json:"ios,omitempty"`
	WINPhone *WinPhoneNotice `json:"winphone,omitempty"`
}

func (*Notice) SetAlert

func (p *Notice) SetAlert(alert string)

func (*Notice) SetAndroidNotice

func (p *Notice) SetAndroidNotice(n *AndroidNotice)

func (*Notice) SetIOSNotice

func (p *Notice) SetIOSNotice(n *IOSNotice)

func (*Notice) SetWinPhoneNotice

func (p *Notice) SetWinPhoneNotice(n *WinPhoneNotice)

type Option

type Option struct {
	SendNo            int         `json:"sendno,omitempty"`
	TimeLive          int         `json:"time_to_live,omitempty"`
	ApnsProduction    bool        `json:"apns_production"`
	OverrideMsgId     int64       `json:"override_msg_id,omitempty"`
	BigPushDuration   int         `json:"big_push_duration,omitempty"`
	ThirdPartyChannel interface{} `json:"third_party_channel,omitempty"`
}

func (*Option) SetApns

func (this *Option) SetApns(apns bool)

func (*Option) SetBigPushDuration

func (this *Option) SetBigPushDuration(bigPushDuration int)

func (*Option) SetOverrideMsgId

func (this *Option) SetOverrideMsgId(id int64)

func (*Option) SetSendno

func (this *Option) SetSendno(no int)

func (*Option) SetTimelive

func (this *Option) SetTimelive(timelive int)

type PayLoad

type PayLoad struct {
	Platform        interface{} `json:"platform"`
	Audience        interface{} `json:"audience"`
	Notification    interface{} `json:"notification,omitempty"`
	Notification3rd interface{} `json:"notification_3rd,omitempty"`
	Message         interface{} `json:"message,omitempty"`
	Options         *Option     `json:"options,omitempty"`
}

func NewPushPayLoad

func NewPushPayLoad() *PayLoad

func (*PayLoad) Set3rdNotice

func (p *PayLoad) Set3rdNotice(notice *ThirdPartyNotice)

func (*PayLoad) SetAudience

func (p *PayLoad) SetAudience(ad *Audience)

func (*PayLoad) SetMessage

func (p *PayLoad) SetMessage(m *Message)

func (*PayLoad) SetNotice

func (p *PayLoad) SetNotice(notice *Notice)

func (*PayLoad) SetOptions

func (p *PayLoad) SetOptions(o *Option)

func (*PayLoad) SetPlatform

func (p *PayLoad) SetPlatform(pf *Platform)

func (*PayLoad) ToBytes

func (p *PayLoad) ToBytes() ([]byte, error)

type Platform

type Platform struct {
	Os interface{}
	// contains filtered or unexported fields
}

func (*Platform) Add

func (this *Platform) Add(os string) error

func (*Platform) AddAndrid

func (this *Platform) AddAndrid()

func (*Platform) AddIOS

func (this *Platform) AddIOS()

func (*Platform) AddWinphone

func (this *Platform) AddWinphone()

func (*Platform) All

func (this *Platform) All()

func (*Platform) Clear

func (this *Platform) Clear()

type PushClient

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

func NewPushClient

func NewPushClient(secret, appKey string) *PushClient

func (*PushClient) CreateSchedule

func (this *PushClient) CreateSchedule(data []byte) (string, error)

func (*PushClient) DeleteSchedule

func (this *PushClient) DeleteSchedule(id string) (string, error)

func (*PushClient) GetReport

func (this *PushClient) GetReport(msg_ids string) (string, error)

func (*PushClient) GetSchedule

func (this *PushClient) GetSchedule(id string) (string, error)

func (*PushClient) Send

func (this *PushClient) Send(data []byte) (string, error)

func (*PushClient) SendDeleteScheduleRequest

func (this *PushClient) SendDeleteScheduleRequest(schedule_id string, url string) (string, error)

func (*PushClient) SendGetReportRequest

func (this *PushClient) SendGetReportRequest(msg_ids string, url string) (string, error)

func (*PushClient) SendGetScheduleRequest

func (this *PushClient) SendGetScheduleRequest(schedule_id string, url string) (string, error)

func (*PushClient) SendPushBytes

func (this *PushClient) SendPushBytes(content []byte) (string, error)

func (*PushClient) SendPushString

func (this *PushClient) SendPushString(content string) (string, error)

func (*PushClient) SendScheduleBytes

func (this *PushClient) SendScheduleBytes(content []byte, url string) (string, error)

type Schedule

type Schedule struct {
	Cid     string                 `json:"cid"`
	Name    string                 `json:"name"`
	Enabled bool                   `json:"enabled"`
	Trigger map[string]interface{} `json:"trigger"`
	Push    *PayLoad               `json:"push"`
}

func NewSchedule

func NewSchedule(name, cid string, enabled bool, push *PayLoad) *Schedule

func (*Schedule) PeriodicalTrigger

func (s *Schedule) PeriodicalTrigger(start time.Time, end time.Time, time time.Time, timeUnit string, frequency int, point []string)

func (*Schedule) SingleTrigger

func (s *Schedule) SingleTrigger(t time.Time)

func (*Schedule) ToBytes

func (this *Schedule) ToBytes() ([]byte, error)

type ThirdPartyNotice

type ThirdPartyNotice struct {
	Title       string                 `json:"title,omitempty"`
	Content     string                 `json:"content"`
	UriActivity string                 `json:"uri_activity,omitempty"`
	UriAction   string                 `json:"uri_action,omitempty"`
	Style       int                    `json:"style,omitempty"`
	Inbox       map[string]interface{} `json:"inbox,omitempty"`
	Extras      map[string]interface{} `json:"extras,omitempty"`
}

type WinPhoneNotice

type WinPhoneNotice struct {
	Alert    string                 `json:"alert"`
	Title    string                 `json:"title,omitempty"`
	OpenPage string                 `json:"_open_page,omitempty"`
	Extras   map[string]interface{} `json:"extras,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL