-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.go
More file actions
88 lines (79 loc) · 2.2 KB
/
push.go
File metadata and controls
88 lines (79 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package jpush
import (
"errors"
"log"
"reflect"
)
//平台
const (
Android = "android"
IOS = "ios"
WinPhone = "winphone"
)
//Push 推送对象结构
type Push struct {
Platform interface{} `json:"platform"`
Audience interface{} `json:"audience"`
Notification interface{} `json:"notification,omitempty"`
Message interface{} `json:"message,omitempty"`
SmsMessage interface{} `json:"sms_message,omitempty"`
Options interface{} `json:"options,omitempty"`
Cid interface{} `json:"cid,omitempty"`
}
//Push 推送
func (c *JPush) Push(push Push) (err error) {
if reflect.ValueOf(push.Platform).IsNil() {
push.Platform = "all"
}
if reflect.ValueOf(push.Audience).IsNil() {
push.Audience = "all"
}
req, err := createRequset(c.ServerAddr+PushURL, "POST", nil, push)
if err != nil {
return err
}
buf, err := c.requset(req)
if err != nil {
return err
}
log.Println(string(buf))
return
}
// PushNotification 推送通知消息
func (c *JPush) PushNotification(platform *Platform, audience *Audience, notification *Notification, options *Options) (err error) {
if notification == nil {
return errors.New("通知消息不能为空")
}
push := Push{
Platform: platform,
Audience: audience,
Options: options,
Notification: notification,
}
return c.Push(push)
}
//PushNotificationToTags 推送通知消息到指定标签
func (c *JPush) PushNotificationToTags(tags []string, notification *Notification) (err error) {
audience := CreateAudience()
audience.SetTag(tags)
return c.PushNotification(nil, audience, notification, nil)
}
//PushNotificationToAlias 推送通知消息到指定别名
func (c *JPush) PushNotificationToAlias(alias []string, notification *Notification) (err error) {
audience := CreateAudience()
audience.SetAlias(alias)
return c.PushNotification(nil, audience, notification, nil)
}
//PushMessage 推送自定义消息
func (c *JPush) PushMessage(platform *Platform, audience *Audience, message *Message, options *Options) (err error) {
if message == nil {
return errors.New("自定义消息不能为空")
}
push := Push{
Platform: platform,
Audience: audience,
Options: options,
Message: message,
}
return c.Push(push)
}