You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.2 KiB
101 lines
2.2 KiB
package jpush
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
jpushclient "github.com/ylywyn/jpush-api-go-client"
|
|
)
|
|
|
|
/*
|
|
极光推送demo
|
|
*/
|
|
const (
|
|
//appKey = "a7aafd610d5309e616c2d02c"
|
|
appKey = "722dbc6ce24f3c6b99eb2993"
|
|
//secret = "97ddbb427e9d17c3ee6f2c18"
|
|
secret = "4043b254ab9feb8f3e97bc57"
|
|
)
|
|
|
|
func main() {
|
|
//构建接收平台
|
|
var jp jpushclient.Platform
|
|
_ = jp.Add(jpushclient.ANDROID)
|
|
_ = jp.Add(jpushclient.IOS)
|
|
//构建接收听众
|
|
var ad jpushclient.Audience
|
|
//s := []string{"laotang_go"}
|
|
//ad.SetTag(s)
|
|
id := []string{"140fe1da9e069dd52e4"}
|
|
ad.SetID(id)
|
|
//ad.All()
|
|
//Notice
|
|
var notice jpushclient.Notice
|
|
notice.SetAlert("alert_test")
|
|
notice.SetAndroidNotice(&jpushclient.AndroidNotice{Alert: "瑞库客大酬宾全场一折起"})
|
|
//notice.SetIOSNotice(&jpushclient.IOSNotice{Alert: "瑞库客大酬宾全场一折起"})
|
|
//notice.SetWinPhoneNotice(&jpushclient.WinPhoneNotice{Alert: "瑞库客大酬宾全场一折起"})
|
|
////jpushclient.Message
|
|
//var msg jpushclient.Message
|
|
//msg.Title = "火影忍者"
|
|
//msg.Content = "测试404"
|
|
|
|
payload := jpushclient.NewPushPayLoad()
|
|
payload.SetPlatform(&jp)
|
|
payload.SetAudience(&ad)
|
|
//payload.SetMessage(&msg)
|
|
payload.SetNotice(¬ice)
|
|
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
type Client struct {
|
|
*jpushclient.PushClient
|
|
}
|
|
|
|
var once sync.Once
|
|
|
|
var client Client
|
|
|
|
func GetClient() *Client {
|
|
once.Do(func() {
|
|
client = Client{jpushclient.NewPushClient(secret, appKey)}
|
|
})
|
|
return &client
|
|
}
|
|
|
|
func (o *Client) PushMsgWithRegister(registerID string, title string, content string) {
|
|
var pf jpushclient.Platform
|
|
pf.Add(jpushclient.ANDROID)
|
|
pf.Add(jpushclient.IOS)
|
|
|
|
var ad jpushclient.Audience
|
|
ad.SetID([]string{registerID})
|
|
var msg jpushclient.Message
|
|
msg.Title = title
|
|
msg.Content = content
|
|
|
|
payload := jpushclient.NewPushPayLoad()
|
|
payload.SetPlatform(&pf)
|
|
payload.SetAudience(&ad)
|
|
payload.SetMessage(&msg)
|
|
bytes, _ := payload.ToBytes()
|
|
r, err := o.Send(bytes)
|
|
if err != nil {
|
|
fmt.Printf("err:%s", err.Error())
|
|
} else {
|
|
fmt.Printf("ok:%s", r)
|
|
}
|
|
}
|