github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/third/kmgJpush/kmgJpush.go (about)

     1  package kmgJpush
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/bronze1man/kmg/errors"
     6  	"github.com/bronze1man/kmg/kmgLog"
     7  	"github.com/isdamir/jpush"
     8  )
     9  
    10  type Client struct {
    11  	c            *jpush.PushClient
    12  	IsIosProduct bool
    13  	name         string
    14  	Platform     SystemPlatform
    15  	IsActive     bool
    16  }
    17  
    18  type NewClientRequest struct {
    19  	Name         string //这个客户端的名字
    20  	AppKey       string
    21  	Secret       string
    22  	Platform     SystemPlatform //iOS、Android平台
    23  	IsIosProduct bool           //如果是否false表示向测试设备推送,如果是true表示向正式设备推送,后台的那个开发与正式似乎没有作用.
    24  	IsActive     bool
    25  }
    26  type JpushConfig struct {
    27  	Content string
    28  	Alias string
    29  	Tag string
    30  	Badge string
    31  }
    32  func (c *Client) EasyPush(config *JpushConfig)(err error){
    33  	if c.IsActive == false{
    34  		kmgLog.Log("jpush", "Jpush Client is not active,please checkout your configure",c.name,c.IsIosProduct,c.IsActive,config)
    35  		return
    36  	}
    37  	if config.Badge == ""{
    38  		config.Badge = "1"
    39  	}
    40  	nb := jpush.NewNoticeBuilder()
    41  	nb.SetPlatform(jpush.AllPlatform())
    42  	if config.Alias == ""||config.Tag == ""{
    43  		nb.SetAudience(jpush.AllAudience())
    44  	}else{
    45  		au := &jpush.Audience{}
    46  		if config.Alias != ""{
    47  			au.SetAlias([]string{config.Alias})
    48  		}
    49  		if config.Tag != ""{
    50  			au.SetTag([]string{config.Tag})
    51  		}
    52  		nb.SetAudience(au)
    53  	}
    54  	//Android配置
    55  	notice := jpush.NewNoticeAndroid()
    56  	notice.Alert = config.Content
    57  	nb.SetNotice(notice)
    58  
    59  	//iOS配置
    60  	iosNotice := jpush.NewNoticeIos()
    61  	iosNotice.Sound = "default"
    62  	iosNotice.Badge = "1"
    63  	iosNotice.Alert = config.Content
    64  	nb.SetNotice(iosNotice)
    65  
    66  	op := jpush.NewOptions()
    67  	op.SetApns_production(c.IsIosProduct)
    68  	nb.SetOptions(op)
    69  	ret, err := c.c.Send(nb)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	if ret.Error.Code == 0 {
    74  		kmgLog.Log("jpush", "Push success", c.name, config.Content)
    75  		return nil
    76  	}
    77  	if ret.Error.Code == 1011 {
    78  		kmgLog.Log("jpush","Not Found User",c.name,config)
    79  		return NotFoundUser
    80  	}
    81  	return fmt.Errorf("code:%d err: %s", ret.Error.Code, ret.Error.Message)
    82  }
    83  
    84  type SystemPlatform string
    85  
    86  const (
    87  	Android     SystemPlatform = "Android"
    88  	IOS         SystemPlatform = "iOS"
    89  	AllPlatform SystemPlatform = "AllPlatform"
    90  )
    91  
    92  func NewClient(req NewClientRequest) *Client {
    93  	return &Client{
    94  		c:            jpush.NewPushClient(req.Secret, req.AppKey),
    95  		IsIosProduct: req.IsIosProduct,
    96  		name:         req.Name,
    97  		Platform:     req.Platform,
    98  		IsActive:     req.IsActive,
    99  	}
   100  }
   101  
   102  var NotFoundUser error = errors.New("[kmgJpush] user not exist")
   103  
   104  func (c *Client) PushToOne(alias string, content string) (err error) {
   105  	err = c.EasyPush(&JpushConfig{
   106  		Alias: alias,
   107  		Content: content,
   108  	})
   109  	if err != nil {
   110  		return err
   111  	}
   112  	return nil
   113  }
   114  
   115  func (c *Client) PushToTag(tag string, content string) (err error) {
   116  	err = c.EasyPush(&JpushConfig{
   117  		Tag: tag,
   118  		Content: content,
   119  	})
   120  	if err != nil {
   121  		return err
   122  	}
   123  	return nil
   124  }
   125  
   126  func (c *Client) PushToAll(content string) (err error) {
   127  	err = c.EasyPush(&JpushConfig{
   128  		Content: content,
   129  	})
   130  	if err != nil {
   131  		return err
   132  	}
   133  	return nil
   134  }