jpush

package module
v0.0.0-...-ed6c091 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2019 License: MIT Imports: 15 Imported by: 0

README

文档查看

JPush Golang Client

Build Codecov ReportCard GoDoc License

快速开始

下载安装
$ go get -u -v github.com/JeffreyBool/jpush-go
创建文件 push.go
package main

import (
	"context"
	"fmt"

	"github.com/JeffreyBool/jpush-go"
)

func main() {
	jpush.Init(2,
		jpush.SetAppKey("b1ccd0dd04ec36b66c75e99f"),
		jpush.SetMasterSecret("ed431429270144d3ed53555b"),
	)

	defer jpush.Terminate()

	payload := &jpush.Payload{
		Platform:     jpush.NewPlatform().All(),
		Audience:     jpush.NewAudience().All(),
		Notification: jpush.NewNotification().SetAlert("通知测试"),
		Options:      jpush.NewOptions().SetSendNO(1),
	}
	err := jpush.Push(context.Background(), payload, func(result *jpush.PushResult, err error) {
		if err != nil {
			panic(err)
		}
		fmt.Println(result.String())
	})
	if err != nil {
		panic(err)
	}
}

编译运行
$ go build push.go
$ ./push

输出结果

{"sendno":"1","msg_id":"3866336947"}

特性

  • 支持异步推送队列
  • 自动处理频率限制
  • 自动维护cid池

MIT License

Copyright (c) 2018 JeffreyBool

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCID 无效的cid
	ErrInvalidCID = errors.New("invalid cid")
)

Functions

func GetPushID

func GetPushID(ctx context.Context) (string, error)

GetPushID 获取推送ID

func GetScheduleID

func GetScheduleID(ctx context.Context) (string, error)

GetScheduleID 获取推送ID

func Push

func Push(ctx context.Context, payload *Payload, callback PushResultHandle) error

Push 消息推送

func PushValidate

func PushValidate(ctx context.Context, payload *Payload, callback PushResultHandle) error

PushValidate 先校验,再推送

func Terminate

func Terminate()

Terminate 终止客户端

Types

type AndroidNotification

type AndroidNotification struct {
	Alert       string                 `json:"alert"`
	Title       string                 `json:"title,omitempty"`
	BuilderID   int                    `json:"builder_id,omitempty"`
	Priority    int                    `json:"priority,omitempty"`
	Category    string                 `json:"category,omitempty"`
	Style       int                    `json:"style,omitempty"`
	AlertType   int                    `json:"alert_type,omitempty"`
	BigText     string                 `json:"big_text,omitempty"`
	Inbox       map[string]interface{} `json:"inbox,omitempty"`
	BigPicPath  string                 `json:"big_pic_path,omitempty"`
	BadgeAddNum int                    `json:"badge_add_num,omitempty"`
	Extras      map[string]interface{} `json:"extras,omitempty"`
}

AndroidNotification Android 平台上的通知

func NewAndroidNotification

func NewAndroidNotification() *AndroidNotification

NewAndroidNotification 创建 Android 平台上的通知实例

func (*AndroidNotification) SetAlert

func (n *AndroidNotification) SetAlert(alert string) *AndroidNotification

SetAlert 通知内容

func (*AndroidNotification) SetAlertType

func (n *AndroidNotification) SetAlertType(alertType int) *AndroidNotification

SetAlertType 通知提醒方式

func (*AndroidNotification) SetBadgeAddNum

func (n *AndroidNotification) SetBadgeAddNum(badgeAddNum int) *AndroidNotification

SetBadgeAddNum 角标数字,取值范围1-99

func (*AndroidNotification) SetBigPicPath

func (n *AndroidNotification) SetBigPicPath(bigPicPath string) *AndroidNotification

SetBigPicPath 大图片通知栏样式

func (*AndroidNotification) SetBigText

func (n *AndroidNotification) SetBigText(bigText string) *AndroidNotification

SetBigText 大文本通知栏样式

func (*AndroidNotification) SetBuilderID

func (n *AndroidNotification) SetBuilderID(builderID int) *AndroidNotification

SetBuilderID 通知栏样式 ID

func (*AndroidNotification) SetCategory

func (n *AndroidNotification) SetCategory(category string) *AndroidNotification

SetCategory 通知栏条目过滤或排序

func (*AndroidNotification) SetExtras

func (n *AndroidNotification) SetExtras(extras map[string]interface{}) *AndroidNotification

SetExtras 扩展字段

func (*AndroidNotification) SetInbox

