Exemplo n.º 1
0
func (c *Client) EasyPush(config *JpushConfig) (err error) {
	if c.IsActive == false {
		kmgLog.Log("jpush", "Jpush Client is not active,please checkout your configure", c.name, c.IsIosProduct, c.IsActive, config)
		return
	}
	if config.Badge == "" {
		config.Badge = "1"
	}
	nb := jpush.NewNoticeBuilder()
	nb.SetPlatform(jpush.AllPlatform())
	if config.Alias == "" || config.Tag == "" {
		nb.SetAudience(jpush.AllAudience())
	} else {
		au := &jpush.Audience{}
		if config.Alias != "" {
			au.SetAlias([]string{config.Alias})
		}
		if config.Tag != "" {
			au.SetTag([]string{config.Tag})
		}
		nb.SetAudience(au)
	}
	//Android配置
	notice := jpush.NewNoticeAndroid()
	notice.Alert = config.Content
	nb.SetNotice(notice)

	//iOS配置
	iosNotice := jpush.NewNoticeIos()
	iosNotice.Sound = "default"
	iosNotice.Badge = "1"
	iosNotice.Alert = config.Content
	nb.SetNotice(iosNotice)

	op := jpush.NewOptions()
	op.SetApns_production(c.IsIosProduct)
	nb.SetOptions(op)
	ret, err := c.c.Send(nb)
	if err != nil {
		return err
	}
	if ret.Error.Code == 0 {
		kmgLog.Log("jpush", "Push success", c.name, config.Content)
		return nil
	}
	if ret.Error.Code == 1011 {
		kmgLog.Log("jpush", "Not Found User", c.name, config)
		return NotFoundUser
	}
	return fmt.Errorf("code:%d err: %s", ret.Error.Code, ret.Error.Message)
}
Exemplo n.º 2
0
func main() {
	c := jpush.NewPushClient(secret, appKey)

	//Platform
	//var pf jpush.Platform
	//pf.Add(ANDROID)

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

	//Notice
	notice := jpush.NewNoticeSimple()
	notice.Alert = "测试"

	//NoticeBuilder
	nb := jpush.NewNoticeBuilder()
	nb.SetPlatform(jpush.AllPlatform())
	nb.SetAudience(jpush.AllAudience())
	nb.SetNotice(notice)
	//push
	ret, err := c.Send(nb)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		if ret.Error.Code == 0 {
			fmt.Println("ok:", ret.Sendno)
		} else {
			fmt.Println("err:", ret.Error.Message)
		}
	}
	noticeAndroid := jpush.NewNoticeAndroid()
	noticeAndroid.Alert = "测试android"
	noticeAndroid.Title = "标题"
	nb = jpush.NewNoticeBuilder()
	nb.SetPlatform(jpush.AllPlatform())
	nb.SetAudience(jpush.AllAudience())
	nb.SetNotice(noticeAndroid)
	//push
	ret, err = c.Send(nb)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		if ret.Error.Code == 0 {
			fmt.Println("ok:", ret.Sendno)
		} else {
			fmt.Println("err:", ret.Error.Message)
		}
	}
	var msg jpush.Message
	msg.Title = "Hello"
	msg.Content = "祝大家工作顺利"

	mb := jpush.NewMessageBuilder()
	mb.SetPlatform(jpush.AllPlatform())
	mb.SetAudience(jpush.AllAudience())
	mb.SetMessage(&msg)

	//push
	ret, err = c.Send(mb)
	if err != nil {
		fmt.Println("err:", err.Error())
	} else {
		if ret.Error.Code == 0 {
			fmt.Println("ok:", ret.Sendno)
		} else {
			fmt.Println("err:", ret.Error.Message)
		}
	}

}