JPush REST API sdk for Golang
Simple install the package to your $GOPATH with the go tool from shell:
$ go get -u code.feelbus.cn/golang/jpush-sdk-go/...
Make sure Git is installed on your machine and in your system's PATH
.
- push api
//set push object
push := jpush.NewPushPayload()
push.Audience = new(jpush.Audience)
push.Audience.SetAlias("78633997736779513PG99000828576232")
push.Notification = jpush.NewNotification()
push.Notification.SetAndroid("push test", "这是一条推送的测试")
push.Notification.Android.AddExtra("testk", "testv")
push.Platform = new(jpush.Platform)
push.Platform.Android()
push.Options = jpush.NewOptions()
push.Options.TimeToLive = 3600
//create push client
client := jpush.NewPushClient(appkey, secret)
//send push
err := client.SendPush(push)
if err != nil {
println(err.Error())
}
- push schedule api(based on push api)
//set push object(same as object of push api)
push := jpush.NewPushPayload()
push.Audience = new(jpush.Audience)
push.Audience.SetAlias("xxxx")
push.Notification = jpush.NewNotification()
push.Notification.SetAndroid("push schedule test", "这是一条定期推送的测试")
push.Platform = new(jpush.Platform)
push.Platform.Android()
//set periodical or single object(only one can be set in a schedule task)
start := time.Now()
end := time.Now().Add(time.Hour * 3600)
t := time.Now().Add(time.Second * 20)
periodical := &jpush.TriggerPeriodicalNode{
Start: jpush.ScheduleDateTime{Time: &start},
End: jpush.ScheduleDateTime{Time: &end},
Frequency: 1,
TimeUnit: "day",
Time: jpush.ScheduleTime{Time: &t},
}
// t2 := time.Now().Add(time.Hour * 24 * 365)
// single := &jpush.TriggerSingleNode{Time: jpush.ScheduleDateTime{Time: &t2}}
//set schedule object
schedule := jpush.NewSchedulePayloadWithPeriodical("push_schedule_test", periodical, push)
//schedule := jpush.NewSchedulePayloadWithSingle("push_schedule_test", single, push)
//create push schedule client
client := jpush.NewScheduleClient(appkey, secret)
//create schedule task
resp, err := client.CreateSchedule(schedule)
if err != nil {
println(err.Error())
return
}
fmt.Println(resp)
- Update Schedule
push := NewPushPayload()
push.Audience = new(Audience)
push.Audience.SetAlias("78589117623039963DR868754020661958")
push.Notification = NewNotification()
push.Notification.SetAndroid("push schedule test", "这是一条定期推送的测试")
push.Platform = new(Platform)
push.Platform.Android()
push.Options.TimeToLive = 9999
push.Options.ApnsProduction = false
t2 := time.Now().Add(time.Minute * 15)
single := &TriggerSingleNode{Time: ScheduleDateTime{Time: &t2}}
//set schedule object
schedule := NewSchedulePayloadWithSingle("push_schedule_test", single, push).AsUpdate()
//create push schedule client
client := NewScheduleClient("", "")
_, err = client.UpdateSchedule(schedule ,scheduleId)
if err != nil {
println(err.Error())
return
}