|
@@ -0,0 +1,166 @@
|
|
|
+// jpush
|
|
|
+package jgpush
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/base64"
|
|
|
+ "encoding/json"
|
|
|
+ "io/ioutil"
|
|
|
+ "log"
|
|
|
+ "net/http"
|
|
|
+ "strings"
|
|
|
+)
|
|
|
+
|
|
|
+type jgconfig struct {
|
|
|
+ Pushurl, Appid, AppSecret string
|
|
|
+ Config config
|
|
|
+}
|
|
|
+type config struct {
|
|
|
+ Platform string
|
|
|
+ Audience map[string]interface{}
|
|
|
+ Notification map[string]interface{}
|
|
|
+ Message map[string]interface{}
|
|
|
+ Options map[string]interface{}
|
|
|
+}
|
|
|
+
|
|
|
+var Jgconfig jgconfig
|
|
|
+
|
|
|
+/**
|
|
|
+*通知推送
|
|
|
+alert:通知栏,空默认配置
|
|
|
+title:通知标题,空默认配置
|
|
|
+infotype:推送信息类型(bid,project,entname)
|
|
|
+ids:极光id组
|
|
|
+extras:自定义字段
|
|
|
+broadcast:是否广播,广播ids作废
|
|
|
+*/
|
|
|
+func JgpushNc(alert, title, infotype string, ids []string, extras map[string]interface{}, isbroadcast bool) {
|
|
|
+ temp := &Jgconfig.Config
|
|
|
+ temp.Audience["registration_id"] = ids
|
|
|
+ android := temp.Notification["android"]
|
|
|
+ if tmp, ok := android.(map[string]interface{}); ok {
|
|
|
+ tmp["alert"] = alert
|
|
|
+ tmp["title"] = title
|
|
|
+ tmp["extras"] = map[string]interface{}{
|
|
|
+ "type": infotype,
|
|
|
+ "list": extras,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ios := temp.Notification["ios"]
|
|
|
+ if tmp, ok := ios.(map[string]interface{}); ok {
|
|
|
+ tmp["alert"] = alert
|
|
|
+ tmp["extras"] = map[string]interface{}{
|
|
|
+ "type": infotype,
|
|
|
+ "list": extras,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ sendcon := map[string]interface{}{
|
|
|
+ "platform": temp.Platform,
|
|
|
+ "audience": temp.Audience,
|
|
|
+ "notification": temp.Notification,
|
|
|
+ "options": temp.Options,
|
|
|
+ //"message": temp.Message,
|
|
|
+ }
|
|
|
+ bs, _ := json.Marshal(sendcon)
|
|
|
+ req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
+ resp, err := http.DefaultClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("JgpushNc", err.Error())
|
|
|
+ } else {
|
|
|
+ bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ resp.Body.Close()
|
|
|
+ log.Println("JgpushNc", string(bs))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+*消息推送
|
|
|
+msg_con:消息内容
|
|
|
+msg_title:消息标题
|
|
|
+msg_type:消息内容类型
|
|
|
+ids:极光id组
|
|
|
+extras:自定义字段
|
|
|
+broadcast:是否广播,广播ids作废
|
|
|
+*/
|
|
|
+func JgpushMsg(msg_title, msg_con, msg_type string, ids []string, extras map[string]interface{}, isbroadcast bool) {
|
|
|
+ temp := Jgconfig.Config
|
|
|
+ temp.Audience["registration_id"] = ids
|
|
|
+ message := temp.Message
|
|
|
+ message["msg_content"] = msg_con
|
|
|
+ message["content_type"] = msg_type
|
|
|
+ message["title"] = msg_title
|
|
|
+ message["extras"] = extras
|
|
|
+
|
|
|
+ sendcon := map[string]interface{}{
|
|
|
+ "platform": temp.Platform,
|
|
|
+ "audience": temp.Audience,
|
|
|
+ "options": temp.Options,
|
|
|
+ "message": temp.Message,
|
|
|
+ }
|
|
|
+ bs, _ := json.Marshal(sendcon)
|
|
|
+ req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
+ resp, err := http.DefaultClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("JgpushMsg", err.Error())
|
|
|
+ } else {
|
|
|
+ bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ resp.Body.Close()
|
|
|
+ log.Println("JgpushMsg", string(bs))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//通知、消息推送、广播
|
|
|
+func JgpushNcAndMsg(alert, title, infotype string, msg_title, msg_con, msg_type string, ids []string, extras, msg_extras map[string]interface{}, isbroadcast bool) {
|
|
|
+ temp := Jgconfig.Config
|
|
|
+ temp.Audience["registration_id"] = ids
|
|
|
+ android := temp.Notification["android"]
|
|
|
+ if tmp, ok := android.(map[string]interface{}); ok {
|
|
|
+ tmp["alert"] = alert
|
|
|
+ tmp["title"] = title
|
|
|
+ tmp["extras"] = map[string]interface{}{
|
|
|
+ "type": infotype,
|
|
|
+ "list": extras,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ios := temp.Notification["ios"]
|
|
|
+ if tmp, ok := ios.(map[string]interface{}); ok {
|
|
|
+ tmp["alert"] = alert
|
|
|
+ tmp["extras"] = map[string]interface{}{
|
|
|
+ "type": infotype,
|
|
|
+ "list": extras,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ temp.Audience["registration_id"] = ids
|
|
|
+ message := temp.Message
|
|
|
+ message["msg_content"] = msg_con
|
|
|
+ message["content_type"] = msg_type
|
|
|
+ message["title"] = msg_title
|
|
|
+ message["extras"] = extras
|
|
|
+
|
|
|
+ sendcon := map[string]interface{}{
|
|
|
+ "platform": temp.Platform,
|
|
|
+ "audience": temp.Audience,
|
|
|
+ "notification": temp.Notification,
|
|
|
+ "options": temp.Options,
|
|
|
+ "message": temp.Message,
|
|
|
+ }
|
|
|
+ if isbroadcast {
|
|
|
+ sendcon["audience"] = "all"
|
|
|
+ }
|
|
|
+ bs, _ := json.Marshal(sendcon)
|
|
|
+ req, _ := http.NewRequest("POST", Jgconfig.Pushurl, strings.NewReader(string(bs)))
|
|
|
+ req.Header.Add("Content-Type", "application/json")
|
|
|
+ req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(Jgconfig.Appid+":"+Jgconfig.AppSecret)))
|
|
|
+ resp, err := http.DefaultClient.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Jgpush", err.Error())
|
|
|
+ } else {
|
|
|
+ bs, _ := ioutil.ReadAll(resp.Body)
|
|
|
+ resp.Body.Close()
|
|
|
+ log.Println("JgpushNcAndMsg", string(bs))
|
|
|
+ }
|
|
|
+}
|