func (n *AndroidNotification) SetInbox(inbox map[string]interface{}) *AndroidNotification

SetInbox 文本条目通知栏样式

func (*AndroidNotification) SetPriority

func (n *AndroidNotification) SetPriority(priority int) *AndroidNotification

SetPriority 通知栏展示优先级

func (*AndroidNotification) SetStyle

func (n *AndroidNotification) SetStyle(style int) *AndroidNotification

SetStyle 通知栏样式类型

func (*AndroidNotification) SetTitle

func (n *AndroidNotification) SetTitle(title string) *AndroidNotification

SetTitle 通知标题

type Audience

type Audience struct {
	IsAll bool
	Value map[string][]string
}

Audience 推送目标

func NewAudience

func NewAudience() *Audience

NewAudience 创建推送目标实例

func (*Audience) All

func (a *Audience) All() *Audience

All 全部设备

func (*Audience) MarshalJSON

func (a *Audience) MarshalJSON() ([]byte, error)

MarshalJSON 实现 JSON 接口

func (*Audience) SetAbTest

func (a *Audience) SetAbTest(abtests ...string) *Audience

SetAbTest 设定A/B Test ID

func (*Audience) SetAlias

func (a *Audience) SetAlias(aliases ...string) *Audience

SetAlias 设定别名

func (*Audience) SetRegistrationID

func (a *Audience) SetRegistrationID(registrationIDs ...string) *Audience

SetRegistrationID 设定注册 ID

func (*Audience) SetSegment

func (a *Audience) SetSegment(segments ...string) *Audience

SetSegment 设定用户分群 ID

func (*Audience) SetTag

func (a *Audience) SetTag(tags ...string) *Audience

SetTag 设定标签 OR

func (*Audience) SetTagAnd

func (a *Audience) SetTagAnd(tags ...string) *Audience

SetTagAnd 设定标签 AND

func (*Audience) SetTagNot

func (a *Audience) SetTagNot(tags ...string) *Audience

SetTagNot 设定标签 NOT

func (*Audience) SetValue

func (a *Audience) SetValue(key string, values ...string) *Audience

SetValue 设定推送目标

type CIDClient

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

CIDClient 推送唯一标识符客户端

func NewCIDClient

func NewCIDClient(count int, opts ...Option) *CIDClient

NewCIDClient 创建获取CID实例

func (*CIDClient) GetPushID

func (c *CIDClient) GetPushID(ctx context.Context) (string, error)

GetPushID 获取推送ID

func (*CIDClient) GetScheduleID

func (c *CIDClient) GetScheduleID(ctx context.Context) (string, error)

GetScheduleID 获取定时ID

type Client

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

Client 推送客户端

func Init

func Init(maxThread int, opts ...Option) *Client

Init 初始化推送客户端

func NewClient

func NewClient(maxThread int, opts ...Option) *Client

NewClient 创建推送客户端实例

func (*Client) GetPushID

func (c *Client) GetPushID(ctx context.Context) (string, error)

GetPushID 获取推送ID

func (*Client) GetScheduleID

func (c *Client) GetScheduleID(ctx context.Context) (string, error)

GetScheduleID 获取定时ID

func (*Client) Push

func (c *Client) Push(ctx context.Context, payload *Payload, callback PushResultHandle) error

Push 消息推送

func (*Client) PushValidate

func (c *Client) PushValidate(ctx context.Context, payload *Payload, callback PushResultHandle) error

PushValidate 先校验,再推送

func (*Client) Terminate

func (c *Client) Terminate()

Terminate 终止客户端

type Error

type Error struct {
	StatusCode int         `json:"status_code"`
	ErrorItem  *ErrorItem  `json:"error,omitempty"`
	HeaderItem *HeaderItem `json:"header,omitempty"`
}

Error 错误

func (*Error) Error

func (e *Error) Error() string

type ErrorItem

type ErrorItem struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

ErrorItem 错误项

func NewErrorItem

func NewErrorItem(code int, message string) *ErrorItem

NewErrorItem 创建错误项实例

type HeaderItem

type HeaderItem struct {
	XRateLimitQuota     int `json:"X-Rate-Limit-Quota"`
	XRateLimitRemaining int `json:"X-Rate-Limit-Remaining"`
	XRateLimitReset     int `json:"X-Rate-Limit-Reset"`
}

HeaderItem 响应头

type IOSNotification

type IOSNotification struct {
	Alert            interface{}            `json:"alert"`
	Sound            string                 `json:"sound,omitempty"`
	Badge            interface{}            `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"`
}

