Code Monkey home page Code Monkey logo

jpush-api-go-client's Introduction

JPush API Go Client

GoDoc Build Status

概述

这是 JPush REST API 的 Golang 版本封装开发包,非官方实现,只支持 v3 版本。

官方 REST API 文档: http://docs.jpush.cn/display/dev/REST+API

安装

使用 go get 安装,无任何第三方依赖:

go get github.com/DeanThompson/jpush-api-go-client

使用方法

1. 创建 JPushClient

import "github.com/DeanThompson/jpush-api-go-client"

const (
    appKey = ""
    masterSecret = ""
)

jclient := jpush.NewJPushClient(appKey, masterSecret)

2. 逐步构建消息体

与推送有关的数据结构都在 push 包里

import "github.com/DeanThompson/jpush-api-go-client/push"

2.1 创建 Platform 对象

platform := push.NewPlatform()

// 用 Add() 方法添加具体平台参数,可选: "all", "ios", "android"
platform.Add("ios", "android")

// 或者用 All() 方法设置所有平台
// platform.All()

2.2 创建 Audience 对象

audience := push.NewAudience()
audience.SetTag([]string{"广州", "深圳"})   // 设置 tag
// audience.SetTagAnd([]string{"北京", "女"}) // 设置 tag_and
// audience.SetAlias([]string{"alias1", "alias2"})  // 设置 alias
// audience.SetRegistrationId([]string{"id1", "id2"})   // 设置 registration_id
// audience.All()   和 platform 一样,可以调用 All() 方法设置所有受众

2.3 创建 Notification 对象

2.3.1 创建 AndroidNotification 对象

// android 平台专有的 notification,用 alert 属性初始化
androidNotification := push.NewAndroidNotification("Android Notification Alert")
androidNotification.Title = "title"
// androidNotification.BuilderId = 10086
androidNotification.AddExtra("key", "value")

2.3.2 创建 IosNotification 对象

// iOS 平台专有的 notification,用 alert 属性初始化
iosNotification := push.NewIosNotification("iOS Notification Alert")
// iosNotification.Sound = "/paht/to/sound"
iosNotification.Badge = 1   // 只支持 int 类型的 badge
// iosNotification.ContentAvailable = true
// iosNotification.Category = "category_name"
// iosNotification.AddExtra("key", "value")

// Validate 方法可以验证 iOS notification 是否合法
// 一般情况下,开发者不需要直接调用此方法,这个方法会在构造 PushObject 时自动调用
// iosNotification.Validate()

2.3.3 创建 WinphoneNotification 对象

// Windows Phone 平台专有的 notification,用 alert 属性初始化
wpNotification := push.NewWinphoneNotification("Winphone Notification Alert")
// wpNotification.Title = "Winphone Notification Title"
// wpNotification.OpenPage = "some page"

// 所有平台的专有 notification 都有 AddExtra 方法,用于添加 extra 信息
wpNotification.AddExtra("extra_key", "extra_value")

2.3.4 创建 Notification 对象

AndroidNotification, IosNotification, WinphoneNotification 三个分别是三种平台专有的 notification。

Notification 是极光推送的“通知”,包含一个 alert 属性,和可选的三个平台属性。

// notification 对象,表示 通知,传递 alert 属性初始化
notification := push.NewNotification("Notification Alert")
notification.Android = androidNotification
notification.Ios = iosNotification
notification.Winphone = wpNotification

2.4 创建 Message 对象

Message 是极光推送的“消息”,也叫透传消息

// message 对象,表示 透传消息,用 content 属性初始化
message := push.NewMessage("Message Content must not be empty")
// message.Title = "Message Title"
// message.ContentType = "application/json"

// 可以调用 AddExtra 方法,添加额外信息
// message.AddExtra("key", 123)

2.5 创建 Options 对象

// option 对象,表示推送可选项
options := push.NewOptions()
// options.SendNo = 12345
// options.OverrideMsgId = 123333333

// Options 的 Validate 方法会对 time_to_live 属性做范围限制,以满足 JPush 的规范
options.TimeToLive = 10000000

// iOS 平台,是否推送生产环境,false 表示开发环境;如果不指定,就是生产环境
options.ApnsProduction = true

// Options 的 Validate 方法会对 big_push_duration 属性做范围限制,以满足 JPush 的规范
options.BigPushDuration = 1500

// Options 对象有 Validate 方法,但实际上这里并不会返回错误,
// 而是对 time_to_live 和 big_push_duration 两个值做了范围限制
// 开发者不需要手动调用此方法,在构建 PushObject 时会自动调用
// err := options.Validate()

2.6 创建 PushObject 对象

payload := push.NewPushObject()
payload.Platform = platform
payload.Audience = audience
payload.Notification = notification
payload.Message = message
payload.Options = options

// 打印查看 json 序列化的结果,也就是 POST 请求的 body
// data, err := json.Marshal(payload)
// if err != nil {
//    fmt.Println("json.Marshal PushObject failed:", err)
// } else {
//    fmt.Println("payload:", string(data), "\n")
// }

3. 推送/推送验证

// result, err := jclient.Push(payload)
result, err := jclient.PushValidate(payload)
if err != nil {
    fmt.Print("PushValidate failed:", err)
} else {
    fmt.Println("PushValidate result:", result)
}

4. 更多示例

更多例子可以看这里:jpush_test.go

jpush-api-go-client's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.