xmpush

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

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 8 Imported by: 0

README

xmpush

小米推送SDK

Software License GoDoc

由于官方没有推出Go版本的推送SDK,故自己实现了。支持:

  • 按cid单推
  • 按cid群推
  • 全推

使用

安装:

go get https://github.com/geek-go/xmpush

SDK 测试(使用前先打开xmpush_test.go配置appid等参数):

# 测试单推
go test -v  -run="^TestXmPush_SendByCid$" 

# 测试群推
go test -v  -run="^TestXmPush_SendByCids$"

# 测试全推
go test -v  -run="^TestXmPush_SendAll$"  

其它例子参照 xmpush_test.go 的调用。

测试用例里针对SDK进行了一些封装,大家可以参考快速实现。

文档参考

https://pkg.go.dev/github.com/geek-go/xmpush

如何参与该项目

如果需要增加个推其它接口的实现,请参考api_开头的文件实现。规范:

  • 每个接口对应一个文件
  • 每个文件均包含接口请求结构体、接口响应结构体、接口调用的实现
  • 增加测试用例
  • 注意避免过度封装,以免使用者困惑

Documentation

Index

Constants

View Source
const (
	SandboxHost    = "https://sandbox.xmpush.xiaomi.com"
	ProductionHost = "https://api.xmpush.xiaomi.com"

	MessageRegIdURL   = "/v3/message/regid"
	MessageAliasURL   = "/v3/message/alias"
	MessageAllURL     = "/v3/message/all"
	MessageAccountURL = "/v2/message/user_account"
	MessageTopicURL   = "/v3/message/topic"
	MessageTopicOpURL = "/v3/message/multi_topic"

	MultiRegIdURL   = "/v2/multi_messages/regids"
	MultiAliasURL   = "/v2/multi_messages/aliases"
	MultiAccountURL = "/v2/multi_messages/user_accounts"

	StatsURL          = "/v1/stats/message/counters"
	MessageStatusURL  = "/v1/trace/message/status"
	MessagesStatusURL = "/v1/trace/messages/status"

	SubscribeURL   = "/v2/topic/subscribe"
	UnsubscribeURL = "/v2/topic/unsubscribe"

	SubscribeAliasURL   = "/v2/topic/subscribe/alias"
	UnsubscribeAliasURL = "/v2/topic/unsubscribe/alias"

	InvalidRegIdsURL = "https://feedback.xmpush.xiaomi.com/v1/feedback/fetch_invalid_regids"

	RegIdAliasURL = "/v1/alias/all"
	RegIdTopicURL = "/v1/topic/all"

	ScheduleJobExistURL  = "/v2/schedule_job/exist"
	ScheduleJobDeleteURL = "/v2/schedule_job/delete"
)

Variables

This section is empty.

Functions

func DoGet

func DoGet(uri string, appSecret string, form *url.Values) ([]byte, error)

func DoPost

func DoPost(uri string, appSecret string, form *url.Values) ([]byte, error)

Types

type CommonParam

type CommonParam struct {
	RegistrationId string `json:"registration_id"`
	Alias          string `json:"alias"`
	UserAccount    string `json:"user_account"`
	Topic          string `json:"topic"`
	Topics         string `json:"topics"`
	TopicOp        string `json:"topic_op"`
}

基础参数

type Extra

type Extra struct {
	SoundUri          string `json:"sound_uri"`
	Ticker            string `json:"ticker"`
	NotifyForeground  string `json:"notify_foreground"`
	NotifyEffect      string `json:"notify_effect"`
	IntentUri         string `json:"intent_uri"`
	FlowControl       string `json:"flow_control"`
	AppVersion_not_in string `json:"app_version_not_in"`
	Badge             string `json:"badge"`    //ios使用
	Category          string `json:"category"` //ios使用
}

type Message

type Message struct {
	CommonParam
	Payload               string            `json:"payload,omitempty"`
	RestrictedPackageName string            `json:"restricted_package_name,omitempty"`
	Title                 string            `json:"title"`
	Description           string            `json:"description"`
	PassThrough           int32             `json:"pass_through"`          // 0 通知栏消息, 1 透传消息
	NotifyType            int32             `json:"notify_type,omitempty"` // -1: DEFAULT_ALL 1: 使用默认提示音提示, 2: 使用默认震动提示, 4: 使用默认led灯光提示
	TimeToLive            int64             `json:"time_to_live,omitempty"`
	TimeToSend            int64             `json:"time_to_send,omitempty"`
	NotifyID              int64             `json:"notify_id,omitempty"`
	Extra                 map[string]string `json:"extra,omitempty"`
}

发送推送需要的参数

type Result

type Result struct {
	Code        int64       `json:"code"`                  //0表示成功,非0表示失败
	Result      string      `json:"result"`                //"ok" 表示成功,"error" 表示失败
	Description string      `json:"description,omitempty"` //对发送消息失败原因的解释
	Info        string      `json:"info,omitempty"`        //详细信息
	Reason      string      `json:"reason,omitempty"`      //失败原因
	Data        *ResultData `json:"data,omitempty"`        //本身就是一个json字符串
}

小米推送接口返回的结果,主体结构是一致的

func SendMessageAll

func SendMessageAll(appSecret string, message *Message) (*Result, error)

给所有人发送消息

func SendMessageByRegAliasIds

func SendMessageByRegAliasIds(appSecret string, message *Message) (*Result, error)

通过Alias群推

func SendMessageByRegIds

func SendMessageByRegIds(appSecret string, message *Message) (*Result, error)

通过RegId群推

type ResultData

type ResultData struct {
	BadRegids string `json:"bad_regids"` //推送失败的cids
	Id        string `json:"id"`         //消息的Id
}

Jump to

Keyboard shortcuts

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