Giter Club home page Giter Club logo

jpush-api-go-client's Introduction

jpush-api-go-client

概述

这是JPush REST API 的 go 版本封装开发包,仅支持最新的REST API v3功能。 REST API 文档:http://docs.jpush.cn/display/dev/Push-API-v3

使用

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

推送流程

1.构建要推送的平台: jpushclient.Platform

//Platform
var pf jpushclient.Platform
pf.Add(jpushclient.ANDROID)
pf.Add(jpushclient.IOS)
pf.Add(jpushclient.WINPHONE)
//pf.All()

2.构建接收听众: jpushclient.Audience

//Audience
var ad jpushclient.Audience
s := []string{"t1", "t2", "t3"}
ad.SetTag(s)
id := []string{"1", "2", "3"}
ad.SetID(id)
//ad.All()

3.构建通知 jpushclient.Notice,或者消息: jpushclient.Message

//Notice
var notice jpushclient.Notice
notice.SetAlert("alert_test")
notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "AndroidNotice"})
notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "IOSNotice"})
notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})
  
//jpushclient.Message
var msg jpushclient.Message
msg.Title = "Hello"
msg.Content = "你是ylywn"

4.构建jpushclient.PayLoad

payload := jpushclient.NewPushPayLoad()
payload.SetPlatform(&pf)
payload.SetAudience(&ad)
payload.SetMessage(&msg)
payload.SetNotice(&notice)

5.构建PushClient,发出推送

c := jpushclient.NewPushClient(secret, appKey)
r, err := c.Send(bytes)
if err != nil {
	fmt.Printf("err:%s", err.Error())
} else {
	fmt.Printf("ok:%s", r)
}

6.完整demo

package main

import (
	"fmt"
	"github.com/ylywyn/jpush-api-go-client"
)

const (
	appKey = "you jpush appkey"
	secret = "you jpush secret"
)

func main() {

	//Platform
	var pf jpushclient.Platform
	pf.Add(jpushclient.ANDROID)
	pf.Add(jpushclient.IOS)
	pf.Add(jpushclient.WINPHONE)
	//pf.All()

	//Audience
	var ad jpushclient.Audience
	s := []string{"1", "2", "3"}
	ad.SetTag(s)
	ad.SetAlias(s)
	ad.SetID(s)
	//ad.All()

	//Notice
	var notice jpushclient.Notice
	notice.SetAlert("alert_test")
	notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "AndroidNotice"})
	notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "IOSNotice"})
	notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "WinPhoneNotice"})

	var msg jpushclient.Message
	msg.Title = "Hello"
	msg.Content = "你是ylywn"

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	payload.SetNotice(&notice)

	bytes, _ := payload.ToBytes()
	fmt.Printf("%s\r\n", string(bytes))

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	str, err := c.Send(bytes)
	if err != nil {
		fmt.Printf("err:%s", err.Error())
	} else {
		fmt.Printf("ok:%s", str)
	}
}

jpush-api-go-client's People

Contributors

ylywyn avatar wheelgong avatar mafangtao avatar zhangshp avatar

Watchers

James Cloos 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.