├── .gitignore ├── LICENSE ├── README.md ├── client.go ├── config.go ├── example ├── push.go └── tags.go ├── message.go ├── push.go ├── request.go ├── response.go └── tags.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Go template 3 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 4 | *.o 5 | *.a 6 | *.so 7 | 8 | # Folders 9 | _obj 10 | _test 11 | 12 | # Architecture specific extensions/prefixes 13 | *.[568vq] 14 | [568vq].out 15 | 16 | *.cgo1.go 17 | *.cgo2.c 18 | _cgo_defun.c 19 | _cgo_gotypes.go 20 | _cgo_export.* 21 | 22 | _testmain.go 23 | 24 | *.exe 25 | *.test 26 | *.prof 27 | 28 | 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 腾讯信鸽go-sdk 2 | ============ 3 | 4 | [![Build Status](https://drone.io/github.com/aiwuTech/xinge/status.png)](https://drone.io/github.com/aiwuTech/xinge/latest) 5 | [![Go Walker](http://gowalker.org/api/v1/badge)](http://gowalker.org/github.com/aiwuTech/xinge) 6 | 7 | Installation 8 | ------------ 9 | 10 | 安装腾讯信鸽go-sdk使用"go get"命令: 11 | 12 | go get github.com/aiwuTech/xinge 13 | 14 | 15 | Update 16 | ------ 17 | 18 | 更新腾讯信鸽go-sdk使用"go get -u"命令 19 | 20 | go get -u github.com/aiwuTech/xinge 21 | 22 | Usage 23 | ----- 24 | 25 | 用法请参考[example](https://github.com/aiwuTech/xinge/tree/master/example),持续更新中 26 | 27 | API 28 | --- 29 | 30 | 请参考[Go Walker](https://gowalker.org/github.com/aiwuTech/xinge) 31 | 32 | 33 | FAQ 34 | --- 35 | 36 | 如果使用过程中遇到任何问题,希望主动与[aiwuTech团队](https://github.com/aiwuTech/)联系,也可提交[Issues](https://github.com/aiwuTech/xinge/issues),我们会及时解决 37 | 38 | 39 | License 40 | ------- 41 | 42 | 腾讯信鸽go-sdk基于 [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). 43 | -------------------------------------------------------------------------------- /client.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | import "errors" 17 | 18 | type Client struct { 19 | AccessId string 20 | AccessKey string 21 | ValidTime uint 22 | SecretKey string 23 | } 24 | 25 | func NewClient(accessId string, validTime uint, accessKey, secretKey string) *Client { 26 | return &Client{ 27 | AccessId: accessId, 28 | AccessKey: accessKey, 29 | ValidTime: validTime, 30 | SecretKey: secretKey, 31 | } 32 | } 33 | 34 | func (cli *Client) NewRequest(method, url string) *Request { 35 | return &Request{ 36 | HttpMethod: method, 37 | HttpUrl: url, 38 | Params: make(map[string]interface{}), 39 | Client: cli, 40 | } 41 | } 42 | 43 | func (cli *Client) AppDeviceNum() (int64, error) { 44 | request := cli.NewRequest("GET", deviceNumUrl) 45 | 46 | response, err := request.Execute() 47 | if err != nil { 48 | return 0, errors.New(" request app device num err:" + err.Error()) 49 | } 50 | 51 | if !response.OK() { 52 | return 0, errors.New(" response err:" + response.Error()) 53 | } 54 | 55 | result := response.Result.(map[string]interface{}) 56 | 57 | return int64(result["device_num"].(float64)), nil 58 | } 59 | -------------------------------------------------------------------------------- /config.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | import "fmt" 17 | 18 | var ( 19 | apiDomain = "openapi.xg.qq.com" 20 | apiVersion = "v2" 21 | ) 22 | 23 | var ( 24 | singleDeviceUrl = fmt.Sprintf("http://%s/%s/push/single_device", apiDomain, apiVersion) 25 | singleAccountUrl = fmt.Sprintf("http://%s/%s/push/single_account", apiDomain, apiVersion) 26 | multiAccountUrl = fmt.Sprintf("http://%s/%s/push/account_list", apiDomain, apiVersion) 27 | allDeviceUrl = fmt.Sprintf("http://%s/%s/push/all_device", apiDomain, apiVersion) 28 | tagsDeviceUrl = fmt.Sprintf("http://%s/%s/push/tags_device", apiDomain, apiVersion) 29 | deviceNumUrl = fmt.Sprintf("http://%s/%s/application/get_app_device_num", apiDomain, apiVersion) 30 | appTagsUrl = fmt.Sprintf("http://%s/%s/tags/query_app_tags", apiDomain, apiVersion) 31 | batchSetTagsUrl = fmt.Sprintf("http://%s/%s/tags/batch_set", apiDomain, apiVersion) 32 | batchDelTagsUrl = fmt.Sprintf("http://%s/%s/tags/batch_del", apiDomain, apiVersion) 33 | tokenTagsUrl = fmt.Sprintf("http://%s/%s/tags/query_token_tags", apiDomain, apiVersion) 34 | tagTokensUrl = fmt.Sprintf("http://%s/%s/tags/query_tag_token_num", apiDomain, apiVersion) 35 | ) 36 | 37 | type PlatformType byte 38 | 39 | const ( 40 | Platform_ios PlatformType = iota 41 | Platform_android 42 | ) 43 | 44 | type MessageType byte 45 | 46 | const ( 47 | MessageType_ios MessageType = iota 48 | MessageType_notify 49 | MessageType_passthrough 50 | ) 51 | 52 | type PushEnv byte 53 | 54 | const ( 55 | PushEnv_android PushEnv = iota 56 | PushEnv_prod 57 | PushEnv_dev 58 | ) 59 | 60 | type MultiPkgType byte 61 | 62 | const ( 63 | MultiPkg_pkg MultiPkgType = iota 64 | MultiPkg_aid 65 | MultiPkg_ios 66 | ) 67 | 68 | type PushType byte 69 | 70 | const ( 71 | PushType_single_device PushType = iota 72 | PushType_single_account 73 | PushType_multi_account 74 | PushType_all_device 75 | PushType_tags_device 76 | ) 77 | 78 | const ( 79 | TagsOp_AND = "AND" 80 | TagsOp_OR = "OR" 81 | ) 82 | -------------------------------------------------------------------------------- /example/push.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package main 15 | 16 | import ( 17 | "fmt" 18 | "github.com/aiwuTech/xinge" 19 | "time" 20 | ) 21 | 22 | var ( 23 | xingeClient = xinge.NewClient("2100094636", 300, "A61ULFG44E8R", "3f4ab60b6c2d95d09587dd8fa17ac04b") 24 | ) 25 | 26 | func main() { 27 | 28 | message := &xinge.AndroidMessage{ 29 | Content: "wifi密码qq发过来", 30 | Title: "wifi密码是多少", 31 | } 32 | 33 | reqPush := &xinge.ReqPush{ 34 | PushType: xinge.PushType_tags_device, 35 | Tags: []string{"标签1", "标签2"}, 36 | TagsOp: xinge.TagsOp_AND, 37 | UserAccounts: []string{"sdfsadf", "lsdjfoiwjo"}, 38 | DeviceToken: "sdfsdfewfsadfsdfsdf", 39 | MessageType: xinge.MessageType_notify, 40 | Message: message, 41 | ExpireTime: 300, 42 | SendTime: time.Now(), 43 | MultiPkgType: xinge.MultiPkg_aid, 44 | PushEnv: xinge.PushEnv_android, 45 | PlatformType: xinge.Platform_android, 46 | LoopTimes: 2, 47 | LoopInterval: 7, 48 | Cli: xingeClient, 49 | } 50 | fmt.Println(reqPush.Push()) 51 | 52 | fmt.Println(xingeClient.AppDeviceNum()) 53 | } 54 | -------------------------------------------------------------------------------- /example/tags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package main 15 | 16 | import ( 17 | "fmt" 18 | "github.com/globalways/xinge" 19 | ) 20 | 21 | var ( 22 | xingeClient = xinge.NewClient("2100094636", 300, "A61ULFG44E8R", "3f4ab60b6c2d95d09587dd8fa17ac04b") 23 | ) 24 | 25 | func main() { 26 | fmt.Println(xingeClient.AppTags(0, 100)) 27 | fmt.Println(xingeClient.SetTags([2]string{"标签1", "094858ba12dd24cf4856bff6f7696826c546bcda"}, [2]string{"标签2", "094858ba12dd24cf4856bff6f7696826c546bcda"})) 28 | fmt.Println(xingeClient.TokenTags("094858ba12dd24cf4856bff6f7696826c546bcda")) 29 | fmt.Println(xingeClient.TagTokensNum("标签1")) 30 | fmt.Println(xingeClient.DelTags([2]string{"标签1", "094858ba12dd24cf4856bff6f7696826c546bcda"}, [2]string{"标签2", "094858ba12dd24cf4856bff6f7696826c546bcda"})) 31 | } 32 | -------------------------------------------------------------------------------- /message.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | type AndroidMessage struct { 17 | Title string `json:"title"` // 标题,必填 18 | Content string `json:"content"` // 内容,必填 19 | AcceptTime []*AcceptTime `json:"accept_time,omitempty"` //表示消息将在哪些时间段允许推送给用户,选填 20 | NotifyId byte `json:"n_id,omitempty"` //通知id,选填。若大于0,则会覆盖先前弹出的相同id通知;若为0,展示本条通知且不影响其他通知;若为-1,将清除先前弹出的所有通知,仅展示本条通知。默认为0 21 | BuilderId int `json:"builder_id,omitempty"` // 本地通知样式,必填 22 | Ring byte `json:"ring,omitempty"` // 是否响铃,0否,1是,下同。选填,默认1 23 | RingRaw string `json:"ring_raw,omitempty"` // 指定应用内的声音(ring.mp3),选填 24 | Vibrate byte `json:"vibrate,omitempty"` // 是否振动,选填,默认1 25 | Lights byte `json:"lights,omitempty"` // 是否呼吸灯,0否,1是,选填,默认1 26 | Clearable byte `json:"clearable,omitempty"` // 通知栏是否可清除,选填,默认1 27 | IconType byte `json:"icon_type,omitempty"` //默认0,通知栏图标是应用内图标还是上传图标,0是应用内图标,1是上传图标,选填 28 | IconRes string `json:"icon_res,omitempty"` // 应用内图标文件名(xg.png)或者下载图标的url地址,选填 29 | StyleId byte `json:"style_id,omitempty"` //Web端设置是否覆盖编号的通知样式,默认1,0否,1是,选填 30 | SmailIcon string `json:"smail_icon,omitempty"` //指定状态栏的小图片(xg.png),选填 31 | Action *AndroidAction `json:"action,omitempty"` // 动作,选填。默认为打开app 32 | CustomContent map[string]interface{} `json:"custom_content"` 33 | } 34 | 35 | type AcceptTime struct { 36 | Start *HourMin `json:"start"` 37 | End *HourMin `json:"end"` 38 | } 39 | 40 | type HourMin struct { 41 | Hour string `json:"hour"` 42 | Min string `json:"min"` 43 | } 44 | 45 | type AndroidAction struct { 46 | ActionType byte `json:"action_type"` // 动作类型,1打开activity或app本身,2打开浏览器,3打开Intent,4通过包名拉起其他应用 47 | Activity string `json:"activity"` 48 | AtyAttr *ActivityAttr `json:"aty_attr"` // activity属性,只针对action_type=1的情况 49 | Browser *Browser `json:"browser"` 50 | IntenterNet string `json:"intent"` 51 | PackageName *Package `json:"package_name"` 52 | } 53 | 54 | type ActivityAttr struct { 55 | IF byte `json:"if"` // 创建通知时,intent的属性,如:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 56 | PF byte `json:"pf"` // PendingIntent的属性,如:PendingIntent.FLAG_UPDATE_CURRENT 57 | } 58 | 59 | // url:打开的url,confirm是否需要用户确认 60 | type Browser struct { 61 | Url string `json:"url"` 62 | Confirm byte `json:"confirm"` 63 | } 64 | 65 | type Package struct { 66 | PackageName string `json:"packageName"` // 要拉起的别的应用的包名 67 | PackageDLUrl string `json:"packageDownloadUrl"` //拉起应用的下载链接(若客户端没有找到此应用会自动去下载) 68 | Confirm byte `json:"confirm"` //是否确认 69 | } 70 | 71 | type IosMessage struct { 72 | Aps *ApsAttr `json:"aps"` 73 | CustomContent map[string]interface{} `json:"custom_content,omitempty"` //参考android的自定义属性 74 | } 75 | 76 | type ApsAttr struct { 77 | Alert string `json:"alert"` 78 | Badge int `json:"badge,omitempty"` 79 | Sound string `json:"sound,omitempty"` 80 | } 81 | -------------------------------------------------------------------------------- /push.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | import ( 17 | "encoding/json" 18 | "errors" 19 | "time" 20 | ) 21 | 22 | type ReqPush struct { 23 | PushType PushType 24 | DeviceToken string // for single-device push 25 | UserAccounts []string 26 | Tags []string 27 | TagsOp string 28 | MessageType MessageType 29 | Message interface{} 30 | ExpireTime int 31 | SendTime time.Time 32 | MultiPkgType MultiPkgType 33 | PushEnv PushEnv 34 | PlatformType PlatformType 35 | LoopTimes int 36 | LoopInterval int 37 | Cli *Client 38 | } 39 | 40 | func (req *ReqPush) Push() error { 41 | var request *Request 42 | 43 | switch req.PushType { 44 | case PushType_single_device: 45 | request = req.Cli.NewRequest("GET", singleDeviceUrl) 46 | request.SetParam("device_token", req.DeviceToken) 47 | case PushType_single_account: 48 | request = req.Cli.NewRequest("GET", singleAccountUrl) 49 | request.SetParam("account", req.UserAccounts[0]) 50 | case PushType_multi_account: 51 | request = req.Cli.NewRequest("GET", multiAccountUrl) 52 | 53 | accounts, err := json.Marshal(req.UserAccounts) 54 | if err != nil { 55 | return errors.New(" marshal account list err:" + err.Error()) 56 | } 57 | request.SetParam("account_list", string(accounts)) 58 | case PushType_all_device: 59 | request = req.Cli.NewRequest("GET", allDeviceUrl) 60 | // request.SetParam("loop_times", req.LoopTimes) 61 | // request.SetParam("loop_interval", req.LoopInterval) 62 | case PushType_tags_device: 63 | request = req.Cli.NewRequest("GET", tagsDeviceUrl) 64 | 65 | tags, err := json.Marshal(req.Tags) 66 | if err != nil { 67 | return errors.New(" marshal tag list err:" + err.Error()) 68 | } 69 | request.SetParam("tags_list", string(tags)) 70 | request.SetParam("tags_op", req.TagsOp) 71 | // request.SetParam("loop_times", req.LoopTimes) 72 | // request.SetParam("loop_interval", req.LoopInterval) 73 | default: 74 | return errors.New(" invalid request push type.") 75 | } 76 | 77 | request.SetParam("message_type", req.MessageType) 78 | 79 | message := "" 80 | switch req.PlatformType { 81 | case Platform_android: 82 | // message 83 | if androidMsg, ok := req.Message.(*AndroidMessage); ok { 84 | androidMessage, err := json.Marshal(androidMsg) 85 | if err != nil { 86 | return errors.New(" marshal android message err:" + err.Error()) 87 | } 88 | 89 | message = string(androidMessage) 90 | } else { 91 | return errors.New(" invalid android message content.") 92 | } 93 | 94 | case Platform_ios: 95 | // message 96 | if iosMsg, ok := req.Message.(*IosMessage); ok { 97 | iosMessage, err := json.Marshal(iosMsg) 98 | if err != nil { 99 | return errors.New(" marshal ios message err:" + err.Error()) 100 | } 101 | 102 | message = string(iosMessage) 103 | } else { 104 | return errors.New(" invalid ios message content.") 105 | } 106 | } 107 | request.SetParam("message", message) 108 | 109 | request.SetParam("expire_time", req.ExpireTime) 110 | request.SetParam("send_time", req.SendTime.Format("2006-01-03 15:33:34")) 111 | // multi_pkg 112 | request.SetParam("multi_pkg", req.MultiPkgType) 113 | // environment 114 | request.SetParam("environment", req.PushEnv) 115 | 116 | response, err := request.Execute() 117 | if err != nil { 118 | return errors.New(" request err:" + err.Error()) 119 | } 120 | 121 | if !response.OK() { 122 | return errors.New(" response err:" + response.Error()) 123 | } 124 | 125 | return nil 126 | } 127 | -------------------------------------------------------------------------------- /request.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | import ( 17 | "crypto/md5" 18 | "encoding/json" 19 | "fmt" 20 | "github.com/aiwuTech/httpclient" 21 | "log" 22 | "net/url" 23 | "sort" 24 | "strings" 25 | "time" 26 | ) 27 | 28 | type Request struct { 29 | HttpMethod string 30 | HttpUrl string 31 | Params map[string]interface{} 32 | Client *Client 33 | } 34 | 35 | func (req *Request) SetParam(name string, value interface{}) { 36 | req.Params[name] = value 37 | } 38 | 39 | func (req *Request) SetParams(params map[string]interface{}) { 40 | for k, v := range params { 41 | req.SetParam(k, v) 42 | } 43 | } 44 | 45 | func (req *Request) Execute() (*Response, error) { 46 | body, err := req.doRequestAndGetBody() 47 | if err != nil { 48 | return nil, err 49 | } 50 | 51 | log.Println(string(body)) 52 | rspMsg := new(Response) 53 | if err := json.Unmarshal(body, rspMsg); err != nil { 54 | return nil, err 55 | } 56 | 57 | return rspMsg, nil 58 | } 59 | 60 | func (req *Request) doRequestAndGetBody() ([]byte, error) { 61 | urls := req.HttpUrl + "?" + req.queryString() 62 | 63 | rsp, err := httpclient.ForwardHttp(req.HttpMethod, urls, nil) 64 | if err != nil { 65 | return nil, err 66 | } 67 | 68 | body := httpclient.GetForwardHttpBody(rsp.Body) 69 | return body, nil 70 | } 71 | 72 | func (req *Request) queryString() string { 73 | reqParams := req.makeRequestParams() 74 | sign := req.md5Signature(reqParams) 75 | 76 | values := url.Values{} 77 | values.Add("sign", sign) 78 | for k, v := range reqParams { 79 | values.Add(k, fmt.Sprintf("%v", v)) 80 | } 81 | 82 | return values.Encode() 83 | } 84 | 85 | /** 86 | 内容签名。生成规则: 87 | A)提取请求方法method(GET或POST); 88 | B)提取请求url信息,包括Host字段的IP或域名和URI的path部分,注意不包括Host的端口和Path的querystring。请在请求中带上Host字段,否则将视为无效请求。 89 | 比如openapi.xg.qq.com/v2/push/single_device或者10.198.18.239/v2/push/single_device; 90 | C)将请求参数(不包括sign参数)格式化成K=V方式,注意:计算sign时所有参数不应进行urlencode; 91 | D)将格式化后的参数以K的字典序升序排列,拼接在一起,注意字典序中大写字母在前; 92 | E)拼接请求方法、url、排序后格式化的字符串以及应用的secret_key; 93 | F)将E形成字符串计算MD5值,形成一个32位的十六进制(字母小写)字符串,即为本次请求sign(签名)的值; 94 | Sign=MD5($http_method$url$k1=$v1$k2=$v2$secret_key); 该签名值基本可以保证请求是合法者发送且参数没有被修改,但无法保证不被偷窥。 95 | 例如: POST请求到接口http://openapi.xg.qq.com/v2/push/single_device, 96 | 有四个参数,access_id=123,timestamp=1386691200,Param1=Value1,Param2=Value2,secret_key为abcde。 97 | 则上述E步骤拼接出的字符串为 98 | POSTopenapi.xg.qq.com/v2/push/single_deviceParam1=Value1Param2=Value2access_id=123timestamp=1386691200abcde, 99 | 注意字典序中大写在前。计算出该字符串的MD5为ccafecaef6be07493cfe75ebc43b7d53,以此作为sign参数的值 100 | */ 101 | func (req *Request) md5Signature(params map[string]interface{}) string { 102 | origin := req.joinRequestParams(params) 103 | if origin == "" { 104 | return "" 105 | } 106 | 107 | urls, err := url.ParseRequestURI(req.HttpUrl) 108 | if err != nil { 109 | return "" 110 | } 111 | 112 | origin = req.HttpMethod + urls.Host + urls.Path + origin + req.Client.SecretKey 113 | 114 | c := md5.New() 115 | c.Write([]byte(origin)) 116 | return strings.ToLower(fmt.Sprintf("%X", c.Sum(nil))) 117 | } 118 | 119 | func (req *Request) joinRequestParams(params map[string]interface{}) string { 120 | if params == nil || len(params) == 0 { 121 | return "" 122 | } 123 | 124 | keys := make([]string, 0) 125 | origin := "" 126 | for k, _ := range params { 127 | keys = append(keys, k) 128 | } 129 | sort.Strings(keys) 130 | 131 | for _, key := range keys { 132 | origin += key + fmt.Sprintf("=%v", params[key]) 133 | } 134 | 135 | return origin 136 | } 137 | 138 | func (req *Request) makeRequestParams() map[string]interface{} { 139 | ps := make(map[string]interface{}) 140 | 141 | ps["access_id"] = req.Client.AccessId 142 | ps["timestamp"] = time.Now().Unix() 143 | ps["valid_time"] = req.Client.ValidTime 144 | 145 | for k, v := range req.Params { 146 | ps[k] = v 147 | } 148 | 149 | return ps 150 | } 151 | -------------------------------------------------------------------------------- /response.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | type Response struct { 17 | RetCode int `json:"ret_code"` 18 | ErrMsg string `json:"err_msg,omitempty"` 19 | Result interface{} `json:"result,omitempty"` 20 | } 21 | 22 | func (rsp *Response) OK() bool { 23 | return rsp.RetCode == 0 24 | } 25 | 26 | func (rsp *Response) Error() string { 27 | return rsp.ErrMsg 28 | } 29 | -------------------------------------------------------------------------------- /tags.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 mint.zhao.chiu@gmail.com 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 | // not use this file except in compliance with the License. You may obtain 5 | // a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 | // License for the specific language governing permissions and limitations 13 | // under the License. 14 | package xinge 15 | 16 | import "errors" 17 | 18 | func (cli *Client) AppTags(start, limit int) (int64, []string, error) { 19 | tags := make([]string, 0) 20 | 21 | request := cli.NewRequest("GET", appTagsUrl) 22 | 23 | request.SetParam("start", start) 24 | request.SetParam("limit", limit) 25 | 26 | response, err := request.Execute() 27 | if err != nil { 28 | return 0, tags, errors.New(" request err:" + err.Error()) 29 | } 30 | 31 | if !response.OK() { 32 | return 0, tags, errors.New(" response err:" + response.Error()) 33 | } 34 | 35 | result := response.Result.(map[string]interface{}) 36 | total := int64(result["total"].(float64)) 37 | if total <= 0 { 38 | return 0, tags, nil 39 | } 40 | 41 | tagList := result["tags"].([]interface{}) 42 | for _, tag := range tagList { 43 | tags = append(tags, tag.(string)) 44 | } 45 | 46 | return total, tags, nil 47 | } 48 | 49 | func (cli *Client) SetTags(tagTokenList ...[2]string) error { 50 | request := cli.NewRequest("GET", batchSetTagsUrl) 51 | 52 | tagTokens := "[" 53 | for _, tagToken := range tagTokenList { 54 | pair := `["` + tagToken[0] + `","` + tagToken[1] + `"]` + "," 55 | tagTokens += pair 56 | } 57 | tagTokens = tagTokens[:len(tagTokens)-1] 58 | tagTokens += "]" 59 | 60 | request.SetParam("tag_token_list", tagTokens) 61 | response, err := request.Execute() 62 | if err != nil { 63 | return errors.New(" request err:" + err.Error()) 64 | } 65 | 66 | if !response.OK() { 67 | return errors.New(" response err:" + response.Error()) 68 | } 69 | 70 | return nil 71 | } 72 | 73 | func (cli *Client) DelTags(tagTokenList ...[2]string) error { 74 | request := cli.NewRequest("GET", batchDelTagsUrl) 75 | 76 | tagTokens := "[" 77 | for _, tagToken := range tagTokenList { 78 | pair := `["` + tagToken[0] + `","` + tagToken[1] + `"]` + "," 79 | tagTokens += pair 80 | } 81 | tagTokens = tagTokens[:len(tagTokens)-1] 82 | tagTokens += "]" 83 | 84 | request.SetParam("tag_token_list", tagTokens) 85 | response, err := request.Execute() 86 | if err != nil { 87 | return errors.New(" request err:" + err.Error()) 88 | } 89 | 90 | if !response.OK() { 91 | return errors.New(" response err:" + response.Error()) 92 | } 93 | 94 | return nil 95 | } 96 | 97 | func (cli *Client) TokenTags(token string) ([]string, error) { 98 | tags := make([]string, 0) 99 | request := cli.NewRequest("GET", tokenTagsUrl) 100 | 101 | request.SetParam("device_token", token) 102 | response, err := request.Execute() 103 | if err != nil { 104 | return tags, errors.New(" request err:" + err.Error()) 105 | } 106 | 107 | if !response.OK() { 108 | return tags, errors.New(" response err:" + response.Error()) 109 | } 110 | 111 | result := response.Result.(map[string]interface{}) 112 | tagList := result["tags"].([]interface{}) 113 | for _, tag := range tagList { 114 | tags = append(tags, tag.(string)) 115 | } 116 | 117 | return tags, nil 118 | } 119 | 120 | func (cli *Client) TagTokensNum(tag string) (int64, error) { 121 | request := cli.NewRequest("GET", tagTokensUrl) 122 | 123 | request.SetParam("tag", tag) 124 | response, err := request.Execute() 125 | if err != nil { 126 | return 0, errors.New(" request err:" + err.Error()) 127 | } 128 | 129 | if !response.OK() { 130 | return 0, errors.New(" response err:" + response.Error()) 131 | } 132 | 133 | result := response.Result.(map[string]interface{}) 134 | return int64(result["device_num"].(float64)), nil 135 | } 136 | --------------------------------------------------------------------------------