package getui

import (
"encoding/json"
"gugegin/tools"
"log"
"strings"
)

//【toSingle】执行cid单推
func TestSingle(cid string) {
cids := []string{cid}
rd := tools.RandomStr(5)
PushSingle(cids, "单推标题"+rd, "单推内容"+rd)
}

//推送给一个用户,执行一次
func PushSingle(cids []string, title string, body string) {
url := BASEURL + "/push/single/cid"

aud := Audience{
Cid: cids,
}
//Click_type
//点击通知后续动作,
//目前支持以下后续动作,
//intent:打开应用内特定页面,
//url:打开网页地址,
//payload:自定义消息内容启动应用,
//payload_custom:自定义消息内容不启动应用,
//startapp:打开应用首页,
//none:纯通知,无后续动作

noti := Notification{
Title: title,
Body: body,
Click_type: "none",
}
pus := Push_message{
Notification: noti,
}

signe := Single{
Request_id: tools.RandomStr(30),
Audience: aud,
Push_message: pus,
}
reqBody := strings.ToLower(tools.StructToJSON(signe))
log.Println("--------PushSingle----reqBody------", reqBody)
s := tools.ClientPost2(url, strings.NewReader(reqBody), TokenValue)
log.Println("--------PushSingle----------", s)
sResult := PushResult{}
err := json.Unmarshal([]byte(s), &sResult)
if err != nil {
log.Println(err)
}
log.Println("--------PushSingle-----code-----", sResult.Code)
if sResult.Code == 0 {
log.Println("推送成功")
}

if sResult.Code == 10001 {
log.Println("--------PushSingle----token过期或无效------")
//token过期
ObtainToken()
PushSingle(cids, title, body)
}
log.Println("--------PushSingle-----msg-----", sResult.Msg)
}