Skip to content

FeiniuBus/jpush-sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

JPush SDK for Go

JPush REST API sdk for Golang


Installation

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.

Example

  1. 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())
	}
  1. 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)
  1. 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
	}