umeng

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 12 Imported by: 0

README

umeng

umeng 提供友盟推送的生产级封装,聚焦配置校验、显式 context、请求清洗、签名和统一错误边界。

设计原则

  • 初始化阶段校验 app_keymaster_secret 和 endpoint。
  • 发送接口统一要求非 nil context.Context
  • 发送前会克隆并清洗请求,避免修改调用方对象。
  • 保留底层 HTTP client 能力,便于接入自定义 transport、trace 和测试桩。

快速开始

client, err := umeng.New(&umeng.Config{
    AppKey:       "app-key",
    MasterSecret: "master-secret",
    AliasType:    "subject_id",
    Production:   true,
})
if err != nil {
    panic(err)
}

_, err = client.Send(context.Background(), &umeng.SendRequest{
    Platform: PlatformAndroid,
    Aliases:  []string{"user-subject-id"},
    Title:    "订单提醒",
    Body:     "订单已送达,请及时查收",
    Extra: map[string]string{
        "link": "app://order/detail?id=1",
    },
})
if err != nil {
    panic(err)
}

Documentation

Index

Constants

View Source
const DefaultEndpoint = "https://msgapi.umeng.com"

Variables

View Source
var (
	ErrNilConfig             = errors.New("umeng: config is required")
	ErrContextRequired       = errors.New("umeng: context is required")
	ErrRequestRequired       = errors.New("umeng: request is required")
	ErrAppKeyRequired        = errors.New("umeng: app key is required")
	ErrMasterSecretRequired  = errors.New("umeng: master secret is required")
	ErrInvalidEndpoint       = errors.New("umeng: endpoint is invalid")
	ErrPlatformRequired      = errors.New("umeng: platform is required")
	ErrNotificationBodyEmpty = errors.New("umeng: notification body is required")
	ErrAndroidTitleRequired  = errors.New("umeng: android notification title is required")
	ErrAliasTypeRequired     = errors.New("umeng: alias type is required when aliases are provided")
	ErrTargetRequired        = errors.New("umeng: exactly one target is required")
	ErrURLRequired           = errors.New("umeng: url is required when after_open is go_url")
	ErrActivityRequired      = errors.New("umeng: activity is required when after_open is go_activity")
	ErrCustomRequired        = errors.New("umeng: custom payload is required when after_open is go_custom")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Code       string
	Message    string
}

func (*APIError) Error

func (e *APIError) Error() string

type AfterOpen

type AfterOpen string
const (
	AfterOpenGoApp      AfterOpen = "go_app"
	AfterOpenGoURL      AfterOpen = "go_url"
	AfterOpenGoActivity AfterOpen = "go_activity"
	AfterOpenGoCustom   AfterOpen = "go_custom"
)

type Client

type Client interface {
	Raw() *http.Client
	Send(context.Context, *SendRequest) (*SendResponse, error)
}

func New

func New(cfg *Config) (Client, error)

func Open

func Open(cfg *Config) (Client, error)

type Config

type Config struct {
	AppKey       string
	MasterSecret string
	Endpoint     string
	AliasType    string
	Production   bool
	UserAgent    string
	HTTPClient   *http.Client
	// contains filtered or unexported fields
}

type Platform

type Platform string
const (
	PlatformAndroid Platform = "android"
	PlatformIOS     Platform = "ios"
)

type SendRequest

type SendRequest struct {
	Platform     Platform
	Broadcast    bool
	Aliases      []string
	AliasType    string
	DeviceTokens []string
	Title        string
	Body         string
	Ticker       string
	Description  string
	AfterOpen    AfterOpen
	URL          string
	Activity     string
	Custom       string
	Extra        map[string]string
	Badge        *int
	Sound        string
	ExpireAfter  time.Duration
}

type SendResponse

type SendResponse struct {
	Ret  string           `json:"ret"`
	Data SendResponseData `json:"data"`
}

type SendResponseData

type SendResponseData struct {
	MsgID     string `json:"msg_id,omitempty"`
	TaskID    string `json:"task_id,omitempty"`
	ErrorCode string `json:"error_code,omitempty"`
	ErrorMsg  string `json:"error_msg,omitempty"`
}

Jump to

Keyboard shortcuts

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