parent
97fa1146b2
commit
30be5c2c48
@ -0,0 +1,17 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
PushUrl = "https://api.jpush.cn/v3/push"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetAuthorization(Appkey string, MasterSecret string) string {
|
||||||
|
str := fmt.Sprintf("%s:%s", Appkey, MasterSecret)
|
||||||
|
buf := []byte(str)
|
||||||
|
Authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(buf))
|
||||||
|
return Authorization
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package push
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/src/common/config"
|
||||||
|
"Open_IM/src/push/jpush/common"
|
||||||
|
"Open_IM/src/push/jpush/requestBody"
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func JGAccountListPush(accounts []string, jsonCustomContent string, Platform string) (*http.Response, error) {
|
||||||
|
|
||||||
|
var pf requestBody.Platform
|
||||||
|
_ = pf.SetAndroid()
|
||||||
|
var au requestBody.Audience
|
||||||
|
au.SetAlias(accounts)
|
||||||
|
var no requestBody.Notification
|
||||||
|
no.SetAlert(jsonCustomContent)
|
||||||
|
var me requestBody.Message
|
||||||
|
me.SetMsgContent(jsonCustomContent)
|
||||||
|
var po requestBody.PushObj
|
||||||
|
po.SetPlatform(&pf)
|
||||||
|
po.SetAudience(&au)
|
||||||
|
po.SetNotification(&no)
|
||||||
|
po.SetMessage(&me)
|
||||||
|
|
||||||
|
con, err := json.Marshal(po)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("POST", common.PushUrl, bytes.NewBuffer(con))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", common.GetAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret))
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return resp, nil
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package requestBody
|
||||||
|
|
||||||
|
const (
|
||||||
|
TAG = "tag"
|
||||||
|
TAG_AND = "tag_and"
|
||||||
|
TAG_NOT = "tag_not"
|
||||||
|
ALIAS = "alias"
|
||||||
|
REGISTRATION_ID = "registration_id"
|
||||||
|
SEGMENT = "segment"
|
||||||
|
ABTEST = "abtest"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Audience struct {
|
||||||
|
Object interface{}
|
||||||
|
audience map[string][]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) set(key string, v []string) {
|
||||||
|
if a.audience == nil {
|
||||||
|
a.audience = make(map[string][]string)
|
||||||
|
a.Object = a.audience
|
||||||
|
}
|
||||||
|
|
||||||
|
//v, ok = this.audience[key]
|
||||||
|
//if ok {
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
a.audience[key] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetTag(tags []string) {
|
||||||
|
a.set(TAG, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetTagAnd(tags []string) {
|
||||||
|
a.set(TAG_AND, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetTagNot(tags []string) {
|
||||||
|
a.set(TAG_NOT, tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetAlias(alias []string) {
|
||||||
|
a.set(ALIAS, alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetRegistrationId(ids []string) {
|
||||||
|
a.set(REGISTRATION_ID, ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Audience) SetAll() {
|
||||||
|
a.Object = "all"
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package requestBody
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
MsgContent string `json:"msg_content"`
|
||||||
|
Title string `json:"title,omitempty"`
|
||||||
|
ContentType string `json:"content_type,omitempty"`
|
||||||
|
Extras map[string]interface{} `json:"extras,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) SetMsgContent(c string) {
|
||||||
|
m.MsgContent = c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) SetTitle(t string) {
|
||||||
|
m.Title = t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) SetContentType(c string) {
|
||||||
|
m.ContentType = c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Message) SetExtras(key string, value interface{}) {
|
||||||
|
if m.Extras == nil {
|
||||||
|
m.Extras = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
m.Extras[key] = value
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package requestBody
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
Alert string `json:"alert,omitempty"`
|
||||||
|
Android *Android `json:"android,omitempty"`
|
||||||
|
IOS *Ios `json:"ios,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Android struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Ios struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *Notification) SetAlert(alert string) {
|
||||||
|
n.Alert = alert
|
||||||
|
}
|
@ -0,0 +1,76 @@
|
|||||||
|
package requestBody
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
const (
|
||||||
|
ANDROID = "android"
|
||||||
|
IOS = "ios"
|
||||||
|
QUICKAPP = "quickapp"
|
||||||
|
WINDOWSPHONE = "winphone"
|
||||||
|
ALL = "all"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Platform struct {
|
||||||
|
Os interface{}
|
||||||
|
osArry []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) Set(os string) error {
|
||||||
|
if p.Os == nil {
|
||||||
|
p.osArry = make([]string, 0, 4)
|
||||||
|
} else {
|
||||||
|
switch p.Os.(type) {
|
||||||
|
case string:
|
||||||
|
return errors.New("platform is all")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, value := range p.osArry {
|
||||||
|
if os == value {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch os {
|
||||||
|
case IOS:
|
||||||
|
fallthrough
|
||||||
|
case ANDROID:
|
||||||
|
fallthrough
|
||||||
|
case QUICKAPP:
|
||||||
|
fallthrough
|
||||||
|
case WINDOWSPHONE:
|
||||||
|
p.osArry = append(p.osArry, os)
|
||||||
|
p.Os = p.osArry
|
||||||
|
default:
|
||||||
|
return errors.New("unknow platform")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (p *Platform) setPlatform(platform string) {
|
||||||
|
switch platform {
|
||||||
|
case ANDROID:
|
||||||
|
p.SetAndroid()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func (p *Platform) SetIOS() error {
|
||||||
|
return p.Set(IOS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) SetAndroid() error {
|
||||||
|
return p.Set(ANDROID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) SetQuickApp() error {
|
||||||
|
return p.Set(QUICKAPP)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) SetWindowsPhone() error {
|
||||||
|
return p.Set(WINDOWSPHONE)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Platform) SetAll() {
|
||||||
|
p.Os = ALL
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
package requestBody
|
||||||
|
|
||||||
|
type PushObj struct {
|
||||||
|
Platform interface{} `json:"platform"`
|
||||||
|
Audience interface{} `json:"audience"`
|
||||||
|
Notification interface{} `json:"notification,omitempty"`
|
||||||
|
Message interface{} `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PushObj) SetPlatform(pf *Platform) {
|
||||||
|
p.Platform = pf.Os
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PushObj) SetAudience(ad *Audience) {
|
||||||
|
p.Audience = ad.Object
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PushObj) SetNotification(no *Notification) {
|
||||||
|
p.Notification = no
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *PushObj) SetMessage(m *Message) {
|
||||||
|
p.Message = m
|
||||||
|
}
|
Loading…
Reference in new issue