huawei

package module
v0.0.0-...-7d71a48 Latest Latest
Warning

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

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

README

image# hwpush 华为push

Software License GoDoc

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

  • 按cid单推
  • 按cid群推

全推不支持,官方接口没有提供全推。

使用

安装:

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

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

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

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

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

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

文档参考

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

如何参与该项目

如果需要增加个推其它接口的实现,请参考push_send实现。规范:

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

Documentation

Index

Constants

View Source
const (
	TOKEN_URL = "https://login.cloud.huawei.com/oauth2/v2/token"
	PUSH_URL  = "https://api.push.hicloud.com/pushsend.do"
)
View Source
const (
	NSP_STATUS_6   = 6
	NSP_STATUS_102 = 102
	NSP_STATUS_105 = 105
	NSP_STATUS_111 = 111
	NSP_STATUS_112 = 112
	NSP_STATUS_113 = 113
	NSP_STATUS_114 = 114
	NSP_STATUS_199 = 199
	NSP_STATUS_403 = 403
)

Variables

View Source
var NSP_STATUS_MSG = map[int]string{
	NSP_STATUS_6:   "session过期",
	NSP_STATUS_102: "无效的SESSION_KEY",
	NSP_STATUS_105: "参数错误",
	NSP_STATUS_111: "系统、服务处理忙",
	NSP_STATUS_112: "找不到对应服务",
	NSP_STATUS_113: "请求服务失败",
	NSP_STATUS_114: "服务不可达、无路由",
	NSP_STATUS_199: "未知错误",
	NSP_STATUS_403: "无权限",
}

Functions

func GetToken

func GetToken(client_id string, client_secret string) string

获取auth_token

Types

type Action

type Action struct {
	Type  int   `json:"type,omitempty"` //1 自定义行为:行为由参数intent定义;2 打开URL:URL地址由参数url定义;3 打开APP:默认值,打开App的首页。注意:富媒体消息开放API不支持。
	Param Param `json:"param,omitempty"`
}

消息点击动作

type BizErr

type BizErr struct {
	Errno  int    `json:"errno"`
	Errmsg string `json:"errmsg"`
}

对方业务异常

func NewBizErr

func NewBizErr(errno int, errmsg string) *BizErr

func SendFormPost

func SendFormPost(url string, data url.Values) ([]byte, error, *BizErr)

post请求

type Body

type Body struct {
	Content string `json:"content"` //消息内容体
	Title   string `json:"title"`   //消息标题
}

消息内容。注意:对于透传类的消息可以是字符串,不必是JSON Object。

type Ext

type Ext struct {
	BadgeAddNum string   `json:"badgeAddNum,omitempty"` //设置应用角标数值,取值范围1-99。
	BadgeClass  string   `json:"badgeClass,omitempty"`  //桌面图标对应的应用入口Activity类。
	BiTag       string   `json:"biTag,omitempty"`       //设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态。
	Customize   []string `json:"customize,omitempty"`   //用于触发onEvent点击事件,扩展样例:[{"season":"Spring"},{"weather":"raining"}] 。说明:这个字段类型必须是JSON Array,里面是key-value的一组扩展信息。
}

扩展信息,含BI消息统计,特定展示风格,消息折叠

type Hps

type Hps struct {
	Msg Msg `json:"msg"`
	Ext Ext `json:"ext,omitempty"`
}

华为Push消息总结构体

type Message

type Message struct {
	Hps Hps `json:"hps"`
}

type Msg

type Msg struct {
	Type   int    `json:"type"` //1, 透传异步消息; 3, 系统通知栏异步消息。注意:2和4以后为保留后续扩展使用
	Body   Body   `json:"body"`
	Action Action `json:"action,omitempty"`
}

PUSH消息定义。包括:消息类型type、消息内容body、消息动作action

type Param

type Param struct {
	Intent     string `json:"intent,omitempty"` //Action的type为1的时候表示自定义行为。
	Url        string `json:"url,omitempty"`    //Action的type为2的时候表示打开URL地址
	AppPkgName string `json:"appPkgName"`       //需要拉起的应用包名,必须和注册推送的包名一致。
}

关于消息点击动作的参数

type PushSendParam

type PushSendParam struct {
	DeviceToken string `json:"device_token"` //JSON数值字符串,单次最多只是100个。
	Payload     string `json:"payload"`      //描述投递消息的JSON结构体,描述PUSH消息的:类型、内容、显示、点击动作、报表统计和扩展信息
	NspCtx      string `json:"nsp_ctx"`
}

请求参数

type PushSendResult

type PushSendResult struct {
	Code      string `json:"code"`
	Msg       string `json:"msg"`
	RequestId string `json:"requestId"`
	Ext       string `json:"ext"`
}

type TokenResultStruct

type TokenResultStruct struct {
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
	TokenType   string `json:"token_type"`
}

type Vers

type Vers struct {
	Ver   string `json:"ver"`
	AppID string `json:"appId"`
}

Jump to

Keyboard shortcuts

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