zhangjinkun 7 年之前
父节点
当前提交
7b190ca802
共有 5 个文件被更改,包括 239 次插入69 次删除
  1. 0 69
      golang/main.go
  2. 36 0
      golang/src/jgpush.json
  3. 166 0
      golang/src/jgpush/jgpush.go
  4. 15 0
      golang/src/main.go
  5. 22 0
      golang/src/main_test.go

+ 0 - 69
golang/main.go

@@ -1,69 +0,0 @@
-package main
-
-import (
-	"encoding/base64"
-	"fmt"
-	"io/ioutil"
-	"net/http"
-	"strings"
-)
-
-const (
-	push_url  = "https://bjapi.push.jiguang.cn/v3/push"
-	appid     = "5efa1257867cf5d77d007ce6"
-	appSecret = "d7a4e79978cd73cd61a5c7f7"
-)
-
-//
-func main() {
-	bodystr := `{
-    "platform": "all",
-    "audience": {
-        "registration_id": [
-            "140fe1da9ea179bd470"
-        ]
-    },
-    "notification": {
-        "android": {
-            "alert": "剑鱼推送消息,最新的订阅内容",
-            "title": "剑鱼招标订阅",
-            "builder_id": 1,
-            "extras": {
-                "newsid": 321
-            }
-        },
-        "ios": {
-            "alert": "推送消息",
-            "sound": "default",
-            "badge": "+1",
-            "extras": {
-                "newsid": 321
-            }
-        }
-    },
-    "message": {
-        "msg_content": "Hi,JPush",
-        "content_type": "text",
-        "title": "msg",
-        "extras": {
-            "key": "value"
-        }
-    },
-    "options": {
-        "time_to_live": 60,
-        "apns_production": false,
-        "apns_collapse_id":"jiguang_test_201706011100"
-    }
-}`
-	req, _ := http.NewRequest("POST", push_url, strings.NewReader(bodystr))
-	req.Header.Add("Content-Type", "application/json")
-	req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(appid+":"+appSecret)))
-	resp, err := http.DefaultClient.Do(req)
-	if err != nil {
-		fmt.Println(err.Error())
-	} else {
-		bs, _ := ioutil.ReadAll(resp.Body)
-		resp.Body.Close()
-		fmt.Println(string(bs))
-	}
-}

+ 36 - 0
golang/src/jgpush.json

@@ -0,0 +1,36 @@
+{
+    "pushurl": "https://bjapi.push.jiguang.cn/v3/push",
+    "appid": "5efa1257867cf5d77d007ce6",
+    "appSecret": "d7a4e79978cd73cd61a5c7f7",
+    "config": {
+        "platform": "all",
+        "audience": {
+            "registration_id": []
+        },
+        "notification": {
+            "android": {
+                "alert": "剑鱼推送消息,最新的订阅内容",
+                "title": "剑鱼招标订阅",
+                "builder_id": 1,
+                "extras": {}
+            },
+            "ios": {
+                "alert": "剑鱼推送消息,最新的订阅内容",
+                "sound": "default",
+                "badge": "+1",
+                "extras": {}
+            }
+        },
+        "message": {
+            "msg_content": "Hi,JPush",
+            "content_type": "text",
+            "title": "msg",
+            "extras": {}
+        },
+        "options": {
+            "time_to_live": 86400,
+            "apns_production": false,
+            "apns_collapse_id": "jiguang_test_201706011100"
+        }
+    }
+}

+ 166 - 0
golang/src/jgpush/jgpush.go

@@ -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))
+	}
+}

+ 15 - 0
golang/src/main.go

@@ -0,0 +1,15 @@
+package main
+
+import (
+	"jgpush"
+	qu "qfw/util"
+)
+
+func init() {
+	qu.ReadConfig("./jgpush.json", &jgpush.Jgconfig)
+}
+
+//13065ffa4e027b9d39e
+func main() {
+	jgpush.JgpushNc("剑鱼推送消息,最新的订阅内容", "招标公告", "bid", []string{"13065ffa4e027b9d39e"}, nil, false)
+}

+ 22 - 0
golang/src/main_test.go

@@ -0,0 +1,22 @@
+// main_test
+package main
+
+import (
+	"jgpush"
+	"testing"
+)
+
+func Test_jgpush(t *testing.T) {
+	ids := []string{"13065ffa4e027b9d39e"}
+	//通知
+	jgpush.JgpushNc("剑鱼推送消息,最新的订阅内容", "招标公告", "bid", ids, map[string]interface{}{
+		"name": "张三", //信息以及其他自定义属性
+	}, false)
+	//消息
+	jgpush.JgpushMsg("msg_title", "msg_con", "text", ids, map[string]interface{}{
+		"name": "张三",
+	}, false)
+	//通知、消息
+	jgpush.JgpushNcAndMsg("alert", "title", "bid", "msg_title", "msg_con", "text", ids, nil, nil, false)
+
+}