IOSNotification iOS 平台上 APNs 通知结构

func NewIOSNotification

func NewIOSNotification() *IOSNotification

NewIOSNotification 创建 iOS 平台上的通知实例

func (*IOSNotification) SetAlert

func (n *IOSNotification) SetAlert(alert interface{}) *IOSNotification

SetAlert 通知内容

func (*IOSNotification) SetBadge

func (n *IOSNotification) SetBadge(badge interface{}) *IOSNotification

SetBadge 应用角标

func (*IOSNotification) SetCategory

func (n *IOSNotification) SetCategory(category string) *IOSNotification

SetCategory 通知栏条目过滤或排序

func (*IOSNotification) SetContentAvailable

func (n *IOSNotification) SetContentAvailable(contentAvailable bool) *IOSNotification

SetContentAvailable 推送唤醒

func (*IOSNotification) SetExtras

func (n *IOSNotification) SetExtras(extras map[string]interface{}) *IOSNotification

SetExtras 扩展字段

func (*IOSNotification) SetMutableContent

func (n *IOSNotification) SetMutableContent(mutableContent bool) *IOSNotification

SetMutableContent 通知扩展

func (*IOSNotification) SetSound

func (n *IOSNotification) SetSound(sound string) *IOSNotification

SetSound 通知提示声音

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"`
}

Message 自定义消息

func NewMessage

func NewMessage() *Message

NewMessage 创建自定义消息实例

func (*Message) SetContent

func (m *Message) SetContent(content string) *Message

SetContent 消息内容本身

func (*Message) SetContentType

func (m *Message) SetContentType(contentType string) *Message

SetContentType 消息内容类型

func (*Message) SetExtras

func (m *Message) SetExtras(extras map[string]interface{}) *Message

SetExtras JSON 格式的可选参数

func (*Message) SetTitle

func (m *Message) SetTitle(title string) *Message

SetTitle 消息标题

type Notification

type Notification struct {
	Alert    string                `json:"alert,omitempty"`
	Android  *AndroidNotification  `json:"android,omitempty"`
	IOS      *IOSNotification      `json:"ios,omitempty"`
	WinPhone *WinPhoneNotification `json:"winphone,omitempty"`
}

Notification 通知

func NewNotification

func NewNotification() *Notification

NewNotification 创建通知实例

func (*Notification) SetAlert

func (n *Notification) SetAlert(alert string) *Notification

SetAlert 设定通知内容

func (*Notification) SetAndroidNotification

func (n *Notification) SetAndroidNotification(android *AndroidNotification) *Notification

SetAndroidNotification 设定 Android 平台上的通知

func (*Notification) SetIOSNotification

func (n *Notification) SetIOSNotification(ios *IOSNotification) *Notification

SetIOSNotification 设定 iOS 平台上的通知

func (*Notification) SetWinPhoneNotification

func (n *Notification) SetWinPhoneNotification(winPhone *WinPhoneNotification) *Notification

SetWinPhoneNotification 设定 Windows Phone 平台上的通知

type OS

type OS string

OS 推送平台

const (
	Android  OS = "android"
	IOS      OS = "ios"
	WinPhone OS = "winphone"
)

定义推送平台

func (OS) String

func (o OS) String() string

type Option

type Option func(o *options)

Option 配置项

func SetAppKey

func SetAppKey(appKey string) Option

SetAppKey 设定 Appkey

func SetCIDCount

func SetCIDCount(count int) Option

SetCIDCount 设定每次获取CID的数量

func SetHost

func SetHost(host string) Option

SetHost 设定请求地址

func SetMasterSecret

func SetMasterSecret(masterSecret string) Option

SetMasterSecret 设定 MasterSecret

type Options

type Options struct {
	SendNO          int    `json:"sendno,omitempty"`
	TimeLive        int    `json:"time_to_live,omitempty"`
	OverrideMsgID   int64  `json:"override_msg_id,omitempty"`
	ApnsProduction  bool   `json:"apns_production"`
	ApnsCollapseID  string `json:"apns_collapse_id,omitempty"`
	BigPushDuration int    `json:"big_push_duration,omitempty"`
}

Options 可选参数

func NewOptions

func NewOptions() *Options

NewOptions 创建可选参数实例

func (*Options) SetApnsCollapseID

func (o *Options) SetApnsCollapseID(apnsCollapseID string) *Options

SetApnsCollapseID 要覆盖的消息 ID

func (*Options) SetApnsProduction

func (o *Options) SetApnsProduction(prod bool) *Options

SetApnsProduction 设定 APNs 是否生产环境

func (*Options) SetBigPushDuration

func (o *Options) SetBigPushDuration(bigPushDuration int) *Options

SetBigPushDuration 定速推送时长(分钟)

func (*Options) SetOverrideMsgID

func (o *Options) SetOverrideMsgID(overrideMsgID int64) *Options

SetOverrideMsgID 要覆盖的消息 ID

func (*Options) SetSendNO

func (o *Options) SetSendNO(sendNO int) *Options

SetSendNO 推送序号

func (*Options) SetTimeLive

func (o *Options) SetTimeLive(timeLive int) *Options

SetTimeLive 离线消息保留时长(秒)

type Payload

type Payload struct {
	Platform     *Platform     `json:"platform"`               // 推送平台
	Audience     *Audience     `json:"audience"`               // 推送目标
	Notification *Notification `json:"notification,omitempty"` // 通知
	Message      *Message      `json:"message,omitempty"`      // 自定义消息
	SmsMessage   *SmsMessage   `json:"sms_message,omitempty"`  // 短信补充
	Options      *Options      `json:"options,omitempty"`      // 可选参数
	CID          string        `json:"cid,omitempty"`          // 推送唯一标识符
}

Payload 推送载荷

func (*Payload) Reader

func (p *Payload) Reader() io.Reader

Reader 序列化为 JSON 流

func (*Payload) String

func (p *Payload) String() string

type Platform

type Platform struct {
	IsAll bool
	Value []string
}

Platform 推送平台

func NewPlatform

func NewPlatform() *Platform

NewPlatform 创建推送平台实例

func (*Platform) Add

func (p *Platform) Add(oss ...OS) *Platform

Add 指定特定推送平台

func (*Platform) All

func (p *Platform) All() *Platform

All 推送到所有平台

func (*Platform) MarshalJSON

func (p *Platform) MarshalJSON() ([]byte, error)

MarshalJSON 实现 JSON 接口

type PushResult

type PushResult struct {
	SendNO string `json:"sendno"`
	MsgID  string `json:"msg_id"`
}

PushResult 推送响应结果

func (*PushResult) String

func (r *PushResult) String() string

type PushResultHandle

type PushResultHandle func(context.Context, *PushResult, error)

PushResultHandle 异步响应结果

type SmsMessage

type SmsMessage struct {
	TempPara  interface{} `json:"temp_para,omitempty"`
	TempID    int64       `json:"temp_id"`
	DelayTime int         `json:"delay_time"`
}

SmsMessage 短信补充

func NewSmsMessage

func NewSmsMessage() *SmsMessage

NewSmsMessage 创建短信补充实例

func (*SmsMessage) SetDelayTime

func (m *SmsMessage) SetDelayTime(delayTime int) *SmsMessage

SetDelayTime 单位为秒,不能超过 24 小时。设置为 0,表示立即发送短信。该参数仅对 android 和 iOS 平台有效,Winphone 平台则会立即发送短信。

func (*SmsMessage) SetTempID

func (m *SmsMessage) SetTempID(tempID int64) *SmsMessage

SetTempID 短信补充的内容模板 ID。没有填写该字段即表示不使用短信补充功能

func (*SmsMessage) SetTempPara

func (m *SmsMessage) SetTempPara(tempPara interface{}) *SmsMessage

SetTempPara 短信模板中的参数

type WinPhoneNotification

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

WinPhoneNotification Windows Phone 平台上的通知

func NewWinPhoneNotification

func NewWinPhoneNotification() *WinPhoneNotification

NewWinPhoneNotification 创建 Windows Phone 平台上的通知实例

func (*WinPhoneNotification) SetAlert

func (n *WinPhoneNotification) SetAlert(alert string) *WinPhoneNotification

SetAlert 通知内容

func (*WinPhoneNotification) SetExtras

func (n *WinPhoneNotification) SetExtras(extras map[string]interface{}) *WinPhoneNotification

SetExtras 扩展字段

func (*WinPhoneNotification) SetOpenPage

func (n *WinPhoneNotification) SetOpenPage(openPage string) *WinPhoneNotification

SetOpenPage 点击打开的页面名称

func (*WinPhoneNotification) SetTitle

func (n *WinPhoneNotification) SetTitle(title string) *WinPhoneNotification

SetTitle 通知标题

Jump to

Keyboard shortcuts

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